aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/extensibility/SingletonExtensionFactory.java
blob: 29705968e01182499b89f7c8403ec441c8a4b372 (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
/*******************************************************************************
 * Copyright (c) 2010-2014, Zoltan Ujhelyi, Istvan Rath and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.extensibility;

import java.lang.reflect.Method;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IExecutableExtensionFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Bundle;

/**
 * Factory to register a static singleton instance in an extension point. 
 * 
 * @author Zoltan Ujhelyi
 * 
 */
public class SingletonExtensionFactory implements IExecutableExtension, IExecutableExtensionFactory {

    private String clazzName;
    private Bundle bundle;

    protected Bundle getBundle() {
        return bundle;
    }
    
    @Override
    public Object create() throws CoreException {
        try {
            final Class<?> clazz = bundle.loadClass(clazzName);
            Method method = clazz.getMethod("instance");
            return method.invoke(null);
        } catch (Exception e) {
            throw new CoreException(new Status(IStatus.ERROR, bundle.getSymbolicName(), "Error loading group "
                    + clazzName, e));
        }
    }

    @Override
    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
            throws CoreException {
        String id = config.getContributor().getName();
        bundle = Platform.getBundle(id);
        if (data instanceof String) {
            clazzName = (String) data;
        } else {
            throw new CoreException(new Status(IStatus.ERROR, bundle.getSymbolicName(),
                    "Unsupported extension initialization data: " + data));
        }
    }

}