User Tools

Site Tools


osgi_web_application

OSGi Web Application

OSGi has support for web applications included for a long time in form of Java Servlet support.

Maven

The maven-bundle-plugin does most of the work for getting the manifest right.

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.3.5</version>
				<executions>
					<execution>
						<id>bundle-manifest</id>
						<phase>process-classes</phase>
						<goals>
							<goal>manifest</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<supportedProjectTypes>
						<supportedProjectType>jar</supportedProjectType>
						<supportedProjectType>bundle</supportedProjectType>
						<supportedProjectType>war</supportedProjectType>
					</supportedProjectTypes>
					<instructions>
						<Embed-Directory>WEB-INF/lib</Embed-Directory>
						<Embed-Dependency>*;scope=compile</Embed-Dependency>
						<Embed-Transitive>true</Embed-Transitive>
					</instructions>
				</configuration>
			</plugin>
			<plugin>
				<!-- Need to use this plugin to build war files -->
				<artifactId>maven-war-plugin</artifactId>
				<groupId>org.apache.maven.plugins</groupId>

				<version>2.4</version>
				<configuration>
					<!-- We don't always have a web.xml -->
					<failOnMissingWebXml>false</failOnMissingWebXml>

					<archive>
						<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

The project specific manifest entries are extracted into an extra properties file:

Bundle-ClassPath: .,WEB-INF/classes
Web-ContextPath: /application_url_part

It is important to add the root of the WAR file to the bundle classpath.

Servlet

An extremely simple Servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(displayName="RPG Next Gen OSGi Simple Servlet" , value="/*")
public class SimpleServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		writer.println("Enter the matrix ...");
	}
}

No web.xml file is needed for this simple setup.

Apache Karaf

For this example to work in Apache Karaf only the war feature is needed.

feature:install war
osgi_web_application.txt · Last modified: 2014/03/18 20:39 by mihael