Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 40b36b8fa7287a671c53d4f7f7a5d7b32cbd58ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.te.core.extensions;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tm.te.core.activator.CoreBundleActivator;
import org.eclipse.tm.te.core.nls.Messages;

/**
 * Target Explorer: Abstract extension point manager implementation.
 */
public abstract class AbstractExtensionPointManager<V> {
	// Flag to mark the extension point manager initialized (extensions loaded).
	private boolean initialized = false;
	// The map of loaded extension listed by their unique id's
	private Map<String, ExecutableExtensionProxy<V>> extensionsMap = new LinkedHashMap<String, ExecutableExtensionProxy<V>>();

	/**
	 * Constructor.
	 */
	public AbstractExtensionPointManager() {
	}

	/**
	 * Returns if or if not the extension point manager got initialized already.
	 * <p>
	 * Initialized means that the manager read the extensions for the managed extension point.
	 *
	 * @return <code>True</code> if already initialized, <code>false</code> otherwise.
	 */
	protected boolean isInitialized() {
		return initialized;
	}

	/**
	 * Sets if or if not the extension point manager is initialized.
	 * <p>
	 * Initialized means that the manager has read the extensions for the managed extension point.
	 *
	 * @return <code>True</code> to set the extension point manager is initialized, <code>false</code> otherwise.
	 */
	protected void setInitialized(boolean initialized) {
		this.initialized = initialized;
	}

	/**
	 * Returns the map of managed extensions. If not loaded before,
	 * this methods trigger the loading of the extensions to the managed
	 * extension point.
	 *
	 * @return The map of extensions.
	 */
	protected Map<String, ExecutableExtensionProxy<V>> getExtensions() {
		// Load and store the extensions thread-safe!
		synchronized (extensionsMap) {
			if (!isInitialized()) { loadExtensions(); setInitialized(true); }
		}
		return extensionsMap;
	}

	/**
	 * Returns the extension point id to read. The method
	 * must return never <code>null</code>.
	 *
	 * @return The extension point id.
	 */
	protected abstract String getExtensionPointId();

	/**
	 * Returns the configuration element name. The method
	 * must return never <code>null</code>.
	 *
	 * @return The configuration element name.
	 */
	protected abstract String getConfigurationElementName();

	/**
	 * Creates the extension proxy instance.
	 *
	 * @param element The configuration element of the extension. Must not be <code>null</code>.
	 * @return The extension proxy instance.
	 *
	 * @throws CoreException If the extension proxy instantiation failed.
	 */
	protected ExecutableExtensionProxy<V> doCreateExtensionProxy(IConfigurationElement element) throws CoreException {
		Assert.isNotNull(element);
		return new ExecutableExtensionProxy<V>(element);
	}

	/**
	 * Store the given extension to the given extensions store. Checks if an extension with the same id does exist
	 * already and throws an exception in this case.
	 *
	 * @param extensions The extensions store. Must not be <code>null</code>.
	 * @param candidate The extension. Must not be <code>null</code>.
	 * @param element The configuration element. Must not be <code>null</code>.
	 *
	 * @throws CoreException In case a extension with the same id as the given extension already exist.
	 */
	protected void doStoreExtensionTo(Map<String, ExecutableExtensionProxy<V>> extensions, ExecutableExtensionProxy<V> candidate, IConfigurationElement element) throws CoreException {
		Assert.isNotNull(extensions);
		Assert.isNotNull(candidate);
		Assert.isNotNull(element);

		// If no extension with this id had been registered before, register now.
		if (!extensions.containsKey(candidate.getId())) {
			extensions.put(candidate.getId(), candidate);
		}
		else {
			throw new CoreException(new Status(IStatus.ERROR,
					CoreBundleActivator.getUniqueIdentifier(),
					0,
					NLS.bind(Messages.Extension_error_duplicateExtension, candidate.getId(), element.getContributor().getName()),
					null));
		}
	}

	/**
	 * Loads the extensions for the managed extension point.
	 */
	protected void loadExtensions() {
		// If already initialized, this method will do nothing.
		if (isInitialized())  return;

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(getExtensionPointId());
		if (point != null) {
			IExtension[] extensions = point.getExtensions();
			for (IExtension extension : extensions) {
				IConfigurationElement[] elements = extension.getConfigurationElements();
				for (IConfigurationElement element : elements) {
					if (getConfigurationElementName().equals(element.getName())) {
						try {
							ExecutableExtensionProxy<V> candidate = doCreateExtensionProxy(element);
							if (candidate.getId() != null) {
								doStoreExtensionTo(extensionsMap, candidate, element);
							} else {
								throw new CoreException(new Status(IStatus.ERROR,
										CoreBundleActivator.getUniqueIdentifier(),
										0,
										NLS.bind(Messages.Extension_error_missingRequiredAttribute, "id", element.getAttribute("label")), //$NON-NLS-1$ //$NON-NLS-2$
										null));
							}
						} catch (CoreException e) {
							Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(new Status(IStatus.ERROR,
									CoreBundleActivator.getUniqueIdentifier(),
									NLS.bind(Messages.Extension_error_invalidExtensionPoint, element.getDeclaringExtension().getUniqueIdentifier()), e));
						}
					}
				}
			}
		}
	}
}

Back to the top