07.12.2010

Eclipse Virgo + Snaps: Modular Web Apps, finally, Part 2

In this part I will try to create some simple Snaps, together establishing a Web Application. I assume you have read Part 1 and have thus set up Virgo itself, applied the Snaps patch and have Eclipse up and running.

Of course, the Virgo Programmers manual covers a lot of the steps nessecary. My emphasis lies on Snaps and ease of development, so lets start:
First of all, you should install the Spring Virgo Tooling from http://dist.springsource.com/release/TOOLS/update/e3.6. After that you should be able to start your installed Virgo Server from within Eclipse following the instructions from the Virgo Programmers Manual.

My first Web application bundle!

Create an Eclipse RT bundle project:
Name it org.test.host. Besides the required project natures, only one file is created: The Manifest. Open it and, in the MANIFEST.MF editor, add the following line:
Web-ContextPath: /test
Then create an index.jsp file in the src-folder with any (simple) content you like, e.g. "hello, world".

Believe it or not, but you have just created your first web application bundle, an artifact that is an OSGi-bundle as well as a web application.
Now in the Server View,  in the popup-menu of your Virgo server, choose add and remove to add the new bundle to your server (of course you could also build a jar and copy it to the pickup area). You bundle is listed as available resource. Add it to your server. The console will show how the bundle is started and bound to the context /test. Thus it is not too surprising to get the hello world message on navigating to http://localhost:8080/test/index.jsp.

My first Snap!

Beware! Things may become boring now.
Create another bundle project, just as above, but to the MANIFEST.MF file now add:
Snap-Host: org.test.host;version="[1.0, 2.0)"
Snap-ContextPath: /snap1
Even another src/index.jsp should show some different content. Project layout should now be similar to this:

Congrats, your first snap was born. After adding it to the server, this content is now served under http://localhost:8080/test/snap1/index.jsp.
Note that this snap is not a web application bundle (we did not add the Web-ContextPath header), thus it does not serve http://localhost:8080/snap1/index.jsp.

Integrating snaps

The interesting thing is, the host can be extended by snaps. And that can be done so that the user interface automatically adapts.
Insert the following code in the index.jsp of the host:


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="snaps" uri="http://www.springsource.org/dmserver/snaps" %>

<html>
<head/>
<body>
<h1>Snaps</h1>
<snaps:snaps var="snaps">
 <c:forEach var="snap" items="${snaps}">
  <li>
   <a href="<c:url value="${snap.contextPath}/index.jsp"/>">
                             ${snap.contextPath}
                        </a>
  </li>
 </c:forEach>
</snaps:snaps>
</body>

To be able to compile this jsp, you need to import some packages into the bundle, namely


  • com.springsource.org.apache.taglibs.standard;version="[1.1.2,1.1.2]",
  • org.eclipse.virgo.snaps.api;version="[0.0.0,2.0.0]",
  • com.springsource.javax.servlet.jsp.jstl;version="[1.1.2,1.1.2]"
Pay attention to add the correct version ranges.
After redeploy (because you have changed the manifest, a simple JSP change is automatically hot deployed... great!), on http://localhost:8080/test/index.jsp, you are presented a list - ahem, well, with one item - of all snaps with a link to their respective index.jsp. This way, the application adapts to the current OSGi bundles state.