NPE with Openstack SDK in OSGi

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

at com.woorea.openstack.base.client.OpenStackClient.request(
at com.woorea.openstack.base.client.OpenStackClient.execute(
at com.woorea.openstack.base.client.OpenStackRequest.execute(

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s