NPE with Openstack SDK in OSGi

If you use the Openstack SDK library, you may have encounter this exception.

java.lang.NullPointerException
at com.woorea.openstack.base.client.OpenStackClient.request(OpenStackClient.java:51)
at com.woorea.openstack.base.client.OpenStackClient.execute(OpenStackClient.java:66)
at com.woorea.openstack.base.client.OpenStackRequest.execute(OpenStackRequest.java:98)

And if you use it in OSGi, you will most likely have it.
The reason is that the default client is searched within the class path by using Java service loaders. And this mechanism does not work within OSGi.

When you encounter this solution, you may think to several solutions.
First, you can take a look at SPI Fly, a sub-project of Apache Aries. However, in my case, I feared a great impact because I only wanted ONE SPI class to be loaded, and not all.

You may also consider pacthing the code of the Openstack SDK to create a default client instance, instead of scanning the classpath.

However, I found out that there were 2 constructors in this library.
The first one takes an endpoint and will search for a connector. The second one takes an endpoint AND a connector. So, basically, to by-pass this error, you only have to instantiate your own connector (such as JerseyConnector) and pass it to your clients.

Keystone keystone = new Keystone( "http://my.url", new JerseyConnector());

And that’s it.


About this entry