Saturday, January 9, 2010

Developing Modular Web Application using Slices on Spring DM 2 RC1

Configurations and Installations
1. Download Spring DM 2 RC1

2. Download Slices bundles from http://www.springsource.com/download/community?project=SpringSource%20Slices&nightly=yes

3. Copy the bundles and the plan file in the dist directory of the zip to dm Server's repository/usr directory.

4. Edit DM Server's config/com.springsource.kernel.userregion.properties file to add slice’s plan under initialArtifacts –
initialArtifacts=repository:plan/com.springsource.kernel.userregion.springdm, repository:plan/com.springsource.server.web, repository:plan/com.springsource.osgi.slices

5. Restart DM server and we are ready to deploy web application using slices.

Sample web application
With Slices, we can develop web application using multiple OSGi bundles; each bundle provides content for a distinct sub-portion of the application's URL space. Slices applications are arranged in a parent/child structure, with each application having one parent called “host” and zero or more children called “slices”.


The advantage using Slices is that any section (slice) of the web application can be hot deployed without restarting the complete web application.


Let us consider a Travel Agent web application that provides reservations for flight and hotel. So, the structure looks like –

The “Flight Reservation” slice can be accessed using URL “/flights” and the “Hotel Reservation” slice can be accessed using URL “/hotels”.

Now let us look into web.xml and MANIFEST.MF for host and slice bundles.

The host web bundle (TravelAgent) has the following web.xml which is similar to any other web application except that com.springsource.osgi.slices.core.SliceHostFilter is configured to route request to its slices.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<filter>
<filter-name>host-filter</filter-name>
<filter-class>com.springsource.osgi.slices.core.SliceHostFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>host-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<servlet>
<servlet-name>agent</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>agent</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

</web-app>


Host web bundle (TravelAgent) has the following MANIFEST.MF

Manifest-Version: 1.0
Import-Bundle: org.springframework.context,org.springframework.context.support
Bundle-Version: 1.0.0
Bundle-Name: TravelAgent
Bundle-ManifestVersion: 2
Bundle-SymbolicName: TravelAgent
Web-ContextPath: /
Import-Package: javax.servlet.http;version="[2.5, 3.0)",org.springfram
ework.beans.factory.xml;version="[2.5.6.A,3.1)",org.springframework.w
eb.servlet;version="[2.5.6.A,3.1)",org.springframework.web.servlet.ha
ndler;version="[2.5.6.A,3.1)",org.springframework.web.servlet.mvc;ver
sion="[2.5.6.A,3.1)",org.springframework.web.servlet.view;version="[2
.5.6.A,3.1)",com.springsource.osgi.slices.core,org.springframework.st
ereotype,org.springframework.web.bind.annotation


Slices (FlightReservation and HotelReservation) has the following web.xml similar to any other web application

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

Slice web bundle (FlightReservation) has the following MANIFEST.MF

Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: FlightReservation
Bundle-ManifestVersion: 2
Bundle-SymbolicName: FlightReservation
Slice-ContextPath: /flights
Slice-Host: TravelAgent;version="[1.0.0, 2.0.0)"
Import-Bundle: org.springframework.context,org.springframework.context.support
Import-Package: javax.servlet.http;version="[2.5, 3.0)",org.springfram
ework.beans.factory.xml;version="[2.5.6.A,3.1)",org.springframework.w
eb.servlet;version="[2.5.6.A,3.1)",org.springframework.web.servlet.ha
ndler;version="[2.5.6.A,3.1)",org.springframework.web.servlet.mvc;ver
sion="[2.5.6.A,3.1)",org.springframework.web.servlet.view;version="[2
.5.6.A,3.1)",com.springsource.osgi.slices.core,org.springframework.st
ereotype,org.springframework.web.bind.annotation

Slice web bundle (HotelReservation) has the following MANIFEST.MF

Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: HotelReservation
Bundle-ManifestVersion: 2
Bundle-SymbolicName: HotelReservation
Slice-ContextPath: /hotels
Slice-Host: TravelAgent;version="[1.0.0, 2.0.0)"
Import-Bundle: org.springframework.context,org.springframework.context.support
Import-Package: javax.servlet.http;version="[2.5, 3.0)",org.springfram
ework.beans.factory.xml;version="[2.5.6.A,3.1)",org.springframework.w
eb.servlet;version="[2.5.6.A,3.1)",org.springframework.web.servlet.ha
ndler;version="[2.5.6.A,3.1)",org.springframework.web.servlet.mvc;ver
sion="[2.5.6.A,3.1)",org.springframework.web.servlet.view;version="[2
.5.6.A,3.1)",com.springsource.osgi.slices.core,org.springframework.st
ereotype,org.springframework.web.bind.annotation

A slice uses “Slice-Host” manifest header to define host to which it attach to. URL portion that the slice handles is defined using “Slice-ContextPath” manifest header.

Here I have not covered any configurations / code related to Spring MVC, we will cover Spring MVC in later posts.

2 comments:

Vikram said...

Hi, I tried the way you mentioned to install Slices into Spring DM. When I start my app, I get

Cannot find 'com.springsource.osgi.slices' version range '[0.0.0, oo)' in repository 'stage-ext-usr'

I downloaded the spring slices from the URL you had provided (5th March 2010) and in the dist there were three jar files.

Please let me know how I can get it started as I am really intrested in getting a start on with slices

Thanking you

Jessie said...

Most people cannot name all of the major hotel chains in a city, and trying to remember those names to visit their respective website will only lead to disappointment and a potentially lost great deal.

Pousada Buzios