Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » getBundleContext() isn't returning Bundle's context classloader
icon9.gif  getBundleContext() isn't returning Bundle's context classloader [message #522272] Mon, 22 March 2010 00:41 Go to next message
Davi Baldin is currently offline Davi BaldinFriend
Messages: 4
Registered: March 2010
Junior Member
Hello all,

I'm running Equinox servlet bridge and I'm getting trouble to retrieve the Bundle's classloader in order to use java util ResourceBundle. The code which is trying to retrieve the Bundle's classloader is running inside a Servlet registered into the OSGi HttpService.

Here is the case:

a) Bundle has Messages.properties files for my locales;
b) Bundle's activator is getting the ResourceBundle correctly;
c) getBundleContext().getClass().getClassLoader() is not able to provide me with the correct classloaded (Bundle's classloader) is order to use the Messages.properties from another bundle.

Activator code inside start()

ResourceBundle b = ResourceBundle.getBundle("Messages");
System.out.println("s " + this.getClass().getClassLoader());

Result: s org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@20c906

[B]Servlet code/B]

long bundleId = 27; //Bundle with Message.properties
Bundle b = Activator.getBundleContext().getBundle(bundleId);
System.out.println("x " + b.getBundleContext().getClass());

Result: x org.eclipse.equinox.servletbridge.FrameworkLauncher$ChildFirstURLClassLoader@94af2f

Requirement

I need to retrieve the Bundle's class loader in order to use a ResourceBundle by searching for files in the Bundle's class loader like:

ResourceBundle.getBundle("Messages", locale, b.getBundleContext().getClass().getClassLoader());

How to achieve this or how to retrieve correctly the Bundle's class loader?

Thanks,

Davi

Re: getBundleContext() isn't returning Bundle's context classloader [message #522280 is a reply to message #522272] Mon, 22 March 2010 02:33 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 437
Registered: July 2009
Senior Member
getBundleContext().getClass().getClassLoader() will get you the class loader of the BundleContext interface class (which is loaded by the class loader which loaded the equinox framework) not your bundle's class loader.

To get what you want you will need wrapper a class loader on top of the Bundle object with something like this http://wiki.eclipse.org/BundleProxyClassLoader_recipe

HTH

Tom.
icon14.gif  Re: getBundleContext() isn't returning Bundle's context classloader [message #522625 is a reply to message #522280] Tue, 23 March 2010 12:10 Go to previous messageGo to next message
Davi Baldin is currently offline Davi BaldinFriend
Messages: 4
Registered: March 2010
Junior Member
YES! - Worked like a charme....

Thank you Thomas.

Regards,

Davi
Re: getBundleContext() isn't returning Bundle's context classloader [message #1103211 is a reply to message #522272] Fri, 06 September 2013 13:48 Go to previous messageGo to next message
Alan DW is currently offline Alan DWFriend
Messages: 108
Registered: March 2012
Senior Member
Hi,

I know that this is kind of a necro-post, but I just wanted to thank Thomas Watson for the link, he just saved me one hell of a lot of trouble - it works like a charm with the BundleProxyClassLoader. Thanks a lot, Thomas!


Alan
Re: getBundleContext() isn't returning Bundle's context classloader [message #1103228 is a reply to message #1103211] Fri, 06 September 2013 14:09 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 437
Registered: July 2009
Senior Member
Thanks! It is actually good that you posted. A better way with the latest R5 OSGi APIs is to get the bundle class loader directly. No real need to create BundleProxyClassLoader ...

ClassLoader bundleCL = bundle.adapt(BundleWiring.class).getClassLoader();

Keep in mind, if the bundle is not resolved then adapting it to org.osgi.framework.wiring.BundleWiring will return null.

HTH

Tom.
Re: getBundleContext() isn't returning Bundle's context classloader [message #1103369 is a reply to message #1103228] Fri, 06 September 2013 17:23 Go to previous message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 55
Registered: July 2009
Member
Tom, maybe you should update the wiki page containing the recipe with the new R4.3 BundleWiring method?
Previous Topic:How to set the maximum threads of a SimpleArtifactRepository
Next Topic:ServiceTracker and DS combined?
Goto Forum:
  


Current Time: Sun Nov 23 10:14:05 GMT 2014

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

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