Skip to main content



      Home
Home » Eclipse Projects » Equinox » Using Class.forName or Bundle.loadClass in bundles - whats the difference?
Using Class.forName or Bundle.loadClass in bundles - whats the difference? [message #95140] Fri, 17 August 2007 11:36 Go to next message
Eclipse UserFriend
Originally posted by: cfraser.levelfour.com

Hi,

I've been trying to find out exactly how the class loading works within Equinox 3.3.

I've inherited some bundle code from another developer that uses a mixture of Class.forName and other parts use Bundle.loadClass and I'm trying to figure out what the differences are.

I've been reading around but can't seem to find a clear statement of the differences. Is it valid to use Class.forName inside a bundle or is that defeating the point of OSGi?

Any help apreciated.

Colin
Re: Using Class.forName or Bundle.loadClass in bundles - whats the difference? [message #95188 is a reply to message #95140] Fri, 17 August 2007 17:24 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alex_blewitt.yahoo.com

Yes, it's valid to use Class.forName inside an OSGi bundle. You'd do that if you have something that you want to determine at runtime (e.g. a database driver) instead of at compile-time.

However, for Class.forName() to work, you'd have to ensure that your bundle imports the necessary package alreday, because otherwise it wouldn't be found.

Using Bundle.loadClass, you can peek at classes in other bundles. This is generally a bad idea. One reason you might want to do it is to ensure that the bundle, if it has Eclipse-AutoStart/LazyStart/Bundle-ActivationPolicy will result in the bundle being started.

Classes loaded by Bundle.loadClass may not be usable in the class package, so generally should be avoided.

Alex.
Re: Using Class.forName or Bundle.loadClass in bundles - whats the difference? [message #95383 is a reply to message #95188] Mon, 20 August 2007 14:53 Go to previous messageGo to next message
Eclipse UserFriend
> Classes loaded by Bundle.loadClass may not be usable in the class package, so generally should be avoided.
>

I'm not sure what Alex means by the class may not be usable in the class
package ...

There are differences between Class.forName and Bundle.loadClass.
Class.forName loads a class from the perspective of the classloader
which loaded the calling Class. Bundle.loadClass loads a class from the
perspective of the classloader of the Bundle object itself.

Typically you use Buddle.loadClass in extender type architectures. For
example, the extension registry implementation is considered an extender
architecture because bundles contribute to the registry but the registry
has no classloading links at runtime to the bundles contributing to the
registry. Instead the registry knows what bundles contribute and uses
the Bundle objects to load classes from individual bundles to create
executable extension objects.

Other examples of extender models where Bundle.loadClass is used are
Declarative Services and Spring-OSGi. In all these cases the extender
model knows what bundles contribute and can obtain a proper Bundle
object to do the loadClass.

Class.forName has many use cases also. A common one is to check if some
optional package is available for some advanced functionality. For
example, the java.nio package. If you have code that is supported on
J2SE 1.3 but you would like to use java.nio for more advanced things if
running on J2SE 1.4 then you can use Class.forName first to make sure
the package is available.

There many other usecases are that use Class.forName in an SPI
architecture. Typically you see this in legacy code that was developed
to run outside of OSGi. For example, configuring an XML parser in JAXP,
or using the hibernate library. Unfortunately these types of designs
have issues in OSGi because the bundles which include this type of code
must have explicit dependencies to have access to packages from other
bundles. Or they can use the DynamicImport-Package header from OSGi or
the buddy classloading mechanism provided by the Equinox runtime.

HTH

Tom
Re: Using Class.forName or Bundle.loadClass in bundles - whats the difference? [message #95664 is a reply to message #95140] Wed, 22 August 2007 10:49 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cfraser.levelfour.com

Thanks for the information.

So to clarify if I understand how these will work:

If I call Bundle.loadClass it will ONLY try and load a class from the specific bundle - it won't go looking on the class path or try using any of the delegation classloaders.

If I call Class.forName it will first look in the bundle, then any delegation classloaders making up the OSGi container (using the standard OSGi class load mechanisms).

Do I have that correct?

Thanks in advance

Colin
Re: Using Class.forName or Bundle.loadClass in bundles - whats the difference? [message #95678 is a reply to message #95664] Wed, 22 August 2007 11:42 Go to previous message
Eclipse UserFriend
Hi, Colin

The class will be loaded as the request came directly from the bundle,
which means that everything will be searched for the class: imports,
requires, fragments and the bundle itself.

Danail

Colin Fraser wrote:
> Thanks for the information.
>
> So to clarify if I understand how these will work:
>
> If I call Bundle.loadClass it will ONLY try and load a class from the specific bundle - it won't go looking on the class path or try using any of the delegation classloaders.
>
> If I call Class.forName it will first look in the bundle, then any delegation classloaders making up the OSGi container (using the standard OSGi class load mechanisms).
>
> Do I have that correct?
>
> Thanks in advance
>
> Colin
Previous Topic:IncompatibleClassChangeError accessing EJBs resided in different EAR through local interface from HT
Next Topic:Problem with Tomcat looping when using BridgeServlet with the console option on
Goto Forum:
  


Current Time: Mon May 12 14:46:03 EDT 2025

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

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

Back to the top