1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.project.internal;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.HashSet;
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;
36 * This registry implementation is not properly dynamic-enabled.
37 * injectDependency is not handled well enough to work dynamically.
39 * @author Tuukka Lehtonen
41 public class ProjectFeatureRegistry implements IProjectFeatureRegistry, IExtensionChangeHandler {
43 private final static String NAMESPACE = "org.simantics.project";
45 private final static String EP_NAME = "feature";
47 private final static String FEATURE = "feature";
49 private final static String INJECT_DEPENDENCY = "injectDependency";
51 private final ExtensionTracker tracker;
53 private IProjectFeatureExtension[] extensions = new IProjectFeatureExtension[0];
55 public ProjectFeatureRegistry() {
56 tracker = new ExtensionTracker();
58 // Cache defined actions
59 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
60 loadExtensions(expt.getConfigurationElements());
62 // Start tracking for new and removed extensions
63 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
64 tracker.registerHandler(this, filter);
67 private void loadExtensions(IConfigurationElement[] configurationElements) {
68 Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));
70 // These are all "feature" elements with required attributes
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());
79 // Ignore extension without an ID
81 if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)
82 System.out.println(this + " skipping feature with empty ID contributed by " + el.getContributor().getName());
85 if (StringUtils.safeString(el.getAttribute("class")).isEmpty()) {
86 // Ignore extension without a feature class
88 if (ProjectPolicy.TRACE_PROJECT_FEATURE_LOAD)
89 System.out.println(this + " skipping feature missing 'class' attribute contributed by " + el.getContributor().getName());
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");
100 ProjectFeatureExtension ext = new ProjectFeatureExtension(el, id, label, description, published, requires, injections, installGroups);
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);
106 newExtensions.add(ext);
111 this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);
114 private Collection<InjectedDependency> readInjectedDependencies(IConfigurationElement element, String id) {
115 Collection<InjectedDependency> result = new ArrayList<InjectedDependency>();
117 for (IConfigurationElement child : element.getChildren(INJECT_DEPENDENCY)) {
118 String targetId = StringUtils.safeString(child.getAttribute("targetId"));
119 if (targetId.isEmpty())
123 result.add(new InjectedDependency(new ProjectFeatureReference(id, false), new ProjectFeatureReference(targetId, false)));
130 private Collection<ProjectFeatureReference> readProjectFeatureReferenceCollection(IConfigurationElement element, String childName) {
131 Collection<ProjectFeatureReference> result = new ArrayList<ProjectFeatureReference>();
133 for (IConfigurationElement child : element.getChildren(childName)) {
134 String id = StringUtils.safeString(child.getAttribute("id"));
139 boolean optional = "true".equalsIgnoreCase( child.getAttribute("optional") );
140 result.add(new ProjectFeatureReference(id, optional));
146 private Collection<GroupReference> readGroupReferenceCollection(IConfigurationElement element, String childName) {
147 Collection<GroupReference> result = new ArrayList<GroupReference>();
149 for (IConfigurationElement child : element.getChildren(childName)) {
150 String id = StringUtils.safeString(child.getAttribute("id"));
156 String version = StringUtils.safeString(child.getAttribute("version"));
157 if (version.isEmpty())
158 // Empty version implies no version, mark that with null.
160 result.add(new GroupReference(id, version));
167 public void addExtension(IExtensionTracker tracker, IExtension extension) {
168 loadExtensions(extension.getConfigurationElements());
172 public void removeExtension(IExtension extension, Object[] objects) {
173 Set<IProjectFeatureExtension> newExtensions = new HashSet<IProjectFeatureExtension>(Arrays.asList(extensions));
175 for (Object o : objects) {
176 tracker.unregisterObject(extension, o);
177 newExtensions.remove(o);
181 this.extensions = newExtensions.toArray(new IProjectFeatureExtension[newExtensions.size()]);
185 * @see org.simantics.project.IProjectFeatureRegistry#getExtensions()
188 public IProjectFeatureExtension[] getExtensions() {
193 public IProjectFeatureExtension getExtensionById(String id) {
195 throw new IllegalArgumentException("null id");
197 for (IProjectFeatureExtension ext : extensions) {
198 if (id.equals(ext.getId()))