How to access a (Blueprint) OSGi service from a Pax Web deployed web bundle?

1.3k views Asked by At

I am currently using Pax Web to deploy an OSGi web application bundle. From this web bundle I need access to a set of OSGi services. In the rest of the system I am using Blueprint for creating and consuming services. This is great as it provides proxies, damping etc.

From the Pax Web deployed web application bundle what is the best way of getting access to these OSGi service?

My thoughts are

1) Get the BundleContext from the ServletContext via the osgi-bundlecontext attribute and then use the OSGi ServiceTracker class.

2) Create a Blueprint context file in the web bundle that contains references to the services I require. The Blueprint context is registered in the OSGi service registry by the Blueprint extender from where I can retrieve it and then look up the beans by id.

3) Create a Blueprint context in the web.xml file using something like Springs ContextLoaderListener.

Option 1 is fine but means I have to track services and don't get any of the benefits of Blueprint.

Option 2 works fine too except it suffers timing issues. In effect there are two services now deploying the application - the Pax Web extender deploying using the web.xml file and the (in my case, Gemini) Blueprint extender deploying the Blueprint context files. On start up these extenders deploy at the same time and there is no guarantee the application context will be available in the OSGi service registry when the Pax web extender instantiates any servlet context listeners. This means OSGi services cannot be called from the context listener code.

Option 3 - not too sure if this can be done. It looks like Spring DM used to use this approach?

I feel I must be missing something - is there an easy way to use Blueprint from within a Pax Web deployed web bundle?

1

There are 1 answers

2
Achim Nierbeck On

Yeah you are missing another one that'll work Use jndi as bridge. If using aries (I don't know about gemini though) you are able to retrieve any OSGi service also as jndi lookup. Now retrieve this via the web.xml and you'll be fine.

For example use

osgi:service/<interface>[/<filter>]

as your jndi source from your web.xml The benefit of this, you don't run into the issues of #2 Regarding #3 that can't be done right now. So basically you still have the choices of 3 but I'd suggest using #4