Skip to main content
summaryrefslogtreecommitdiffstats
blob: 16a884acbc3b4db91e4b55e987f54ffb34068e35 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 BMW Car IT, Technische Universitaet Muenchen, 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:
 *     Johannes Faltermeier - Initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.edapt.internal.migration;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.edapt.spi.migration.MigrationPlugin;
import org.osgi.framework.Bundle;

/**
 * Helper class for reading custom {@link EFactory EFactories} from the extension point.
 * 
 * @author jfaltermeier
 *
 */
public final class FactoryHelper {
	
	/**
	 * The instance.
	 */
	public static final FactoryHelper INSTANCE = new FactoryHelper();

	private static final String CLASS = "class";
	private static final String NS_URI = "nsURI";
	private static final String POINT_ID = "org.eclipse.emf.edapt.factories";

	private Map<String, Class<? extends EFactory>> nsURIToFactoryMap;

	private FactoryHelper() {
		nsURIToFactoryMap = new LinkedHashMap<String, Class<? extends EFactory>>();
		readExtensionPoint();
	}

	private void readExtensionPoint() {
		final IExtensionRegistry extensionRegistry = Platform
				.getExtensionRegistry();
		final IConfigurationElement[] configurationElements = extensionRegistry
				.getConfigurationElementsFor(POINT_ID);
		for (final IConfigurationElement configurationElement : configurationElements) {
			registerFactory(configurationElement);
		}
	}

	private void registerFactory(IConfigurationElement configurationElement) {
		try {
			String nsURI = configurationElement.getAttribute(NS_URI);
			String bundle = configurationElement.getContributor().getName();
			String className = configurationElement.getAttribute(CLASS);
			Class<? extends EFactory> clazz = loadClass(bundle, className);
			if (nsURI == null || clazz == null) {
				return;
			}
			nsURIToFactoryMap.put(nsURI, clazz);
		} catch (ClassNotFoundException e) {
			MigrationPlugin.INSTANCE.log(e);
		}

	}

	@SuppressWarnings("unchecked")
	private static Class<? extends EFactory> loadClass(String bundleName,
			String clazz) throws ClassNotFoundException {
		final Bundle bundle = Platform.getBundle(bundleName);
		if (bundle == null) {
			MigrationPlugin.INSTANCE.log("Could not get bundle " + bundleName + " from platform.");
		}
		return (Class<? extends EFactory>) bundle.loadClass(clazz);
	}

	/**
	 * Overrides the {@link EFactory} for the given {@link EPackage} if a custom EFactory 
	 * was registered for the factory's nsURI.
	 * 
	 * @param ePackage the package
	 */
	public void overrideFactory(EPackage ePackage) {
		try {
			if (!nsURIToFactoryMap.containsKey(ePackage.getNsURI())) {
				return;
			}
			Class<? extends EFactory> clazz = nsURIToFactoryMap.get(ePackage
					.getNsURI());
			EFactory eFactory = clazz.getConstructor().newInstance();
			ePackage.setEFactoryInstance(eFactory);
		} catch (InstantiationException e) {
			MigrationPlugin.INSTANCE.log(e);
		} catch (IllegalAccessException e) {
			MigrationPlugin.INSTANCE.log(e);
		} catch (IllegalArgumentException e) {
			MigrationPlugin.INSTANCE.log(e);
		} catch (InvocationTargetException e) {
			MigrationPlugin.INSTANCE.log(e);
		} catch (NoSuchMethodException e) {
			MigrationPlugin.INSTANCE.log(e);
		} catch (SecurityException e) {
			MigrationPlugin.INSTANCE.log(e);
		}
	}
}

Back to the top