Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSGI creating modular web application

I have been looking for a solution to create a modular web application, which is modular in the sense that user can provide its own plugin in form of a simple jar which will then provide its own data to my web application and my webapp will be responsible for displaying it.

Now the catch is i want my web app to be as generic as possible without relying on the j2ee web container to support anything . i.e. i cant rely on my web container to provide osgi support and deploy web application as an osgi bundle itself ( which truly makes things very simple for eg. glassfish and WAS).

I am planning to use equinox and only solution i see currently is the servlet bridge they provide on their official site, but to me it is really a pain to delegate everything to a servlet which will in turn interpret the request and find an apropriate bundle Class and then again communicate back somehow only the data to the web application.

To me it would be wonderful if my web app was also a bundle. Is there anything close to this ideal solution which i can try for? Or any other communication method between the two relams of osgi and web appliction (container)?

like image 978
FUD Avatar asked Sep 01 '11 05:09

FUD


2 Answers

The OSGi spec details the WAB (Web Archive Bundle) format.

And Pax Web offers great support for WAB/WAR webapps (PAX Web runs fine on Equinox, Felix, etc)

Using pax web you get the BundleContext via the ServletContext, eg:

BundleContext bundleContext = (BundleContext) getServletContext().getAttribute("osgi-bundlecontext");

For the user driven pluggability you mention, I'd suggested you provide some service interfaces for the plugin bundles to implement and in your webapp use a ServiceTracker to listen for their registration (unless you're using Declarative Services). You also easily be able to install bundles from an upload servlet.

I'm guessing users uploading plugins would have to be logged in and authorized, so security issues will have been met at this point.


EDIT: replying to comment here as not enough space in comment field

Apologies, think I misinterpreted you question - you have an existing webapp container(s) and you want to deploy a WAR with OSGi functionality? If that's the case then either use the ServletBridge as others have mentioned or embed an OSGi framework into your WAR (this is relatively easy, see this for example).

You could even make this optional by attempting to get the BundleContext from the ServletContext and if this returns null then launch your own embedded framework. That way it'll run in a native OSGi container (e.g. Glassfish) or in a Java EE app server.

Otherwise, PaxWeb is an implementation of the HttpService and WebApp OSGi specs, but with lots of extensions to make life easier - but you deploy this to an OSGi container.

like image 120
earcam Avatar answered Oct 14 '22 03:10

earcam


You might want to look into Apache Sling. It is a web framework that has an embedded OSGi container. The OSGi container is called Apache Felix and is pretty good.

like image 2
Swaranga Sarma Avatar answered Oct 14 '22 01:10

Swaranga Sarma