Skip to main content
summaryrefslogtreecommitdiffstats
blob: 74136b11ffe4d1df5dd94d87c4a0412ed638e96a (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*******************************************************************************
 * Copyright (c) 2012, 2016 Obeo 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:
 *     Obeo - initial API and implementation
 *     Martin Fleck - bug 483798
 *******************************************************************************/
package org.eclipse.emf.compare.rcp.extension;

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.ILog;
import org.eclipse.core.runtime.IRegistryEventListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.compare.rcp.internal.EMFCompareRCPMessages;

/**
 * Abstract utility class to listen to the {@link IExtensionRegistry}. It provides base implementation to
 * {@link #added(IExtension[])} and {@link #removed(IExtension[])}. Users must implement
 * {@link #readElement(IConfigurationElement, Action)} according to their extension schema.
 * <p>
 * The helper method {@link #readRegistry()} is browsing already registered extension to the extension
 * registry making it easy to read the registry after having added the listener to it.
 * <p>
 * {@link #readRegistry()} is delegating to {@link #readElement(IConfigurationElement, Action)}.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public abstract class AbstractRegistryEventListener implements IRegistryEventListener {

	/**
	 * Enumeration that says if the {@link IConfigurationElement} is to be added or removed from the
	 * {@link IExtensionRegistry}.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	protected enum Action {
		/**
		 * The {@link IConfigurationElement} is to be added.
		 */
		ADD,
		/**
		 * The {@link IConfigurationElement} is to be removed.
		 */
		REMOVE
	}

	/**
	 * The namespace of the extension point to be monitored.
	 */
	private final String namespace;

	/**
	 * The extension point ID to be monitored.
	 */
	private final String extensionPointID;

	/** The log in which to report failures from this listener. */
	private final ILog log;

	/**
	 * Creates a new registry event listener.
	 * 
	 * @param namespace
	 *            The namespace of the extension point to be monitored.
	 * @param extensionPointID
	 *            The extension point ID to be monitored
	 * @param log
	 *            The log object to be used to log error and/or warning.
	 */
	public AbstractRegistryEventListener(String namespace, String extensionPointID, ILog log) {
		this.namespace = namespace;
		this.extensionPointID = extensionPointID;
		this.log = log;
	}

	/**
	 * Reads the given registry and call {@link #readElement(IConfigurationElement, Action)} as the read
	 * {@link IConfigurationElement} were just added to it.
	 * 
	 * @param extensionRegistry
	 *            the registry to read.
	 */
	public void readRegistry(IExtensionRegistry extensionRegistry) {
		IExtensionPoint point = extensionRegistry.getExtensionPoint(namespace, extensionPointID);
		if (point != null) {
			IConfigurationElement[] elements = point.getConfigurationElements();
			for (int i = 0; i < elements.length; i++) {
				internalReadElement(elements[i], Action.ADD);
			}
		}
	}

	/**
	 * Reads the given {@code element}. This method can be call when the element is added or remove. The
	 * {@code action} reflect it. It returns true if the element is recognized as valid regarding the
	 * monitored extension point. It will be call on sub elements recursively for all recognized ones.
	 * 
	 * @param element
	 *            the element to be read.
	 * @param action
	 *            is the element added or removed.
	 * @return true if the element is recognized as valid regarding the monitored extension point.
	 */
	protected boolean readElement(IConfigurationElement element, Action action) {
		final boolean ret;
		if (validateExtensionElement(element)) {
			switch (action) {
				case ADD:
					ret = addedValid(element);
					break;
				case REMOVE:
					ret = removedValid(element);
					break;
				default:
					ret = false;
					break;
			}
		} else {
			ret = false;
		}
		return ret;
	}

	/**
	 * Validates if the given element is an element for the given extension and is well constructed. Returns
	 * true if the element should be further parsed for addition or removal.
	 * 
	 * @param element
	 *            the element to validate.
	 * @return true if the element should be further parsed for addition or removal, else otherwise.
	 */
	protected abstract boolean validateExtensionElement(IConfigurationElement element);

	/**
	 * Process the given element as the addition of a valid element extension.
	 * 
	 * @param element
	 *            the element to be added.
	 * @return true if the given element has been added and if its children should be processed, false
	 *         otherwise.
	 */
	protected abstract boolean addedValid(IConfigurationElement element);

	/**
	 * Process the given element as the removal of a valid element extension.
	 * 
	 * @param element
	 *            the element to be removed.
	 * @return true if the given element has been removed and if its children should be processed, false
	 *         otherwise.
	 */
	protected abstract boolean removedValid(IConfigurationElement element);

	/**
	 * Reads the given element and, if recognized, browse recursively the children and try to read it.
	 * 
	 * @param element
	 *            the element to be read.
	 * @param action
	 *            is the element added or removed.
	 */
	private void internalReadElement(IConfigurationElement element, Action action) {
		boolean recognized = readElement(element, action);
		if (recognized) {
			IConfigurationElement[] children = element.getChildren();
			for (int i = 0; i < children.length; ++i) {
				internalReadElement(children[i], action);
			}
		}
	}

	/**
	 * Delegates the logging of a missing attribute to {@link #log(IConfigurationElement, String)} with a
	 * proper message.
	 * 
	 * @param element
	 *            the element to which an attribute is missing.
	 * @param attributeName
	 *            the name of the missing attribute.
	 */
	protected void logMissingAttribute(IConfigurationElement element, String attributeName) {
		log(IStatus.ERROR, element, EMFCompareRCPMessages.getString("missing.extension.attribute", //$NON-NLS-1$
				attributeName));
	}

	/**
	 * Log the error to the current plugin logger.
	 * 
	 * @param severity
	 *            Severity of this message. One of <code>IStatus.OK</code>, <code>IStatus.ERROR</code>,
	 *            <code>IStatus.INFO</code>, <code>IStatus.WARNING</code>, or <code>IStatus.CANCEL</code>
	 * @param element
	 *            the element from which comes to the error.
	 * @param message
	 *            the message to be logged.
	 */
	protected void log(int severity, IConfigurationElement element, String message) {
		log.log(new Status(severity, element.getDeclaringExtension().getContributor().getName(), message));
	}

	/**
	 * Log the error to the current plugin logger.
	 * 
	 * @param element
	 *            the element from which comes to the error.
	 * @param t
	 *            the exception to be logged.
	 */
	protected void log(IConfigurationElement element, Throwable t) {
		log.log(new Status(IStatus.ERROR, element.getDeclaringExtension().getContributor().getName(),
				t.getMessage(), t));
	}

	/**
	 * Logs the given error with a human-readable error message.
	 * 
	 * @param element
	 *            The element from which originates the error.
	 * @param errorMessage
	 *            Human-readable cause of this exception.
	 * @param cause
	 *            Actual exception that is to be logged.
	 * @since 2.2
	 */
	protected void log(IConfigurationElement element, String errorMessage, Throwable cause) {
		log.log(new Status(IStatus.ERROR, element.getDeclaringExtension().getContributor().getName(),
				errorMessage, cause));
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.runtime.IRegistryEventListener#added(org.eclipse.core.runtime.IExtension[])
	 */
	public void added(IExtension[] extensions) {
		for (IExtension extension : extensions) {
			if (extension.getExtensionPointUniqueIdentifier().equals(extensionPointID)) {
				IConfigurationElement[] configurationElement = extension.getConfigurationElements();
				for (int j = 0; j < configurationElement.length; ++j) {
					internalReadElement(configurationElement[j], Action.ADD);
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.runtime.IRegistryEventListener#removed(org.eclipse.core.runtime.IExtension[])
	 */
	public void removed(IExtension[] extensions) {
		for (IExtension extension : extensions) {
			if (extension.getExtensionPointUniqueIdentifier().equals(extensionPointID)) {
				IConfigurationElement[] configurationElement = extension.getConfigurationElements();
				for (int j = 0; j < configurationElement.length; ++j) {
					internalReadElement(configurationElement[j], Action.REMOVE);
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.runtime.IRegistryEventListener#added(org.eclipse.core.runtime.IExtensionPoint[])
	 */
	public void added(IExtensionPoint[] extensionPoints) {
		// no need to listen to this.
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.runtime.IRegistryEventListener#removed(org.eclipse.core.runtime.IExtensionPoint[])
	 */
	public void removed(IExtensionPoint[] extensionPoints) {
		// no need to listen to this.
	}
}

Back to the top