diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java new file mode 100644 index 00000000..29705968 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java | |||
@@ -0,0 +1,62 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Zoltan Ujhelyi, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.extensibility; | ||
10 | |||
11 | import java.lang.reflect.Method; | ||
12 | |||
13 | import org.eclipse.core.runtime.CoreException; | ||
14 | import org.eclipse.core.runtime.IConfigurationElement; | ||
15 | import org.eclipse.core.runtime.IExecutableExtension; | ||
16 | import org.eclipse.core.runtime.IExecutableExtensionFactory; | ||
17 | import org.eclipse.core.runtime.IStatus; | ||
18 | import org.eclipse.core.runtime.Platform; | ||
19 | import org.eclipse.core.runtime.Status; | ||
20 | import org.osgi.framework.Bundle; | ||
21 | |||
22 | /** | ||
23 | * Factory to register a static singleton instance in an extension point. | ||
24 | * | ||
25 | * @author Zoltan Ujhelyi | ||
26 | * | ||
27 | */ | ||
28 | public class SingletonExtensionFactory implements IExecutableExtension, IExecutableExtensionFactory { | ||
29 | |||
30 | private String clazzName; | ||
31 | private Bundle bundle; | ||
32 | |||
33 | protected Bundle getBundle() { | ||
34 | return bundle; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public Object create() throws CoreException { | ||
39 | try { | ||
40 | final Class<?> clazz = bundle.loadClass(clazzName); | ||
41 | Method method = clazz.getMethod("instance"); | ||
42 | return method.invoke(null); | ||
43 | } catch (Exception e) { | ||
44 | throw new CoreException(new Status(IStatus.ERROR, bundle.getSymbolicName(), "Error loading group " | ||
45 | + clazzName, e)); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public void setInitializationData(IConfigurationElement config, String propertyName, Object data) | ||
51 | throws CoreException { | ||
52 | String id = config.getContributor().getName(); | ||
53 | bundle = Platform.getBundle(id); | ||
54 | if (data instanceof String) { | ||
55 | clazzName = (String) data; | ||
56 | } else { | ||
57 | throw new CoreException(new Status(IStatus.ERROR, bundle.getSymbolicName(), | ||
58 | "Unsupported extension initialization data: " + data)); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | } | ||