tags:

views:

277

answers:

2

I have a 3rd party JAR that I have converted to an OSGI bundle using bnd. The code I need to call to use it from my own bundle looks something like this:

ThirdParty.setRegKey(myRegKey);
ThirdParty thirdParty = new ThirdParty();
thirdParty.callMethod();

What seems to be causing me problems is the first line - the static method call. Outside of the OSGI container, using the standard JAR, this works fine. Inside OSGI, however, I get an error at line 3 stating that no registration key has been set.

Are there any issues with static method calls across bundles like this? It's almost as if the static context is not being shared across the bundles.

+2  A: 

You may need to export / import the package containing the ThirdParty class. Otherwise it ends up in different class loaders for different bundles, so it is indeed not shared.

The true OSGi way would be to do this via some service.

starblue
Thanks for the clarification. In the 3rd party bundle manifest I have Export-Package: com.thirdparty.package and then in the manifest of my bundle, Import-Package: com.thirdparty.package. Is this what you mean?
William
The sample code suggests that the MANIFEST imports and exports are correct or he wouldn't be able to compile, and the stated problem is a runtime failure on the 3rd line.
hbunny
@William Yes. Though, if your code is all within one bundle this shouldn't be the problem.
starblue
+2  A: 

Have you debugged the code? Since you don't tell us which 3rd-party library you are working with and we don't know your full environment, it could be the setRegKey() or callMethod() are trying to do something 'smart' which doesn't work inside OSGi.

The JDBC driver manager has this problem where your bundle imports the JDBC driver class but the driver manager decides that the calling thread from your bundle shouldn't 'see' the JDBC driver class, so it fails when run inside OSGi.

hbunny