Skip to content

Package: ExtensionParser$ExtensionDescriptor

ExtensionParser$ExtensionDescriptor

nameinstructionbranchcomplexitylinemethod
ExtensionParser.ExtensionDescriptor(ElementRegistry, String, String, IConfigurationElement)
M: 3 C: 69
96%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 15
94%
M: 0 C: 1
100%
getClassAttributeName()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getConfigurationElement()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getType()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolve()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /********************************************************************************
2: * Copyright (c) 2011 Eike Stepper (Berlin, Germany) and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eike Stepper - initial API and implementation
13: ********************************************************************************/
14: package org.eclipse.emf.ecp.internal.core.util;
15:
16: import java.io.File;
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.IExtensionDelta;
23: import org.eclipse.core.runtime.IExtensionPoint;
24: import org.eclipse.core.runtime.IRegistryChangeEvent;
25: import org.eclipse.core.runtime.IRegistryChangeListener;
26: import org.eclipse.core.runtime.Platform;
27: import org.eclipse.emf.ecp.spi.core.util.ECPDisposable;
28: import org.eclipse.emf.ecp.spi.core.util.InternalRegistryElement;
29: import org.eclipse.net4j.util.lifecycle.Lifecycle;
30: import org.osgi.framework.Bundle;
31:
32: /**
33: * @author Eike Stepper
34: * @param <ELEMENT>
35: */
36: public abstract class ExtensionParser<ELEMENT extends InternalRegistryElement> extends Lifecycle implements
37:         IRegistryChangeListener {
38:         private final ElementRegistry<ELEMENT, ?> elementRegistry;
39:
40:         private final String namespace;
41:
42:         private final String name;
43:
44:         public ExtensionParser(ElementRegistry<ELEMENT, ?> elementRegistry, String namespace, String name) {
45:                 this.elementRegistry = elementRegistry;
46:                 this.namespace = namespace;
47:                 this.name = name;
48:         }
49:
50:         public final ElementRegistry<ELEMENT, ?> getElementRegistry() {
51:                 return elementRegistry;
52:         }
53:
54:         public final String getNamespace() {
55:                 return namespace;
56:         }
57:
58:         public final String getName() {
59:                 return name;
60:         }
61:
62:         /** {@inheritDoc} */
63:         @Override
64:         public void registryChanged(IRegistryChangeEvent event) {
65:                 final Set<String> remove = new HashSet<String>();
66:                 final Set<ELEMENT> add = new HashSet<ELEMENT>();
67:                 for (final IExtensionDelta delta : event.getExtensionDeltas(namespace, name)) {
68:                         final IExtension extension = delta.getExtension();
69:                         final int kind = delta.getKind();
70:                         switch (kind) {
71:                         case IExtensionDelta.ADDED:
72:                                 addExtension(extension, add);
73:                                 break;
74:
75:                         case IExtensionDelta.REMOVED:
76:                                 removeExtension(extension, remove);
77:                                 break;
78:                         default:
79:                                 break;
80:                         }
81:                 }
82:
83:                 final Set<ELEMENT> removedElements = elementRegistry.doChangeElements(remove, add);
84:                 if (removedElements != null) {
85:                         for (final ELEMENT removedElement : removedElements) {
86:                                 if (removedElement instanceof ECPDisposable) {
87:                                         final ECPDisposable disposable = removedElement;
88:                                         disposable.dispose();
89:                                 }
90:                         }
91:                 }
92:         }
93:
94:         @Override
95:         protected void doActivate() throws Exception {
96:                 super.doActivate();
97:
98:                 final String extensionPointID = namespace + "." + name; //$NON-NLS-1$
99:                 final Set<ELEMENT> add = new HashSet<ELEMENT>();
100:
101:                 final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(extensionPointID);
102:                 for (final IExtension extension : extensionPoint.getExtensions()) {
103:                         addExtension(extension, add);
104:                 }
105:
106:                 elementRegistry.doChangeElements(null, add);
107:                 Platform.getExtensionRegistry().addRegistryChangeListener(this, namespace);
108:         }
109:
110:         @Override
111:         protected void doDeactivate() throws Exception {
112:                 Platform.getExtensionRegistry().removeRegistryChangeListener(this);
113:                 super.doDeactivate();
114:         }
115:
116:         protected void addExtension(IExtension extension, Set<ELEMENT> result) {
117:                 final String name = extension.getUniqueIdentifier();
118:                 final IConfigurationElement configurationElement = extension.getConfigurationElements()[0];
119:
120:                 final ELEMENT element = createElement(name, configurationElement);
121:                 element.setLabel(extension.getLabel());
122:                 element.setDescription(configurationElement.getAttribute("description")); //$NON-NLS-1$
123:                 result.add(element);
124:         }
125:
126:         protected void removeExtension(IExtension extension, Set<String> result) {
127:                 final String name = extension.getUniqueIdentifier();
128:                 result.add(name);
129:         }
130:
131:         protected abstract ELEMENT createElement(String name, IConfigurationElement configurationElement);
132:
133:         /**
134:          * @author Eike Stepper
135:          * @param <ELEMENT> The type of this descriptor which must extend {@link InternalRegistryElement}
136:          */
137:         public static class ExtensionDescriptor<ELEMENT extends InternalRegistryElement>
138:                 extends ElementDescriptor<ELEMENT> {
139:                 private final String type;
140:
141:                 private final IConfigurationElement configurationElement;
142:
143:                 public ExtensionDescriptor(ElementRegistry<ELEMENT, ?> registry, String name, String type,
144:                         IConfigurationElement configurationElement) {
145:                         super(registry, name);
146:                         this.type = type;
147:                         this.configurationElement = configurationElement;
148:
149:                         try {
150:                                 final String bundleName = configurationElement.getContributor().getName();
151:                                 final Bundle bundle = Platform.getBundle(bundleName);
152:                                 String location = bundle.getLocation();
153:
154:•                                if (location.startsWith("initial@")) { //$NON-NLS-1$
155:                                         location = location.substring("initial@".length()); //$NON-NLS-1$
156:                                 }
157:
158:                                 final String prefix = "reference:file:"; //$NON-NLS-1$
159:•                                if (location.startsWith(prefix)) {
160:                                         location = location.substring(prefix.length());
161:
162:                                         // TODO Trace properly
163:                                         System.out.println(getClass().getSimpleName() + ": " + bundleName + " [" + bundle.getBundleId() //$NON-NLS-1$ //$NON-NLS-2$
164:                                                 + "] --> file:" + new File(location).getCanonicalPath()); //$NON-NLS-1$
165:                                 }
166:                         } catch (final Exception ex) {
167:                                 ex.printStackTrace();
168:                         }
169:                 }
170:
171:                 /** {@inheritDoc} */
172:                 @Override
173:                 public String getType() {
174:                         return type;
175:                 }
176:
177:                 public final IConfigurationElement getConfigurationElement() {
178:                         return configurationElement;
179:                 }
180:
181:                 @Override
182:                 @SuppressWarnings("unchecked")
183:                 protected ELEMENT resolve() throws Exception {
184:                         return (ELEMENT) configurationElement.createExecutableExtension(getClassAttributeName());
185:                 }
186:
187:                 protected String getClassAttributeName() {
188:                         return "class"; //$NON-NLS-1$
189:                 }
190:         }
191: }