Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Reading resources from a Eclipse plugin ?
Reading resources from a Eclipse plugin ? [message #906001] Fri, 31 August 2012 12:41 Go to next message
Kosala Yapa is currently offline Kosala YapaFriend
Messages: 159
Registered: September 2010
Senior Member
Hi All,

I am trying to read model.ecore at model folder in plugin "com.kosala.model".

Here is my code:

Bundle bundle = Platform.getBundle("com.kosala.model");
URL fileURL = bundle.getEntry("model/model.ecore");
File file = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}


This gives an error:

Exception in thread "main" java.lang.NullPointerException.

bundle gets null.

Anyone has a clue to slove this issue?

Thanks in advance
K
Re: Reading resources from a Eclipse plugin ? [message #906072 is a reply to message #906001] Fri, 31 August 2012 15:07 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph CarrollFriend
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

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
Re: Reading resources from a Eclipse plugin ? [message #907113 is a reply to message #906072] Mon, 03 September 2012 10:13 Go to previous message
Kosala Yapa is currently offline Kosala YapaFriend
Messages: 159
Registered: September 2010
Senior Member
JD,

Thanks a mil for this helpful information.

KY
Previous Topic:File Path to jar inside another jar
Next Topic:Editor type based on document location
Goto Forum:
  


Current Time: Thu Dec 18 11:40:43 GMT 2014

Powered by FUDForum. Page generated in 0.01658 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software