Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c617b12c049deb4ceba433f98ce60ed93019ea58 (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
/*****************************************************************************
 * Copyright (c) 2012 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.toolsmiths.factory;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.papyrus.toolsmiths.Activator;
import org.eclipse.papyrus.toolsmiths.model.customizationplugin.CustomizationPluginPackage;

public class ExtensionFactoryRegistry {

	public static final String EXTENSION_ID = Activator.PLUGIN_ID + ".factory";

	public static ExtensionFactoryRegistry instance = new ExtensionFactoryRegistry();

	private final Map<EClass, ExtensionFactory> factories;

	private ExtensionFactoryRegistry() {
		factories = new HashMap<EClass, ExtensionFactory>();
		loadExtensionFactories();
	}

	private void loadExtensionFactories() {
		IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);

		for (IConfigurationElement e : config) {
			ExtensionFactory factory;
			try {
				factory = (ExtensionFactory) e.createExecutableExtension("factory"); //$NON-NLS-1$
				if (!CustomizationPluginPackage.eINSTANCE.getCustomizableElement().isSuperTypeOf(factory.getCustomizableElementClass())) {
					Activator.log.warn(String.format("The plugin %s contributed an invalid factory (%s).\nThe associated EClass must implement CustomizableElement", e.getContributor().getName(), e.getAttribute("factory")));
					continue;
				}

				factories.put(factory.getCustomizableElementClass(), factory);
			} catch (Exception ex) {
				Activator.log.warn(String.format("The plugin %s contributed an invalid factory (%s)", e.getContributor().getName(), e.getAttribute("factory")));
				Activator.log.error(ex);
			}
		}
	}

	public ExtensionFactory getFactory(EClass type) {
		return factories.get(type);
	}

	public void registerFactory(EClass forType, ExtensionFactory factory) {
		factories.put(forType, factory);
	}

	public Collection<? extends ExtensionFactory> getFactories() {
		return factories.values();
	}
}

Back to the top