Introduction to the OSGi Web Container |
|

Updated: added version control instructions for Git.
For the last few months I've been working with Subbarao Meduri, Graham Charters, Hal Hildebrand and others from the OSGi Enterprise Expert Group on the RFC66 Web Container specification. The Web Container specification defines how WAR files can be deployed on an OSGi service platform in a standard way.
This is extremely interesting for us, because dm Server has supported WAR files for nearly 18 months now and we are excited to be able to work towards a standard model. As an end user, you'll be able to deploy WAR files on OSGi without depending on proprietary APIs or features.
SpringSource is responsible for writing the Web Container reference implementation, and I've been working on this over the last few weeks. In this blog, I'll describe the main features of the Web Container specification and discuss the steps you need to get started with the RI code. I am not permitted to post the specification document, but I'll outline the most important points.
At the moment, there is no binary distribution for the RI, but it is really easy to get started by building from source.
Main Features in Web Container
The most interesting features supported by the Web Container are:
- Installation of WAR files
- Installation of Web Application Bundles (WABs)
- Web application lifecycle control using an extender
- Control of configuration properties using URL parameters
Installing WAR Files
For me, the most exciting feature of the Web Container is the ability to deploy WAR files directly into OSGi *without having to change your code*. (WAR files that use JNDI cannot be used without changes to your code, because JNDI is not currently part of the specification. There is work underway to fix this, so I don't expect this limitation to exist forever.)
To install a WAR file into OSGi, you simply prepend webbundle: to the file location when calling BundleContext.installBundle or when using your platform's console. For example, in Equinox I can do this:
install webbundle:file:formtags.war
Of course, this requires that you have a Web Container implementation installed into your OSGi platform.
The Web Container performs various transformations on the WAR to ensure that is has a symbolic name, version and the necessary imports for it to work. The Web Container will also update the bundle classpath to add WEB-INF/classes and every JAR file in WEB-INF/lib.
Installing Web Application Bundles (WABs)
If you don't want to rely on Web Container to transform your WAR files, then you can bypass the transformation stage entirely. Simply, add the appropriate manifest headers yourself and install the bundle directly, omitting the webbundle protocol.
WABs cannot have any entries on the bundle class path that are not under WEB-INF. This is to prevent any of your application classes being visible as resources in your web application. It is likely that other restrictions will arise for WABs, but these are yet to be finalized.
Lifecycle control using an extender
The Web Container uses the extender model to control web application lifecycle. Web applications are started when the corresponding bundle is started, and stopped when that bundle is stopped.
In Equinox, this means I can start and stop my web application directly from the console. For example, if the formtags.war bundle above is given bundle ID 50:
start 50
... Tomcat output ...
stop 50
Controlling configuration using URL parameters
WAR file configuration can be controlled by appending certain supported configuration properties to the install URL.
For example, to control the context path of the web application, you can add the Web-ContextPath option:
install webbundle:file:formtags.war?Web-ContextPath=ftags
Or to control the generated Bundle-SymbolicName header:
install webbundle:file:formtags.war?Bundle-SymbolicName=ftags.bundle
Web Container and dm Server
The code in the Web Container RI is mostly taken from and inspired by the code in dm Server, However, the RI *does not* require full dm Server. We'll be replacing the current web code in dm Server with the Web Container RI and we'll also be adopting Web Container as the recommended approach for building web applications on OSGi.
Introduction to the Reference Implementation
The Web Container RI uses Tomcat as the servlet container implementation. The RI itself is made up of four bundles. The core bundle contains all the code that is servlet container-independent. The tomcat bundle contains the Tomcat specific code. The tomcat.fragment bundle contains the default Tomcat configuration and is attached to the Tomcat Catalina bundle as a fragment. The extender bundle contains all the extender behaviour and can be removed if you prefer to manage web application lifecycle manually.
Building the Reference Implementation
To get running with the RI you need to build it from source. The source can be obtained directly from Git:
git clone git://git.springsource.org/osgi-web-container/osgi-web-container.git
Once the code is checked out, you can build it and test it using the commands below:
cd build-web-container
ant clean test collect
If the tests fail for you, please raise a JIRA. After a successful build you can run the Web Container and install some WAR files.
Running the Reference Implementation
I'm using PAX Runner to run the Web Container RI. My PAX Runner configuration runs the Web Container using the built binaries and Ivy-managed dependencies.
In the build-web-container directory you'll find a file called runner.bundles. This file can be used to instruct PAX Runner to install all the bundles needed by the Web Container:
pax-run --platform=equinox --snapshot runner.bundles
This command starts Equinox using PAX Runner. The –snapshot flag tells PAX Runner to download the latest stream stable build of Equinox – one that implements the latest known OSGi 4.2 specification.
Once Equinox starts issue the ss command to verify that the bundles are installed, you should see around 45 bundles installed and running.
Installing an application
I'm using the FormTags WAR sample from dm Server for testing. You can download this from here.
If I try to install the FormTags application directly, things don't quite work out as expected:
install webbundle:file:formtags.war
Bundle id is 48
start 48
After running start, I get an error complaining about ClassNotFoundException for some class in the org.xml.sax package. The issue here is that WAR files are only given 4 imports by default: javax.servlet, javax.servlet.http, javax.servlet.jsp and javax.servlet.tagext.
It is relatively easy to get around this using the Import-Package URL parameter:
uninstall 48
install webbundle:file:/Users/robharrop/tmp/formtags.war?Import-Package=org.xml.sax,org.xml.sax.helpers,javax.xml.parsers,org.w3c.dom
Bundle id is 49
start 49
Here I'm specifying the extra package imports needed by the FormTags application. This time, this application starts successfully and can be accessed from within the web browser at http://localhost:8080/formtags.
In dm Server, the WAR file gets an import for every package exported by the system bundle, which means common system types are automatically available. I think this is a useful feature and I'm interested in knowing if you agree or if you prefer manually controlling access to system packages.
What next?
The Web Container specification is still moving and I'm trying to keep the RI in sync. I'm working on moving dm Server to use the Web Container RI in place of its own web support and I'll have more to say on that in the coming weeks.
Similar Posts
- Deploying WARs to the OSGi Web Container is now even easier
- What the OSGi Web Container means for dm Server
- dm Server 2.0.0 released
- dm Server 2.0 RC1 released
- First Eclipse Gemini Web Milestone Ships





Sam Brannen says:
Added on May 27th, 2009 at 1:40 pmRob,
That's great news, and I'm glad to see you guys are making good progress with RFC 66!
[QUOTE]I'm working on moving dm Server to use the Web Container RI in place of its own web support…[/QUOTE]
Does this mean death to the Web Module format and associated Web-* manifest headers we introduced in dm Server, or is that the topic of your next blog?
FYI: You mentioned a diagram regarding the contents of the RI, but it appears that you forgot to include the image in the post. Some JavaDoc still refers to "war:" instead of "webbundle:".
I noticed your use of the com.springsource.osgi.test OSGi testing support. Is this something that will be publicly released, or is it solely intended for testing the RI? Similarly, are you planning any synergies between this framework and the upcoming com.springsource.server.test dm Server Test Framework (e.g., externalized configuration via Properties files and the @Plugins support for Ivy, Maven, etc.)?
Cheers,
Sam
Sam Brannen says:
Added on May 27th, 2009 at 2:13 pm[QUOTE]In dm Server, the WAR file gets an import for every package exported by the system bundle, which means common system types are automatically available. I think this is a useful feature and I'm interested in knowing if you agree or if you prefer manually controlling access to system packages.[/QUOTE]
I agree that this is very useful. In fact, I think it's unintuitive that one should have to supply a list of packages which should be (IMHO) always available. On the flip side, you could of course provide a mechanism (e.g., URL query parameter) to disable the automatic import.
– Sam
Michael Pilquist says:
Added on May 27th, 2009 at 8:54 pmGreat news, and it is nice to see that the RI is licensed under the ASL2. I assume this means that Spring DM 2's web support will be replaced with this RI? Will this be distributed as part of DM2 or will it be a separate distribution?
Regarding auto-importing the packages from the system bundle, I prefer to semi-manually control imports via BND (haven't tried Bundlor yet). The OSGi manifest can be used as the common language for tooling to analyze bundle dependencies and enforce architectural constraints. If magic happens at build time, this isn't a problem — the resulting manifest can still be analyzed by tools. But if the magic happens at run time, the tool chain needs to understand how the manifest will be transformed.
Daniel Rubio says:
Added on May 27th, 2009 at 11:19 pmNice to see the Enterprise OSGi specs going forward.
As a dm Server user, will this RI change the way dm Server supports its deployment formats (Standard WAR , shared library/service WARs deployments,etc) ? Or is this more a 'behind the scenes' for dm Server users, as in 'the dm Server is now RFC66 compliant'?
A 1 on 'common system types being automatically available' Packages like org.xml.sax,org.xml.sax.helpers,javax.xml.parsers,org.w3c.dom are just the tip of the iceberg (javax.sql is another popular one that comes to mind ) of many packages assumed to be'always' available.
On the thread of the web container/dm server progress , a related topic, would you happen to have any updates on the UI composition topic you mentioned on a previous post? I haven't found any other source(blog, docs, code) on this topic other than this post (http://blog.springsource.com/2009/04/01/springsource-dm-server-roadmap/).
Congratulations on your progress,
Rob Harrop (blog author) says:
Added on May 28th, 2009 at 12:53 pm@Sam – the diagram actually didn't add much value and the text has now been removed.
@All- I'm currently putting together another entry to discuss the impact on dm Server of moving on dm Server to Web Container. Web Modules are being removed and I'm interested in feedback about what value-added features people like to see layered on top of WAR support. More on that in the next entry.
@Sam – regarding the Test Framework, we're currently in a holding pattern right now. We have all we need internally and we're actually focusing on testing that doesn't require starting OSGi. I'm trying to put together a really good set of stub classes which make it really nice to test outside of OSGi. Something similar to the MockHttpServlet* classes in spring-test.
@All – regarding the system package support. I've raised the issue with the spec group and I'll be adding a URL parameter flag in the next few days to enable system package import. In the work to integrate Web Container into dm Server I've made system package inclusion the default
Sakuraba says:
Added on May 29th, 2009 at 5:58 amSpring – The future is now.
Anoop says:
Added on June 2nd, 2009 at 3:31 amWill the OSGI Web Container support "Modular Web applications" as mentioned in the DM Server roadmap (http://blog.springsource.com/2009/04/01/springsource-dm-server-roadmap/) – See below
"Using the modular web application support in dm Server you will be able to split your web content across multiple bundles, referred to as web fragments. All web fragments in an application share the same ServletContext session state. Each web fragment is assigned to handle a sub-portion of the URL space for the application, much like web context mapping is done with WAR applications"
Also will Spring-MVC with RESTful support and Spring WebFlow(along with Spring JS and Spring Faces) be supported on the OSGI Web Container ?
Rob Harrop (blog author) says:
Added on June 2nd, 2009 at 5:45 am@Anoop
Modular web application support will run on the Web Container but it won't be a part of the spec.
Spring-MVC, REST and WebFlow will all work on the Web Container. In fact, I'm just finishing up the last few test cases now
Rob
Don Laidlaw says:
Added on June 5th, 2009 at 8:56 amThe build using ant and ivy worked fine and all the tests ran. It sure would be convenient for me to be able to access all these artifacts in maven though! We can generate poms for the main artifacts using the provided build, but the transitive dependencies are not resolvable with maven at this time. Any plans?
Rob Harrop (blog author) says:
Added on June 8th, 2009 at 1:38 pmDon,
I _thought_ we were generating the right Maven metadata with our builds
Let me check what's up and I'll get back to you.
Regards,
Rob
Don Laidlaw says:
Added on June 15th, 2009 at 12:46 pmThere is a little disconnect at the moment in the structure of a web application bundle and what is required in Eclipse for running the bundle using the OSGi Framework launch configuration. I know – it has been discussed already in other places about the Eclipse required location of the manifest. But now, to run the web application bundle in eclipse using the launch configurations we will also have to put all the web resources in the root of the project. Either that, or forget about running the bundle using the nice tooling in eclipse. Any thoughts on that problem, and how to overcome it?
Rob Harrop says:
Added on June 15th, 2009 at 3:20 pmDon,
I don't have a huge amount of experience using the Eclipse PDE tools and launch configurations. For OSGi application development I'm mostly using the SpringSource Tool Suite with dm Server. In STS you get a nice integration of WTP and OSGi tools to give you hot-deployment and live refresh of OSGi-based web applications.
In the next release of the dm Server tools plugin that will accompany dm Server 2.0 M3, the web support is being updated to support RFC66 WARs to the same level that dm Server web modules were supported. Expect to see this hit the download site in the next two weeks.
Rob
Tom H says:
Added on June 22nd, 2009 at 2:01 pmHi,
I thought the post was gret but the version of ivy pulled from svn isn't honoring any proxy settings. For me that means it works fine at home but then when I try it up at work it hangs. After a while a timeout is hit and then the build fails.
I tried hitting your JIRA link but I don't have an account…
Thanks,
Dave H says:
Added on June 26th, 2009 at 7:06 pmThis is good stuff. One question I have is does the web container support installing exploded WARs? I tried to install an exploded war using multiple permutations of the form:
install webbundle:file:/path/to/exploded/war
install webbundle:reference:file:/path/to/exploded/war
Is this possible?
Andy Wilkinson (blog author) says:
Added on June 27th, 2009 at 6:17 am@Dave, support for exploded WAR files is in our backlog for the 2.0 release: https://issuetracker.springsource.com/browse/DMS-114
Valery Tydykov says:
Added on June 27th, 2009 at 9:07 pmDoes Web Container RI work inside of non-OSGI servlet engines, e.g. WebSphere, WebLogic?
Rob Harrop (blog author) says:
Added on July 29th, 2009 at 9:28 am@Valery
The Web Container works only inside an OSGi runtime. If there are servlet engines that provide an OSGi runtime I expect the Web Container will run just fine in that environment, but without OSGi present nothing will run.
Regards,
Rob
Valery Tydykov says:
Added on July 29th, 2009 at 11:18 amRob,
<>
I can deploy servlet bridge (e.g. Equinox) inside of WebSphere, WebLogic. The servlet bridge provides an OSGi runtime. Will Web Container RI work inside of the
servlet bridge? Will there be a conflict between the two servers listening on the same port?
Yassine says:
Added on August 15th, 2009 at 1:00 pmthe url: https://anonsvn.springsource.org/svn/dm-server-osgi-web-container/trunk returning an error:
Could not open the requested SVN filesystem
and my native svn client gets the same phrase: "Could not open the requested SVN filesystem" when trying to co the project
could you please fix the problem?
thanks for the great blog entry !!
cheers
yassine
Miles G says:
Added on October 29th, 2009 at 3:17 amHi,
This is a great blog. Thanks for it.
I synced the sources and tried the OSGI Web Container.
One thing that I didn't manage to do is to change the context root.
You wrote that the following has to be used for this purpose:
install webbundle:file:formtags.war?Web-ContextPath=ftags
But maybe I'm doing something wrong, because the context root is again the war name and was not changed.
Can you give me a hint?
Thanks in advance
Dobri says:
Added on February 22nd, 2010 at 7:44 amHi,
I want to play a bit with osgi-web-container but seems:
git clone git://git.springsource.org/osgi-web-container/osgi-web-container.git
is no more active. Where I can download the web container from?
I have one more question. Is it possible to have the native WAR deployment explained without having the Spring DM Server? I mean to have pure Equinox, Tomcat/Catalina bundles from the springsource repository and the osgi-web-container bundle build? I read that:
"The code in the Web Container RI is mostly taken from and inspired by the code in dm Server, However, the RI *does not* require full dm Server. We'll be replacing the current web code in dm Server with the Web Container RI and we'll also be adopting Web Container as the recommended approach for building web applications on OSGi."
so it seams there are still some dependencies on SpringDM Server?
Thank you in advance for every information you provide to me,
Dobri
Andy Wilkinson (blog author) says:
Added on February 22nd, 2010 at 8:37 amThe repository git://git.springsource.org/osgi-web-container/osgi-web-container.git is still active. What error message did you get when you tried to clone it?
The OSGi Web Container doesn't have any dependencies upon dm Server, and can be run in pure Equinox. There are instructions for doing so above using Pax Runner. See the section entitled "Running the Reference Implementation".
You may be interested to know that things are moving along with the web container's move to the Gemini project (http://www.eclipse.org/proposals/gemini/) at Eclipse.org. There's also a user forum available for the project: http://www.eclipse.org/forums/index.php?t=thread&frm_id=153
Dobri says:
Added on February 22nd, 2010 at 9:22 amHi Andy,
thank you for the fast reply.
The problem with git at my side is:
"D:\>git clone git://git.springsource.org/osgi-web-container/osgi-web-container.git
Initialized empty Git repository in D:/osgi-web-container/.git/
git.springsource.org[0: 89.21.231.40]: errno=No such file or directory
fatal: unable to connect a socket (No such file or directory)"
I asked some of my colleagues to check, too, and they get the same result.
When I get the osgi-web-container then I will definitely check the sample proposed.
Thanks for the information about the movement to Gemini project.
Andy Wilkinson (blog author) says:
Added on February 22nd, 2010 at 10:36 amThat's strange. I've just tried doing a fresh clone and it worked fine on both Mac and Windows using Git 1.6.3.2 and 1.6.4.msysgit.0 respectively. Which client are you using?
Dobri says:
Added on February 22nd, 2010 at 10:57 amI am using:
git-gui version 0.12.0.23.ga91be
git version 1.6.5.1.1367.gcd48
Tcl/Tk version 8.5.7
I will check with the version you are using.
Thanks.
Dobri says:
Added on February 24th, 2010 at 3:09 amHi Andy,
I am not an expert but I am interested in some aspects of deploying WAR into the OSGi world.
I am not sure this is the place to ask you that question, but if you do not need to have your WAR as a bundle, is it possible to configure the web server (e.g. Tomcat) bundle in such a way, that WAR files are deployed in the natural way. Just dropping them into the "webapp" (Tomcat) folder. Thus you can deploy your legacy WARs without bundlizing them. I can imagine doing this modifying the startup bundle of Tomcat and modify its default-server.xml settings?
Thanks!
Andy Wilkinson (blog author) says:
Added on February 24th, 2010 at 7:35 am@Dobri, there's no mechanism in the RI itself for automatic deployment when a war's dropped into a directory. dm Server, which embeds the RI, offers this functionality in the form of its pickup directory. You can, of course, deploy a standard WAR in the RI programatically using the webbundle: URL scheme as described above.
Dobri says:
Added on February 24th, 2010 at 8:16 am@Andy, my understanding is that if you use "webbundle: URL scheme as described above", than the WAR will be bundle-ized and will be part of the OSGi life-cycle. Then more or less you have a modified WAR (modified MANIFEST with OSGi headers) that can benefit from OSGi platform (e.g. services).
Anyway, my question is a little bit different. What about having WAR deployed without bundalization, so it is not part of the OSGi. I mean can we have war deployed into Tomcat bundle dedicated folder, without the need to have RI? E.g. deployed into a "webapp" folder of the Tomcat bundle (available into the SpringSource repo) and "exploded" into it.
I was thinking that the default-server.xml used by Tomcat startup bundle (available as part of Spring DM) could be modified so a "webapp" folder was set up? Thus you do not need the RI and you "have" full backward compatibility of your legacy WARs. WARs are fully functional, but without being directly part of OSGi platform (e.g. only Tomcat is a bundle).Last but not least, you have "exploded" WAR.
Thank you in advance!
@n@stell says:
Added on March 19th, 2010 at 2:01 amHi,
I have additional question here for the OSGi Web Container.
I tried it and saw that the application is loaded from the war file.
But my application uses a functionality (such as getRealPath() etc.) that expects that application is extracted as in the standard Tomcat/Jetty.
Is there some property that I can use in order to force the OSGI Web Container to extract the war file on the file system or the application is loaded always from the archive?
If there is no such functionality, then do you plan to provide such?
Thanks in advance
Rochi Dommarco says:
Added on April 23rd, 2010 at 3:42 amHi Rob,
I want to migrate an existing OSGI App to WebContainer. The App registers some servlets to the HTTPService (PaxWeb). I put in place a WAR to manage the security with SpringSecurity, but I need to register the servlets to the Web Container. How I can do? Any example?
Thanks in advance
Anton Yazovskiy says:
Added on May 6th, 2010 at 11:30 amHi.
Thx for this project! This is exactly that I'd looking for.
Have some problem:
then try clone git:
- git clone git://git.springsource.org/osgi-web-container/osgi-web-container.git
every thing looks good. But then I try to run Ant:
- ant clean test collect
I got the problem:
"Cannot find MY_PATH\osgi-web-container\spring-build/multi-bundle\default.xml imported from MY_PATH\osgi-web-container\build-web-container\build.xml"