-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginUtil.java
75 lines (68 loc) · 2.59 KB
/
PluginUtil.java
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
/*******************************************************************************
* Copyright (c) 2016 RCP Vision (http://www.rcp-vision.com) 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:
* Lorenzo Bettini - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.parsley.runtime.ui;
import java.lang.reflect.Method;
import java.util.Dictionary;
import org.eclipse.emf.parsley.runtime.exception.PluginConfigurationException;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkUtil;
/**
* Utility methods for plug-ins.
*
* @author Lorenzo Bettini - initial API and implementation
*
*/
public class PluginUtil {
protected PluginUtil() {
}
public static Bundle getBundle(Class<?> classFromBundle) {
return FrameworkUtil.getBundle(classFromBundle);
}
/**
* Retrieves the {@link AbstractUIPlugin} activator from the specified
* {@link Bundle}, via reflection, assuming that the activator has a static
* method getDefault returning the singleton instance. If the specified
* {@link Bundle} is null, then it simply returns null.
*
* @param bundle
* @return
* @throws PluginConfigurationException
* if bundle is not found or if it doesn't contain the
* getDefault method
*/
public static AbstractUIPlugin getPlugin(Bundle bundle) {
if (bundle == null) {
return null;
}
final Dictionary<String, String> headers = bundle.getHeaders();
String activator = headers.get(Constants.BUNDLE_ACTIVATOR);
Class<?> activatorClass;
Object activatorInstance;
try {
activatorClass = bundle.loadClass(activator);
try {
Method method = activatorClass.getMethod("getDefault");
activatorInstance = method.invoke(null);
} catch (NoSuchMethodException e) {
throw new PluginConfigurationException(
"Cannot find getDefault method in activator of bundle " + bundle.getBundleId(), e);
} catch (Exception e) {
throw new PluginConfigurationException(
"Error accessing getDefault method in activator of bundle " + bundle.getBundleId(), e);
}
return (AbstractUIPlugin) activatorInstance;
} catch (ClassNotFoundException e) {
throw new PluginConfigurationException("Cannot find activator for bundle " + bundle.getBundleId(), e);
}
}
}