Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb2fa65454eb7204c6db88de23a4858280339d97 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 * 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:
 *  Remi Schnekenburger (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.extendedtypes;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.gmf.runtime.emf.type.core.IContainerDescriptor;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelperAdvice;
import org.osgi.framework.Bundle;

/**
 * Registry that manages all possible pre/post action configurations
 */
public class AspectConfigurationFactoryRegistry {

	/** private singleton instance */
	private static AspectConfigurationFactoryRegistry registry;

	/** map configuration type to matcher descriptor */
	protected Map<String, ConfigurableClassDescriptor> configurationTypeToClassDescriptor = null;

	/**
	 * returns the singleton instance of this registry
	 *
	 * @return the singleton instance of this registry
	 */
	public static synchronized AspectConfigurationFactoryRegistry getInstance() {
		if (registry == null) {
			registry = new AspectConfigurationFactoryRegistry();
			registry.init();
		}
		return registry;
	}

	/**
	 * Inits the registry.
	 */
	protected void init() {
		configurationTypeToClassDescriptor = new HashMap<String, AspectConfigurationFactoryRegistry.ConfigurableClassDescriptor>();
		// read invariant rule configuration etension point
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(IAspectTypeExtensionPoint.EXTENSION_POINT_ID);
		// for each element, parses and retrieve the model file. then loads it and returns the root element
		for (IConfigurationElement configurationElement : elements) {
			// contributor will always be the same, but implementation could be different.
			String contributorName = configurationElement.getContributor().getName();

			String configurationClass = configurationElement.getAttribute(IAspectTypeExtensionPoint.CONFIGURATION_CLASS);

			String editHelperAdviceClassName = configurationElement.getAttribute(IAspectTypeExtensionPoint.EDIT_HELPER_ADVICE_CLASS);
			String containerDescriptorClassName = configurationElement.getAttribute(IAspectTypeExtensionPoint.CONTAINER_DESCRIPTOR_CLASS);
			String creationElementValidatorClassName = configurationElement.getAttribute(IAspectTypeExtensionPoint.CREATION_ELEMENT_VALIDATOR_CLASS);

			ConfigurableClassDescriptor configurableClassDescriptor = new ConfigurableClassDescriptor(contributorName, editHelperAdviceClassName, contributorName, containerDescriptorClassName, contributorName, creationElementValidatorClassName);
			configurationTypeToClassDescriptor.put(configurationClass, configurableClassDescriptor);
		}

	}

	/**
	 * @param ruleConfiguration
	 * @return
	 */
	public IContainerDescriptor createContainerDescriptor(ActionConfiguration ruleConfiguration) {
		Class<IActionContainerDescriptor<ActionConfiguration>> containerDescriptorClass = getContainerDescriptorClass(ruleConfiguration);
		if (containerDescriptorClass == null) {
			return null;
		}
		try {
			IActionContainerDescriptor<ActionConfiguration> containerDescriptor = containerDescriptorClass.newInstance();
			if (containerDescriptor != null) {
				containerDescriptor.init(ruleConfiguration);
			}
			return containerDescriptor;
		} catch (InstantiationException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		}
		return null;
	}

	/**
	 * @param actionConfiguration
	 * @return
	 */
	public IEditHelperAdvice createEditHelperAdvice(ActionConfiguration actionConfiguration) {
		Class<IActionEditHelperAdvice<ActionConfiguration>> editHelperAdviceClass = getEditHelperAdviceClass(actionConfiguration);
		if (editHelperAdviceClass == null) {
			Activator.log.error("impossible to find the edit helper advice implementation for configuration type : " + ((actionConfiguration != null) ? actionConfiguration.eClass().getName() : "null"), null);
			return null;
		}
		try {
			IActionEditHelperAdvice<ActionConfiguration> editHelperAdvice = editHelperAdviceClass.newInstance();
			if (editHelperAdvice != null) {
				editHelperAdvice.init(actionConfiguration);
			}
			return editHelperAdvice;
		} catch (InstantiationException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		}
		return null;
	}

	/**
	 * @param ruleConfiguration
	 * @return
	 */
	public ICreationElementValidator createCreationElementValidator(ActionConfiguration actionConfiguration) {
		Class<IActionCreationElementValidator<ActionConfiguration>> creationElementValidatorClass = getCreationElementValidatorClass(actionConfiguration);
		if (creationElementValidatorClass == null) {
			Activator.log.error("impossible to find the Creation Element Validator for configuration type : " + ((actionConfiguration != null) ? actionConfiguration.eClass().getName() : "null"), null);
			return null;
		}
		try {
			IActionCreationElementValidator<ActionConfiguration> creationElementValidator = creationElementValidatorClass.newInstance();
			if (creationElementValidator != null) {
				creationElementValidator.init(actionConfiguration);
			}
			return creationElementValidator;
		} catch (InstantiationException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		}
		return null;
	}


	/**
	 * @param ruleConfiguration
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Class<IActionCreationElementValidator<ActionConfiguration>> getCreationElementValidatorClass(ActionConfiguration configuration) {
		String configurationType = configuration.eClass().getInstanceClassName();
		String className = configurationTypeToClassDescriptor.get(configurationType).getCreationElementValidatorClassName();
		String contributorName = configurationTypeToClassDescriptor.get(configurationType).getCreationElementValidatorContributorName();
		// look in the list of registered matcher for the right one
		if (className == null) {
			Activator.log.error("There should be an implementation class for the configuration " + configurationType + " from contributor " + contributorName, null);
		} else if (contributorName != null) {
			return (Class<IActionCreationElementValidator<ActionConfiguration>>) loadClass(className, contributorName);
		}
		return null;
	}

	/**
	 * @param configurationType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Class<IActionEditHelperAdvice<ActionConfiguration>> getEditHelperAdviceClass(ActionConfiguration configuration) {
		String configurationType = configuration.eClass().getInstanceClassName();
		String className = configurationTypeToClassDescriptor.get(configurationType).getEditHelperAdviceClassName();
		String contributorName = configurationTypeToClassDescriptor.get(configurationType).getEditHelperAdviceContributorName();

		// look in the list of registered edit helper advices for the right one
		return (Class<IActionEditHelperAdvice<ActionConfiguration>>) loadClass(className, contributorName);
	}

	/**
	 * @param configurationType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Class<IActionContainerDescriptor<ActionConfiguration>> getContainerDescriptorClass(ActionConfiguration configuration) {
		String configurationType = configuration.eClass().getInstanceClassName();
		String className = configurationTypeToClassDescriptor.get(configurationType).getContainerDescriptorClassName();
		String contributorName = configurationTypeToClassDescriptor.get(configurationType).getContainerDescriptorContributorName();


		// look in the list of registered edit helper advices for the right one
		if (className != null && contributorName != null) {
			return (Class<IActionContainerDescriptor<ActionConfiguration>>) loadClass(className, contributorName);
		}
		return null;
	}

	protected static class ConfigurableClassDescriptor {

		/**
		 * @param editHelperAdviceContributorName
		 * @param editHelperAdviceClassName
		 * @param containerDescriptorContributorName
		 * @param containerDescriptorClassName
		 */
		public ConfigurableClassDescriptor(String editHelperAdviceContributorName, String editHelperAdviceClassName, String containerDescriptorContributorName, String containerDescriptorClassName, String creationElementValidatorContributorName,
				String creationElementValidatorClassName) {
			this.editHelperAdviceContributorName = editHelperAdviceContributorName;
			this.editHelperAdviceClassName = editHelperAdviceClassName;
			this.containerDescriptorContributorName = containerDescriptorContributorName;
			this.containerDescriptorClassName = containerDescriptorClassName;
			this.creationElementValidatorContributorName = creationElementValidatorContributorName;
			this.creationElementValidatorClassName = creationElementValidatorClassName;
		}

		private final String editHelperAdviceContributorName;

		private final String editHelperAdviceClassName;

		private final String containerDescriptorContributorName;

		private final String containerDescriptorClassName;

		private final String creationElementValidatorContributorName;

		private final String creationElementValidatorClassName;

		/**
		 * @return
		 */
		public String getCreationElementValidatorContributorName() {
			return creationElementValidatorContributorName;
		}

		/**
		 * @return
		 */
		public String getCreationElementValidatorClassName() {
			return creationElementValidatorClassName;
		}

		/**
		 * @return the editHelperAdviceContributorName
		 */
		public String getEditHelperAdviceContributorName() {
			return editHelperAdviceContributorName;
		}


		/**
		 * @return the editHelperAdviceClassName
		 */
		public String getEditHelperAdviceClassName() {
			return editHelperAdviceClassName;
		}


		/**
		 * @return the containerDescriptorContributorName
		 */
		public String getContainerDescriptorContributorName() {
			return containerDescriptorContributorName;
		}


		/**
		 * @return the containerDescriptorClassName
		 */
		public String getContainerDescriptorClassName() {
			return containerDescriptorClassName;
		}

	}

	// /////////////////////////////////////////////////////////////////////////
	// loading resource
	// /////////////////////////////////////////////////////////////////////////
	/** A map of classes that have been successfully loaded, keyed on the class name optionally prepended by the plugin ID, if specified. */
	private static Map<String, WeakReference<Class<?>>> successLookupTable = new HashMap<String, WeakReference<Class<?>>>();

	/** A map of classes that could not be loaded, keyed on the class name, optionally prepended by the plugin ID if specified. */
	private static Set<String> failureLookupTable = new HashSet<String>();

	/** A map to hold the bundle to exception list */
	private static Map<Bundle, Set<String>> bundleToExceptionsSetMap = new HashMap<Bundle, Set<String>>();

	/**
	 * A utility method to load a class using its name and a given class loader.
	 *
	 * @param className
	 *            The class name
	 * @param bundle
	 *            The class loader
	 * @return The loaded class or <code>null</code> if could not be loaded
	 */
	protected static Class<?> loadClass(String className, String pluginId) {
		StringBuffer keyStringBuf = new StringBuffer(className.length() + pluginId.length() + 2); // 2 is for . and extra.
		keyStringBuf.append(pluginId);
		keyStringBuf.append('.');
		keyStringBuf.append(className);
		String keyString = keyStringBuf.toString();
		WeakReference<Class<?>> ref = successLookupTable.get(keyString);
		Class<?> found = (ref != null) ? ref.get() : null;
		if (found == null) {
			if (ref != null) {
				successLookupTable.remove(keyString);
			}
			if (!failureLookupTable.contains(keyString)) {
				try {
					Bundle bundle = basicGetPluginBundle(pluginId);
					if (bundle != null) {
						// never load the class if the bundle is not active other wise
						// we will cause the plugin to load
						// unless the class is in the exception list
						int state = bundle.getState();
						if (state == org.osgi.framework.Bundle.ACTIVE || isInExceptionList(bundle, className)) {
							found = bundle.loadClass(className);
							successLookupTable.put(keyString, new WeakReference<Class<?>>(found));
							if (state == org.osgi.framework.Bundle.ACTIVE) {
								bundleToExceptionsSetMap.remove(bundle);
							}
						}
					} else {
						failureLookupTable.add(keyString);
					}
				} catch (ClassNotFoundException e) {
					failureLookupTable.add(keyString);
				}
			}
		}
		return found;
	}

	/**
	 * Given a bundle id, it checks if the bundle is found and activated. If it
	 * is, the method returns the bundle, otherwise it returns <code>null</code>.
	 *
	 * @param pluginId
	 *            the bundle ID
	 * @return the bundle, if found
	 */
	protected static Bundle getPluginBundle(String pluginId) {
		Bundle bundle = basicGetPluginBundle(pluginId);
		if (null != bundle && bundle.getState() == org.osgi.framework.Bundle.ACTIVE) {
			return bundle;
		}
		return null;
	}

	private static Bundle basicGetPluginBundle(String pluginId) {
		return Platform.getBundle(pluginId);
	}

	private static boolean isInExceptionList(Bundle bundle, String className) {
		String packageName = className.substring(0, className.lastIndexOf('.'));
		Set<String> exceptionSet = bundleToExceptionsSetMap.get(bundle);
		if (exceptionSet == null) {
			Dictionary<String, String> dict = bundle.getHeaders();
			String value = dict.get("Eclipse-LazyStart"); //$NON-NLS-1$
			if (value != null) {
				int index = value.indexOf("exceptions"); //$NON-NLS-1$
				if (index != -1) {
					try {
						int start = value.indexOf('"', index + 1);
						int end = value.indexOf('"', start + 1);
						String exceptions = value.substring(start + 1, end);
						exceptionSet = new HashSet<String>(2);
						StringTokenizer tokenizer = new StringTokenizer(exceptions, ","); //$NON-NLS-1$
						while (tokenizer.hasMoreTokens()) {
							exceptionSet.add(tokenizer.nextToken().trim());
						}
					} catch (IndexOutOfBoundsException exception) {
						// this means the MF did not follow the documented format for the exceptions list so i'll consider it empty
						exceptionSet = Collections.emptySet();
					}
				} else {
					exceptionSet = Collections.emptySet();
				}
			} else {
				exceptionSet = Collections.emptySet();
			}
			bundleToExceptionsSetMap.put(bundle, exceptionSet);
		}
		return exceptionSet.contains(packageName);
	}

}

Back to the top