|
Re: Reading resources from a Eclipse plugin ? [message #906072 is a reply to message #906001] |
Fri, 31 August 2012 15:07 |
|
Use the sample below as something to build off of. To get a resource, first retrieve the bundle using the getBundleForName() and then call bundle.getEntry() to retrieve the resource within the bundle. Note that getEntry() will only search the specified bundle, which will return a platform URL to the resource.
package braintrader.workbench.internal.common;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
@SuppressWarnings({"deprecation", "rawtypes", "unchecked"})
public class WorkbenchCommonActivator implements BundleActivator {
// Change to match your bundle ID
public static final String PLUGIN_ID = "braintrader.workbench.common"; //$NON-NLS-1$
private static WorkbenchCommonActivator defaultInstance;
private BundleContext context;
private ServiceTracker locationTracker;
private ServiceTracker pkgAdminTracker;
/**
* Get the default activator.
*
* @return a BundleActivator
*/
public static WorkbenchCommonActivator getDefault() {
return defaultInstance;
}
public WorkbenchCommonActivator() {
defaultInstance = this;
}
/**
* @return the bundle object
*/
public Bundle getBundle() {
return context.getBundle();
}
/**
* @return the PackageAdmin service from this bundle
*/
public PackageAdmin getBundleAdmin() {
if (pkgAdminTracker == null) {
if (context == null)
return null;
pkgAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
pkgAdminTracker.open();
}
return (PackageAdmin) pkgAdminTracker.getService();
}
/**
* @param bundleName
* the bundle id
* @return A bundle if found, or <code>null</code>
*/
public Bundle getBundleForName(String bundleName) {
Bundle[] bundles = getBundleAdmin().getBundles(bundleName, null);
if (bundles == null)
return null;
// Return the first bundle that is not installed or uninstalled
for (int i = 0; i < bundles.length; i++) {
if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundles[i];
}
}
return null;
}
/**
* NOTE: This method is marked as public only because the class is located in
* an *.internal.* package within the bundle and is not shared outside of the bundle
* If you want to make this Activator available from any bundle, change the visibility
* of this method to /* package */
*
* @return this bundles context
*/
public BundleContext getContext() {
return context;
}
/**
* @return the instance Location service
*/
public Location getInstanceLocation() {
if (locationTracker == null) {
Filter filter = null;
try {
filter = context.createFilter(Location.INSTANCE_FILTER);
} catch (InvalidSyntaxException e) {
// ignore this. It should never happen as we have tested the
// above format.
}
locationTracker = new ServiceTracker(context, filter, null);
locationTracker.open();
}
return (Location) locationTracker.getService();
}
public void start(BundleContext context) throws Exception {
this.context = context;
}
public void stop(BundleContext context) throws Exception {
if (pkgAdminTracker != null) {
pkgAdminTracker.close();
pkgAdminTracker = null;
}
if (locationTracker != null) {
locationTracker.close();
locationTracker = null;
}
this.context = null;
}
}
JD
|
|
|
|
Powered by
FUDForum. Page generated in 0.04124 seconds