Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68d88e48f81a0510be9cfb1ae8d57d666962bee8 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 * Copyright (c) 2011 Tasktop Technologies.
 * 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.commons.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.eclipse.core.runtime.Assert;
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.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

/**
 * @author Steffen Pingel
 * @author Sam Davis
 * @since 3.7
 */
public class ExtensionPointReader<T> {

	private static final String ATTRIBUTE_EXTENSION_PRIORITY = "extensionPriority"; //$NON-NLS-1$

	private static final class PriorityComparator implements Comparator<IConfigurationElement> {

		public int compare(IConfigurationElement arg0, IConfigurationElement arg1) {
			double p0 = 0;
			double p1 = 0;
			try {
				String priorityAttribute = arg0.getAttribute(ATTRIBUTE_EXTENSION_PRIORITY);
				if (priorityAttribute != null) {
					p0 = Double.parseDouble(priorityAttribute);
				}
			} catch (NumberFormatException e) {
			}
			try {
				String priorityAttribute = arg1.getAttribute(ATTRIBUTE_EXTENSION_PRIORITY);
				if (priorityAttribute != null) {
					p1 = Double.parseDouble(priorityAttribute);
				}
			} catch (NumberFormatException e) {
			}
			if (p1 > p0) {
				return 1;
			} else if (p1 < -p0) {
				return -1;
			}
			return 0;
		}
	}

	private static final PriorityComparator PRIORITY_COMPARATOR = new PriorityComparator();

	private final String extensionId;

	private final String elementId;

	private final Class<T> clazz;

	private String classAttributeId;

	private final String pluginId;

	private final List<T> items;

	private String filterAttributeId;

	private String filterAttributeValue;

	public ExtensionPointReader(String pluginId, String extensionId, String elementId, Class<T> clazz,
			String filterAttributeId, String filterAttributeValue) {
		this(pluginId, extensionId, elementId, clazz);
		this.filterAttributeId = filterAttributeId;
		this.filterAttributeValue = filterAttributeValue;
	}

	public ExtensionPointReader(String pluginId, String extensionId, String elementId, Class<T> clazz) {
		Assert.isNotNull(pluginId);
		Assert.isNotNull(extensionId);
		Assert.isNotNull(elementId);
		Assert.isNotNull(clazz);
		this.pluginId = pluginId;
		this.extensionId = extensionId;
		this.elementId = elementId;
		this.clazz = clazz;
		this.classAttributeId = "class"; //$NON-NLS-1$
		this.items = new ArrayList<T>();
	}

	public final String getPluginId() {
		return pluginId;
	}

	public final String getElementId() {
		return elementId;
	}

	public final void setClassAttributeId(String classAttributeId) {
		this.classAttributeId = classAttributeId;
	}

	public final String getClassAttributeId() {
		return classAttributeId;
	}

	public IStatus read() {
		items.clear();

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		if (registry == null) {
			return Status.CANCEL_STATUS;
		}

		MultiStatus result = new MultiStatus(pluginId, 0, NLS.bind(
				"Extensions for {0}/{1} failed to load", pluginId, elementId), null); //$NON-NLS-1$

		IExtensionPoint extensionPoint = registry.getExtensionPoint(pluginId + "." + extensionId); //$NON-NLS-1$
		if (extensionPoint != null) {
			IExtension[] extensions = extensionPoint.getExtensions();
			for (IExtension extension : extensions) {
				IConfigurationElement[] elements = extension.getConfigurationElements();
				Arrays.sort(elements, PRIORITY_COMPARATOR);
				for (IConfigurationElement element : elements) {
					if (element.getName().equals(elementId) && shouldRead(element)) {
						T item = readElement(element, result);
						if (item != null) {
							items.add(item);
						}
					}
				}
			}
		}

		handleResult(result);

		return result;
	}

	/**
	 * Determines whether the element should be instantiated by this ExtensionPointReader. This implementation checks
	 * whether the element defines an attribute with id and value matching filterAttributeId and filterAttributeValue.
	 * If filterAttributeValue is the empty string, an element is also considered to match if it does not define the
	 * attribute.
	 * <p>
	 * Subclasses may override.
	 */
	protected boolean shouldRead(IConfigurationElement element) {
		return filterAttributeId == null || filterAttributeValue == null
				|| filterAttributeValue.equals(element.getAttribute(filterAttributeId))
				|| (filterAttributeValue.length() == 0 && element.getAttribute(filterAttributeId) == null);
	}

	protected void handleResult(IStatus result) {
		if (!result.isOK()) {
			StatusHandler.log(result);
		}
	}

	public T getItem() {
		return (items.isEmpty()) ? null : items.get(0);
	}

	public List<T> getItems() {
		return new ArrayList<T>(items);
	}

	protected T readElement(IConfigurationElement element, MultiStatus result) {
		try {
			Object object = element.createExecutableExtension(getClassAttributeId());
			if (clazz.isInstance(object)) {
				return clazz.cast(object);
			} else {
				result.add(new Status(IStatus.ERROR, pluginId, NLS.bind(
						"Class ''{0}'' does not extend expected class for extension contributed by {1}", //$NON-NLS-1$
						object.getClass().getCanonicalName(), getPluginId())));
			}
		} catch (Throwable e) {
			result.add(new Status(IStatus.ERROR, pluginId, NLS.bind(
					"Failed to load for extension contributed by {0}", getPluginId()), e)); //$NON-NLS-1$
		}
		return null;
	}

}

Back to the top