Skip to main content



      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] Sun, 21 March 2010 20:41 Go to next message
Eclipse UserFriend
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] Sun, 21 March 2010 22:33 Go to previous messageGo to next message
Eclipse UserFriend
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 08:10 Go to previous messageGo to next message
Eclipse UserFriend
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 09:48 Go to previous messageGo to next message
Eclipse UserFriend
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 10:09 Go to previous messageGo to next message
Eclipse UserFriend
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 13:23 Go to previous message
Eclipse UserFriend
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: Sat Jul 05 10:45:36 EDT 2025

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

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

Back to the top