]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/internal/ProjectFeatureRegistry.java
Fix CopyAdvisorUtil.copy2 to copy IsRelatedTo-statements left out before
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / internal / ProjectFeatureRegistry.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project.internal;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtension;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
27 import org.eclipse.core.runtime.dynamichelpers.IFilter;
28 import org.simantics.project.features.registry.GroupReference;
29 import org.simantics.project.features.registry.IProjectFeatureExtension;
30 import org.simantics.project.features.registry.IProjectFeatureRegistry;
31 import org.simantics.project.features.registry.InjectedDependency;
32 import org.simantics.project.features.registry.ProjectFeatureReference;
33 import org.simantics.utils.strings.StringUtils;
34
35 /**
36  * This registry implementation is not properly dynamic-enabled.
37  * injectDependency is not handled well enough to work dynamically.
38  * 
39  * @author Tuukka Lehtonen
40  */
41 public class ProjectFeatureRegistry implements IProjectFeatureRegistry, IExtensionChangeHandler {
42
43     private final static String        NAMESPACE         = "org.simantics.project";
44
45     private final static String        EP_NAME           = "feature";
46
47     private final static String        FEATURE           = "feature";
48
49     private final static String        INJECT_DEPENDENCY = "injectDependency";
50
51     private final ExtensionTracker     tracker;
52
53     private IProjectFeatureExtension[] extensions        = new IProjectFeatureExtension[0];
54
55     public ProjectFeatureRegistry() {
56         tracker = new ExtensionTracker();
57
58         // Cache defined actions
59         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
60         loadExtensions(expt.getConfigurationElements());
61
62         // Start tracking for new and removed extensions
63         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
64         tracker.registerHandler(this, filter);
65     }
66
67     private void loadExtensions(IConfigurationElement[] configurationElements) {
68         Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));
69
70         // These are all "feature" elements with required attributes
71         //  - id
72         //  - feature
73         for (IConfigurationElement el : configurationElements) {
74             if (FEATURE.equals(el.getName())) {
75                 String id = StringUtils.safeString(el.getAttribute("id"));
76                 if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)
77                     System.out.println(this + " Trying to load project feature extension id '" + id + "' contributed by " + el.getContributor().getName());
78                 if (id.isEmpty()) {
79                     // Ignore extension without an ID
80                     // TODO: log warning
81                     if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)
82                         System.out.println(this + " skipping feature with empty ID contributed by " + el.getContributor().getName());
83                     continue;
84                 }
85                 if (StringUtils.safeString(el.getAttribute("class")).isEmpty()) {
86                     // Ignore extension without a feature class
87                     // TODO: log warning
88                     if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)
89                         System.out.println(this + " skipping feature missing 'class' attribute contributed by " + el.getContributor().getName());
90                     continue;
91                 }
92                 // Load optional attributes
93                 String label = StringUtils.safeString(el.getAttribute("label"));
94                 String description = StringUtils.safeString(el.getAttribute("description"));
95                 boolean published = "true".equalsIgnoreCase(el.getAttribute("published"));
96                 Collection<ProjectFeatureReference> requires = readProjectFeatureReferenceCollection(el, "requires");
97                 Collection<InjectedDependency> injections = readInjectedDependencies(el, id);
98                 Collection<GroupReference> installGroups = readGroupReferenceCollection(el, "installGroup");
99
100                 ProjectFeatureExtension ext = new ProjectFeatureExtension(el, id, label, description, published, requires, injections, installGroups);
101
102                 // Start tracking the new extension object, its removal will be notified of
103                 // with removeExtension(extension, Object[]).
104                 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
105
106                 newExtensions.add(ext);
107             }
108         }
109
110         // Atomic assignment
111         this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);
112     }
113
114     private Collection<InjectedDependency> readInjectedDependencies(IConfigurationElement element, String id) {
115         Collection<InjectedDependency> result = new ArrayList<InjectedDependency>();
116
117         for (IConfigurationElement child : element.getChildren(INJECT_DEPENDENCY)) {
118             String targetId = StringUtils.safeString(child.getAttribute("targetId"));
119             if (targetId.isEmpty())
120                 // Invalid extension
121                 return null;
122             
123             result.add(new InjectedDependency(new ProjectFeatureReference(id, false), new ProjectFeatureReference(targetId, false)));
124             
125         }
126
127         return result;
128     }
129
130     private Collection<ProjectFeatureReference> readProjectFeatureReferenceCollection(IConfigurationElement element, String childName) {
131         Collection<ProjectFeatureReference> result = new ArrayList<ProjectFeatureReference>();
132
133         for (IConfigurationElement child : element.getChildren(childName)) {
134             String id = StringUtils.safeString(child.getAttribute("id"));
135             if (id.isEmpty()) {
136                 // Invalid extension
137                 continue;
138             }
139             boolean optional = "true".equalsIgnoreCase( child.getAttribute("optional") );
140             result.add(new ProjectFeatureReference(id, optional));
141         }
142
143         return result;
144     }
145
146     private Collection<GroupReference> readGroupReferenceCollection(IConfigurationElement element, String childName) {
147         Collection<GroupReference> result = new ArrayList<GroupReference>();
148
149         for (IConfigurationElement child : element.getChildren(childName)) {
150             String id = StringUtils.safeString(child.getAttribute("id"));
151             if (id.isEmpty()) {
152                 // Invalid extension
153                 // TODO: log warning
154                 continue;
155             }
156             String version = StringUtils.safeString(child.getAttribute("version"));
157             if (version.isEmpty())
158                 // Empty version implies no version, mark that with null.
159                 version = null;
160             result.add(new GroupReference(id, version));
161         }
162
163         return result;
164     }
165
166     @Override
167     public void addExtension(IExtensionTracker tracker, IExtension extension) {
168         loadExtensions(extension.getConfigurationElements());
169     }
170
171     @Override
172     public void removeExtension(IExtension extension, Object[] objects) {
173         Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));
174
175         for (Object o : objects) {
176             tracker.unregisterObject(extension, o);
177             newExtensions.remove(o);
178         }
179
180         // Atomic assignment
181         this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);
182     }
183
184     /* (non-Javadoc)
185      * @see org.simantics.project.IProjectFeatureRegistry#getExtensions()
186      */
187     @Override
188     public IProjectFeatureExtension[] getExtensions() {
189         return extensions;
190     }
191
192     @Override
193     public IProjectFeatureExtension getExtensionById(String id) {
194         if (id == null)
195             throw new IllegalArgumentException("null id");
196
197         for (IProjectFeatureExtension ext : extensions) {
198             if (id.equals(ext.getId()))
199                 return ext;
200         }
201         return null;
202     }
203
204 }