User Tools

Site Tools


vaadin_7_osgi_web_application

This is an old revision of the document!


Vaadin 7 OSGi Web Application

Vaadin 7 is a Servlet based Java web framework. And as most OSGi container support Servlet based web applications very well. So this should be a match made in heaven. But their are some minor things one will stumble upon when trying to run a Vaadin application inside an OSGi container. One of those things is always the classpath.

From Vaadin 6 to Vaadin 7

Some major changes have been made to the Vaadin API from version 6 to 7 but also some structural changes to the deployment. When Vaadin 6 was comfortable packaged into one jar Vaadin 7 comes with multiple jars. The Vaadin 6 jar didn't depend on any other jar which was very convenient. In contrast Vaadin 7 depends on dozens of other jars (some of them not OSGi ready out of the box).

OSGi Web Application Packaging

OSGi Web Applications are called WAB ⇒ Web Application Bundle. Same as a bundle is just a jar file with an extended manifest file the same goes for the WAB. A web application bundle is just a WAR file with an extended manifest.

Demo Vaadin 7 OSGi Web Applicaton

The demo application is a stripped barebone Vaadin application. It uses the default widgetset and the default Vaadin Reindeer theme.

It is derived from the Vaadin application maven archetype and just shows a button which will display a message on click.

Development

Maven and the Maven Bundle Plugin (and some other plugins) are used for development.

Application Code

To get the application code from the source code repository just clone it with hg (Mercurial):

hg clone http://hg.code.sf.net/u/fist/test.vaadin7 test.vaadin7

VaadinServlet

The VaadinServlet class is used to register the web application at the servlet container. Due to a classpath problem a class extending VaadinServlet must be created so that the bundle can resolve all needed classes.

package test.vaadin7;

import com.vaadin.server.VaadinServlet;

public class OsgiVaadinServlet extends VaadinServlet {

    private static final long serialVersionUID = -4733496883901044844L;

}

web.xml

The registration of the servlet is done via the web.xml file (annotations didn't work).

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
	version="3.0">

	<servlet>
		<servlet-name>vaadin_servlet</servlet-name>

		<servlet-class>test.vaadin7.OsgiVaadinServlet</servlet-class>

		<init-param>
			<param-name>UI</param-name>
			<param-value>test.vaadin7.MyVaadinUI</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>vaadin_servlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>

Don't forget to specify your own VaadinServlet class in the web.xml file.

Maven Bundle Plugin

The Maven Bundle Plugin takes care of all things related to the building of a bundle with the needed entries in the manifest file. Just add it to the pom.xml.

<plugin>
	<groupId>org.apache.felix</groupId>
	<artifactId>maven-bundle-plugin</artifactId>
	<version>2.3.6</version>
	<extensions>true</extensions>
	<executions>
		<execution>
			<id>bundle-manifest</id>
			<phase>process-classes</phase>
			<goals>
				<goal>manifest</goal>
			</goals>
		</execution>
		<execution>
			<id>bundle-install</id>
			<phase>install</phase>
			<goals>
				<goal>install</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<supportedProjectTypes>
			<supportedProjectType>ejb</supportedProjectType>
			<supportedProjectType>war</supportedProjectType>
			<supportedProjectType>bundle</supportedProjectType>
			<supportedProjectType>jar</supportedProjectType>
		</supportedProjectTypes>
		<instructions>
			<!-- Read all OSGi configuration info from this optional file -->
			<_include>-osgi.properties</_include>
			<Bundle-PresentationName>${project.artifactId}</Bundle-PresentationName>
			<Embed-Directory>WEB-INF/lib</Embed-Directory>
			<Embed-Dependency>*;scope=compile</Embed-Dependency>
			<Embed-Transitive>true</Embed-Transitive>
		</instructions>
	</configuration>
</plugin>

Building

Building the web application bundle with maven is no more than doing a

mvn clean package

The target folder should contain the bundle (war) file.

Deployment

Apache Karaf

Installing Prerequisites

Installing Vaadin

Installing Demo

Eclipse Equinox

Installing Prerequisites

Installing Vaadin

Installing Demo

Open Questions

The Vaadin Push support has been tested in this setup. It may need some additional classes which has to be available on the bundle classpath.

Source Code

vaadin_7_osgi_web_application.1394551143.txt.gz · Last modified: 2014/03/11 15:19 by mihael