Photo Album Sample Project

Work in progress.

The purpose of this sample is to demonstrate how easy it becomes to implement a Spring DM web application using Pax Web and Pax Web Extender.
The application itself is a simple photo album application that is build up from more albums. The list of albums should be dynamic so when an album becomes available it is displayed into the main page.

The application is build up from scratch very easily using Pax Construct with a simple script as below and consists of the following bundles:

  • menu: application entry point showing available albums
  • dogs: album of dogs (servlet)
  • cats: album of cats (jsp)
  • ducks: album of ducks (Spring MVC)
pax-create-project -g org.ops4j.pax.web-extender.samples -a spring-dm
cd spring-dm
pax-create-bundle -p org.ops4j.pax.web.extender.samples.spring.menu -- -Dspring
pax-create-bundle -p org.ops4j.pax.web.extender.samples.spring.dogs -- -Dspring
pax-create-bundle -p org.ops4j.pax.web.extender.samples.spring.cats -- -Dspring
pax-create-bundle -p org.ops4j.pax.web.extender.samples.spring.ducks -- -Dspring

How does it work

The menu

The menu bundle is an Spring DM powered bundle that publishes the servlet (a Spring bean) PhotoAlbumsMenuServlet into OSGi services as an javax.servlet.Servlet using Spring DM <osgi:service .../>. The servlet shows a set of all active PhotoAlbums, albums that are "discovered" using Spring DM <osgi:set .../>.
Every one of the three albums (dogs, cats, ducks) expose themselves as a PhotoAlbum using Spring DM <osgi:service .../>.

The dogs

The dogs bundle is an Spring DM powered bundle that publishes a servlet that will show the album content bounded to http://localhost:8080/dogs and a PhotoAlbum service in order to be shown in the menu. All of this is done using Spring <bean.../> and using Spring DM <osgi:service .../>.

The cats

The cats bundle is an Spring DM powered bundle that beside publishing a PhotoAlbum, enables the jsp support by registering the url patterns (*.jsp) as a JspRegistration service using Spring DM <osgi:set .../>.

The ducks bundle

The ducks bundle brings Spring Web MVC into picture as part of an Spring DM powered bundle. If you are not familiar with Spring Web MVC I suggest that you find that out on Springframework site. Those familiar with Spring Web MVC know that the start up point is the DispatcherServlet that you usually configure it into your web.xml and it will load the application context from a well defined (customizable location) xml. To make it play along with Spring DM small customization is required in order to override this behavior (creating the application context) and make it use the application context created by Spring DM. Here is an example of how you can archive that. There is not enough to have an application context because DispatcherServlet needs a WebApplicationContext. So, in this example a wrapper application context over Spring DM application context is created using Spring AOP. The AOP part is not required as a "usual" wrapper will be just fine.
Then once we have the servlet, the rest is as for the other bundles: expose it using using Spring DM <osgi:service .../>, ...

Where is my web server?

Wondering how do the servlets, resources or jsps get on the web? Well, you can read more about on Pax Web Extender - Whiteboard but in short Pax Web Whiteboard Extender tracks these kind of elements and registers/unregisters them automatically with Pax Web (OSGi Http Service implementation and more). There are also inline comments into spring xml file.

Source code

Source code is available in OPS4J repository at https://scm.ops4j.org/repos/ops4j/laboratory/users/adreghiciu/spring-dm/.

Building

To build the photo album sample you have to checkout the source code and from the root directory:

mvn clean install

Running

You can run the photo album sample in different ways (after you build it), like:

  • using Pax Construct: from the root directory:
    pax-provision
    
  • using OPS4J maven pax plugin (par of Pax Construct): from the root directory:
    mvn pax:provision
    
  • using Pax Runner: you can do that in many ways but a simple method will be: from the root directory:
    pax-run --args=file:spring-dm.args
    

Once the application gets started point your web browser to http://localhost:8080/spring.
To observe how application reacts to albums life cycle you can stop / start any of the bundles. You can do that from the started osgi framework console using start and stop commands.

Content