]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / internal / ProjectFeatureRegistry.java
diff --git a/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java b/bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java
new file mode 100644 (file)
index 0000000..ea126d9
--- /dev/null
@@ -0,0 +1,206 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.project.internal;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.IConfigurationElement;\r
+import org.eclipse.core.runtime.IExtension;\r
+import org.eclipse.core.runtime.IExtensionPoint;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
+import org.simantics.project.features.registry.GroupReference;\r
+import org.simantics.project.features.registry.IProjectFeatureExtension;\r
+import org.simantics.project.features.registry.IProjectFeatureRegistry;\r
+import org.simantics.project.features.registry.InjectedDependency;\r
+import org.simantics.project.features.registry.ProjectFeatureReference;\r
+import org.simantics.utils.strings.StringUtils;\r
+\r
+/**\r
+ * This registry implementation is not properly dynamic-enabled.\r
+ * injectDependency is not handled well enough to work dynamically.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ProjectFeatureRegistry implements IProjectFeatureRegistry, IExtensionChangeHandler {\r
+\r
+    private final static String        NAMESPACE         = "org.simantics.project";\r
+\r
+    private final static String        EP_NAME           = "feature";\r
+\r
+    private final static String        FEATURE           = "feature";\r
+\r
+    private final static String        INJECT_DEPENDENCY = "injectDependency";\r
+\r
+    private final ExtensionTracker     tracker;\r
+\r
+    private IProjectFeatureExtension[] extensions        = new IProjectFeatureExtension[0];\r
+\r
+    public ProjectFeatureRegistry() {\r
+        tracker = new ExtensionTracker();\r
+\r
+        // Cache defined actions\r
+        IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);\r
+        loadExtensions(expt.getConfigurationElements());\r
+\r
+        // Start tracking for new and removed extensions\r
+        IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);\r
+        tracker.registerHandler(this, filter);\r
+    }\r
+\r
+    private void loadExtensions(IConfigurationElement[] configurationElements) {\r
+        Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));\r
+\r
+        // These are all "feature" elements with required attributes\r
+        //  - id\r
+        //  - feature\r
+        for (IConfigurationElement el : configurationElements) {\r
+            if (FEATURE.equals(el.getName())) {\r
+                String id = StringUtils.safeString(el.getAttribute("id"));\r
+                if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)\r
+                    System.out.println(this + " Trying to load project feature extension id '" + id + "' contributed by " + el.getContributor().getName());\r
+                if (id.isEmpty()) {\r
+                    // Ignore extension without an ID\r
+                    // TODO: log warning\r
+                    if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)\r
+                        System.out.println(this + " skipping feature with empty ID contributed by " + el.getContributor().getName());\r
+                    continue;\r
+                }\r
+                if (StringUtils.safeString(el.getAttribute("class")).isEmpty()) {\r
+                    // Ignore extension without a feature class\r
+                    // TODO: log warning\r
+                    if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)\r
+                        System.out.println(this + " skipping feature missing 'class' attribute contributed by " + el.getContributor().getName());\r
+                    continue;\r
+                }\r
+                // Load optional attributes\r
+                String label = StringUtils.safeString(el.getAttribute("label"));\r
+                String description = StringUtils.safeString(el.getAttribute("description"));\r
+                boolean published = "true".equalsIgnoreCase(el.getAttribute("published"));\r
+                Collection<ProjectFeatureReference> requires = readProjectFeatureReferenceCollection(el, "requires");\r
+                Collection<InjectedDependency> injections = readInjectedDependencies(el);\r
+                Collection<GroupReference> installGroups = readGroupReferenceCollection(el, "installGroup");\r
+\r
+                ProjectFeatureExtension ext = new ProjectFeatureExtension(el, id, label, description, published, requires, injections, installGroups);\r
+\r
+                // Start tracking the new extension object, its removal will be notified of\r
+                // with removeExtension(extension, Object[]).\r
+                tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
+\r
+                newExtensions.add(ext);\r
+            }\r
+        }\r
+\r
+        // Atomic assignment\r
+        this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);\r
+    }\r
+\r
+    private Collection<InjectedDependency> readInjectedDependencies(IConfigurationElement element) {\r
+        Collection<InjectedDependency> result = new ArrayList<InjectedDependency>();\r
+\r
+        for (IConfigurationElement child : element.getChildren(INJECT_DEPENDENCY)) {\r
+            String id = StringUtils.safeString(child.getAttribute("id"));\r
+            if (id.isEmpty())\r
+                // Invalid extension\r
+                return null;\r
+\r
+            String targetId = StringUtils.safeString(child.getAttribute("targetId"));\r
+            if (targetId.isEmpty())\r
+                // Invalid extension\r
+                return null;\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    private Collection<ProjectFeatureReference> readProjectFeatureReferenceCollection(IConfigurationElement element, String childName) {\r
+        Collection<ProjectFeatureReference> result = new ArrayList<ProjectFeatureReference>();\r
+\r
+        for (IConfigurationElement child : element.getChildren(childName)) {\r
+            String id = StringUtils.safeString(child.getAttribute("id"));\r
+            if (id.isEmpty()) {\r
+                // Invalid extension\r
+                continue;\r
+            }\r
+            boolean optional = "true".equalsIgnoreCase( child.getAttribute("optional") );\r
+            result.add(new ProjectFeatureReference(id, optional));\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    private Collection<GroupReference> readGroupReferenceCollection(IConfigurationElement element, String childName) {\r
+        Collection<GroupReference> result = new ArrayList<GroupReference>();\r
+\r
+        for (IConfigurationElement child : element.getChildren(childName)) {\r
+            String id = StringUtils.safeString(child.getAttribute("id"));\r
+            if (id.isEmpty()) {\r
+                // Invalid extension\r
+                // TODO: log warning\r
+                continue;\r
+            }\r
+            String version = StringUtils.safeString(child.getAttribute("version"));\r
+            if (version.isEmpty())\r
+                // Empty version implies no version, mark that with null.\r
+                version = null;\r
+            result.add(new GroupReference(id, version));\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
+        loadExtensions(extension.getConfigurationElements());\r
+    }\r
+\r
+    @Override\r
+    public void removeExtension(IExtension extension, Object[] objects) {\r
+        Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));\r
+\r
+        for (Object o : objects) {\r
+            tracker.unregisterObject(extension, o);\r
+            newExtensions.remove(o);\r
+        }\r
+\r
+        // Atomic assignment\r
+        this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see org.simantics.project.IProjectFeatureRegistry#getExtensions()\r
+     */\r
+    @Override\r
+    public IProjectFeatureExtension[] getExtensions() {\r
+        return extensions;\r
+    }\r
+\r
+    @Override\r
+    public IProjectFeatureExtension getExtensionById(String id) {\r
+        if (id == null)\r
+            throw new IllegalArgumentException("null id");\r
+\r
+        for (IProjectFeatureExtension ext : extensions) {\r
+            if (id.equals(ext.getId()))\r
+                return ext;\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r