Skip to main content

OSGi enabled web applications inGlassFish

Posted by ss141213 on June 4, 2009 at 3:29 PM PDT

Since the OSGi-fication of GlassFish started, the initial response was very encouraging, but we were often asked as to how we planned to expose the benefits of OSGi platform to end users in a more direct way. We are committed to making this possible as is evident from the following quote from our initial proposal: "If GlassFish can benefit from OSGi, why not applications deployed in GlassFish? Application developers would like to use sophisticated versioning, class loading, dependency management and component model of OSGi. There is a growing demand for servers that expose such facilities to application developers. So, we shall investigate the use of OSGi by applications." I am glad to say that we have made considerable progress in this area. I just now put back a very preliminary implementation of OSGi EEG RFC #66 in GlassFish workspace that allows web applications (war files) to be deployed as OSGi bundles and there by taking advantage of OSGi platform as well as Java EE platform. Web applications and OSGi is a subject which is being actively discussed in OSGi Enterprise Expert Group (EEG) as RFC #66. As I said, what I have put back is just the start. Basic servlet. JSP, JSF, JDBC, JNDI, etc. works. Injection should work, but I have not tried yet. I know for sure JPA does not yet work as JPA classloading requirements conflict with OSGi's. Work is in progress to resolve it - stay tuned. So, how do you use these features in GlassFish? There are basically two starting points: a) You have a plain vanilla war file. b) You have a war file that has OSGi metadata in it. In the former case, you have to somehow instruct the server that it has to add necessary OSGi metadata to the war file. You should also be able to customize the transformation step. It is achieved by use of a special URl protocol called webbundle together with use of URL query parameters. The server has a custom URL handler for this protocol and it does an in place manifest rewrite when it encounters this scheme. To use it, you can do something like this in GlassFish:

telnet localhost 6666
install webbundle:file:///tmp/mybundle.war
start #bundle_id

(telnet support in GlassFish is provided by use of remote shell as described here.) The above commands will make your web app available at localhost:8080/mybundle/. Now you can control the life cycle of the web app via the OSGi bundle. e.g., if you stop the bundle by issuing stop #bundle_id, the web app gets undeployed. To deploy it again, issue the start command. In the latter case, i.e., case #b above, you just have to add a specific meta-data called Web-ContextPath in the to mark the OSGi bundle as a Web Application Bundle, (WAB) in short. Once you have done that, you can either install and start by running the shell commands without using webbundle protocol or simply copy the bundle to glassfish/domains/domain1/autodeploy-bundles/ dir. How this directory works is already described in a previous blog. Although I want to write more now, it's pretty late here. SO, catch you soon with instructions to install the new feature in GlassFish with some concrete example. Thanks.

Related Topics >>


Hi Sahoo! Please, answer to the following questions, as I ...

Hi Sahoo!
Please, answer to the following questions, as I asked it at different forums but still no answer.

We have glassfish 4 and we try to use wab instead of non-osgi ear and war. Everything is ok. We did all settings for maven, generated wab and copied it to domain/autodeploy/bundles. In pom.xml we have

groupId org.apache.felix /groupId
Web-ContextPath /webclient /Web-ContextPath
And we get our html page by http://x.x.x.x:8080/webclient/index.html. Now we want to assign it to one of virtual servers in otder to get it via domain name. So the questions:

1. If it's possible to assign a virtual server to wab by copying it to autodeploy/bundles folder (as we do) how to do it?
2. We tried to deploy it via console - applications -> deploy. Type - other, selected virtual server and checked osgi type. But we can't get it by domain name (404-not found). When you choose type-other there is no context root as in case of type - web application. We tryied to set it in WEB-INF/glassfish-web.xml
context-root/ /context-root
parameter-encoding default-charset="UTF-8"/
But we can't get it. It again works by ip but not by virtual server domain name. What do we do wrong?

Thanks Sahoo. I have it working now. I appreciate your reply.

That basically means the bundle is not in synch with your glassfish distribution. Which build of GF are you using? If you are using a promoted build, then you need to download corresponding build of osgi-web-container. e.g., let's say you are using b57 of GFv3, then you can download osgi-web-container.jar from
If you go one directory up, you shall find all the versions.
You can say why you didn't see a version mismatch exception from OSGi, well, that's a long story and I can't explain our version policy in this comment.
Feel free to ask questions at users(at)

Hi Sahoo, Thanks for your reply. I had already deployed the osgi-web-container jar, but I tried redeploying the same and I noticed the following exception in the log file. Also, I can see the bundle in the actice state (Felix remote shell) [#|2009-08-03T07:57:19.045-0400|INFO|glassfish|null|_ThreadID=15;_ThreadName=Thread-1;|Installed /services/glassfishv3/glassfish/domains/domain1/autodeploy-bundles/osgi-web-container-3.0-SNAPSHOT.jar|#] [#|2009-08-03T07:57:19.067-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;|Exception in thread "Thread-25" |#] [#|2009-08-03T07:57:19.068-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;|java.lang.NoSuchMethodError: org.glassfish.api.admin.ServerEnvironment.getStatus()Lorg/glassfish/api/admin/ServerEnvironment$Status;|#] [#|2009-08-03T07:57:19.069-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;| at org.glassfish.web.osgi.WebExtender.isServerStarted(|#] [#|2009-08-03T07:57:19.069-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;| at org.glassfish.web.osgi.WebExtender.access$300(|#] [#|2009-08-03T07:57:19.069-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;| at org.glassfish.web.osgi.WebExtender$|#] [#|2009-08-03T07:57:19.070-0400|SEVERE|glassfish|null|_ThreadID=23;_ThreadName=Thread-1;| at|#] I would be thankful if you can reply. Mahendra.

rfc#66 implementation is not "yet" part of GlassFish distribution. I think in the next promoted build, it will be part of distribution. In the meanwhile, you need to download the implementation bundle from
and copy it to autodeploy-bundles directory. No need to restart server. You can then try your example. I explained these steps in the next blog:
Thanks, Sahoo

Hi Sahoo, I am getting the following error when i try to install the war file - -> install webbundle:file:///tmp/mybundle.war Unknown protocol: webbundle Any ideas? Thanks, Mahendra.