You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Shai Amar <sh...@krynnlance.com> on 2011/09/06 17:41:05 UTC

running servlet in felix OSGi

Hi

I'm trying to run a simple servlet in Felix OSGi under Glassfish 3.1
application server

I've created 3 files:

   1. *BackAppsOSGiSevletPOC*
   2. *ServletServiceTracker *
   3. *ServletActivator*

The files are specified below.

Then, after I've installed the module, I've tried to activate the servlet
via the request *http://localhost:8080/simple* and got nothing.
I've tried to check and recheck what I've done and came out empty.

Can anyone tell me what I'm missing here?


*BackAppsOSGiSevletPOC *with the following code:

-------------------------------------------------------------------------------------------------------------------------------------------
*package com.krynnlance.backapps.client.servlet;*
*
*
*import java.io.IOException;*
*
*
*import javax.servlet.ServletException;*
*import javax.servlet.http.HttpServlet;*
*import javax.servlet.http.HttpServletRequest;*
*import javax.servlet.http.HttpServletResponse;*
*
*
*public class BackAppsOSGiSevletPOC extends HttpServlet*
*{*
*
*
* private static final long serialVersionUID = 1L;*
* *
* /***
* * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)*
* */*
* protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException *
* {*
* response.setContentType("text/plain");*
* response.getWriter().write("This is the Servlet module [GET request]...");
*
* }*
*
*
*}*
-------------------------------------------------------------------------------------------------------------------------------------------

*ServletServiceTracker* with the following code:

-------------------------------------------------------------------------------------------------------------------------------------------
*package com.krynnlance.backapps.client.servlet;*
*
*
*import org.osgi.framework.BundleContext;*
*import org.osgi.framework.ServiceReference;*
*import org.osgi.service.http.HttpService;*
*import org.osgi.util.tracker.ServiceTracker;*
*
*
*public class ServletServiceTracker extends ServiceTracker*
*{*
* *
* public ServletServiceTracker(BundleContext context)*
* {*
* /***
* * The service tracker will be registered for a specific class - *
* * in our case that's org.osgi.service.http.HttpService*
* */*
* super(context, HttpService.class.getName(), null);*
* }*
* *
* /***
* * When the service tracker is asked to return the HttpService, *
* * we can register our servlet*
* */*
* public Object addingService(ServiceReference reference)*
* {*
* HttpService httpService = (HttpService)super.addingService(reference);*
* *
* if(httpService == null)*
* {*
* return null;*
* }*
* *
* try*
* {*
* System.out.println("Registering servlet at /simple");*
* httpService.registerServlet("/simple", new BackAppsOSGiSevletPOC(), null,
null);*
* }*
* catch (Exception e) *
* {*
* e.printStackTrace();*
* }*
* *
* return httpService;*
* }*
* *
* /***
* * Likewise, we must make sure to unregister the servlet when the *
* * service tracker is asked to shut down the HttpService*
* */*
* public void removedService(ServiceReference reference, Object service)*
* {*
* HttpService httpService = (HttpService)service;*
* *
* System.out.println("Unregistering /simple");*
* httpService.unregister("/simple");*
* *
* super.removedService(reference, service);*
* }*
*}*
-------------------------------------------------------------------------------------------------------------------------------------------

*ServletActivator *with the following code:

-------------------------------------------------------------------------------------------------------------------------------------------
*package com.krynnlance.backapps.client.servlet;*
*
*
*import org.osgi.framework.BundleActivator;*
*import org.osgi.framework.BundleContext;*
*
*
*public class ServletActivator implements BundleActivator*
*{*
* private ServletServiceTracker servletServiceTracker;*
* *
* *
* @Override*
* public void start(BundleContext ctx) throws Exception *
* {*
* servletServiceTracker = new ServletServiceTracker(ctx);*
* servletServiceTracker.open();*
* }*
*
*
* @Override*
* public void stop(BundleContext ctx) throws Exception *
* {*
* servletServiceTracker.close();*
* servletServiceTracker = null;*
* }*
*
*
*}*

-------------------------------------------------------------------------------------------------------------------------------------------

And the *pom.xml* woth the following code:

-------------------------------------------------------------------------------------------------------------------------------------------
*<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"*
* xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">*
* *
* <!-- PARENT POM.XML -->*
* <parent>*
*  <artifactId>backapps</artifactId>*
*  <groupId>com.krynnlance</groupId>*
*  <version>0.0.1</version>*
* </parent>*
* <modelVersion>4.0.0</modelVersion>*
* <groupId>com.krynnlance</groupId>*
* <artifactId>BackAppsOSGiSevletPOC</artifactId>*
* <packaging>bundle</packaging>*
* <version>1.0</version>*
* <name>BackApps OSGi Servlet Proof of Concept</name>*
* <description></description>*
* <organization>*
* <name>Krynnlance LTD</name>*
* <url>http://www.krynnlance.com</url>*
* </organization>*
* <dependencies>*
*
*
* <!--  JUNIT DEPENDENCY FOR TESTING -->*
* <dependency>*
* <groupId>junit</groupId>*
* <artifactId>junit</artifactId>*
* <version>3.8.2</version>*
* <scope>test</scope>*
* </dependency>*
* *
* <!-- Logging -->*
* <!--<dependency>*
*    <groupId>log4j</groupId>*
*    <artifactId>log4j</artifactId>*
*    <version>1.2.16</version>*
* </dependency>-->*
* *
* <!-- This JAR suppose to be in the war package -->*
* <!-- BackApps Beans Dependencies -->*
*    <dependency>*
* <groupId>com.krynnlance.backapps.common</groupId>*
* <artifactId>BackAppsCommon</artifactId>*
* <version>1.0</version>*
* </dependency>*
*    *
* <!-- SERVLET -->*
* <dependency>*
*  <groupId>org.glassfish</groupId>*
*  <artifactId>javax.servlet</artifactId>*
*  <version>3.1</version>*
* </dependency>*
* *
* <!-- OSGi modules dependencies -->*
* *
* <dependency>*
*    <groupId>org.apache.felix</groupId>*
*    <artifactId>felix</artifactId>*
*    <version>3.2.2</version>*
* </dependency>*
* *
* <!-- HTTP SERVICE -->*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.api</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.base</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.bridge</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.bundle</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.jetty</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.proxy</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <dependency>*
*  <groupId>org.apache.felix</groupId>*
*  <artifactId>org.apache.felix.http.whiteboard</artifactId>*
*  <version>2.0.4</version>*
* </dependency>*
* <!-- END OF HTTP SERVICE -->*
* *
* </dependencies>*
* <build>*
* <resources>*
* <resource>*
* <filtering>false</filtering>*
* <directory>src/main/resources</directory>*
* </resource>*
* <resource>*
* <filtering>false</filtering>*
* <directory>src/main/java</directory>*
* <includes>*
* <include>**</include>*
* </includes>*
* <excludes>*
* <exclude>**/*.java</exclude>*
* </excludes>*
* </resource>*
* </resources>*
* <testResources>*
* <testResource>*
* <filtering>false</filtering>*
* <directory>src/test/java</directory>*
* <includes>*
* <include>**</include>*
* </includes>*
* <excludes>*
* <exclude>**/*.java</exclude>*
* </excludes>*
* </testResource>*
* </testResources>*
* <plugins>*
* <plugin>*
* <inherited>true</inherited>*
* <groupId>org.apache.maven.plugins</groupId>*
* <artifactId>maven-compiler-plugin</artifactId>*
* <configuration>*
* <source>1.5</source>*
* <target>1.5</target>*
* <optimize>true</optimize>*
* <debug>true</debug>*
* </configuration>*
* </plugin>*
* <plugin>*
* <groupId>org.apache.felix</groupId>*
* <artifactId>maven-bundle-plugin</artifactId>*
* <extensions>true</extensions>*
* <configuration>*
*  <instructions>*
*
<!--<Export-Package>com.krynnlance.backapps.common.logging</Export-Package>-->
*
*
<Bundle-Activator>com.krynnlance.backapps.client.servlet.ServletActivator</Bundle-Activator>
*
*  </instructions>*
* </configuration>*
*  </plugin>*
* <plugin>*
* <groupId>org.mortbay.jetty</groupId>*
* <artifactId>maven-jetty-plugin</artifactId>*
* </plugin> *
* </plugins>*
* </build>*
* <properties>*
* <jetty.version>6.1.4</jetty.version>*
* </properties>*
* </project>*
-------------------------------------------------------------------------------------------------------------------------------------------

-- 
Regards

Shai Amar

Technologies manager, Founder
Krynnlance
www.krynnlance.com
www.backapps.com

Re: running servlet in felix OSGi

Posted by Sahoo <sa...@oracle.com>.
GlassFish 3.1 didn't come preinstalled with osgi-http service 
implementation. Pl. use GlassFish 3.1.1 which comes preinstalled with 
osgi-http implementation.

Thanks,
Sahoo
On Tuesday 06 September 2011 09:11 PM, Shai Amar wrote:
> Hi
>
> I'm trying to run a simple servlet in Felix OSGi under Glassfish 3.1
> application server
>
> I've created 3 files:
>
>     1. *BackAppsOSGiSevletPOC*
>     2. *ServletServiceTracker *
>     3. *ServletActivator*
>
> The files are specified below.
>
> Then, after I've installed the module, I've tried to activate the servlet
> via the request *http://localhost:8080/simple* and got nothing.
> I've tried to check and recheck what I've done and came out empty.
>
> Can anyone tell me what I'm missing here?
>
>
> *BackAppsOSGiSevletPOC *with the following code:
>
> -------------------------------------------------------------------------------------------------------------------------------------------
> *package com.krynnlance.backapps.client.servlet;*
> *
> *
> *import java.io.IOException;*
> *
> *
> *import javax.servlet.ServletException;*
> *import javax.servlet.http.HttpServlet;*
> *import javax.servlet.http.HttpServletRequest;*
> *import javax.servlet.http.HttpServletResponse;*
> *
> *
> *public class BackAppsOSGiSevletPOC extends HttpServlet*
> *{*
> *
> *
> * private static final long serialVersionUID = 1L;*
> * *
> * /***
> * * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
> response)*
> * */*
> * protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException *
> * {*
> * response.setContentType("text/plain");*
> * response.getWriter().write("This is the Servlet module [GET request]...");
> *
> * }*
> *
> *
> *}*
> -------------------------------------------------------------------------------------------------------------------------------------------
>
> *ServletServiceTracker* with the following code:
>
> -------------------------------------------------------------------------------------------------------------------------------------------
> *package com.krynnlance.backapps.client.servlet;*
> *
> *
> *import org.osgi.framework.BundleContext;*
> *import org.osgi.framework.ServiceReference;*
> *import org.osgi.service.http.HttpService;*
> *import org.osgi.util.tracker.ServiceTracker;*
> *
> *
> *public class ServletServiceTracker extends ServiceTracker*
> *{*
> * *
> * public ServletServiceTracker(BundleContext context)*
> * {*
> * /***
> * * The service tracker will be registered for a specific class - *
> * * in our case that's org.osgi.service.http.HttpService*
> * */*
> * super(context, HttpService.class.getName(), null);*
> * }*
> * *
> * /***
> * * When the service tracker is asked to return the HttpService, *
> * * we can register our servlet*
> * */*
> * public Object addingService(ServiceReference reference)*
> * {*
> * HttpService httpService = (HttpService)super.addingService(reference);*
> * *
> * if(httpService == null)*
> * {*
> * return null;*
> * }*
> * *
> * try*
> * {*
> * System.out.println("Registering servlet at /simple");*
> * httpService.registerServlet("/simple", new BackAppsOSGiSevletPOC(), null,
> null);*
> * }*
> * catch (Exception e) *
> * {*
> * e.printStackTrace();*
> * }*
> * *
> * return httpService;*
> * }*
> * *
> * /***
> * * Likewise, we must make sure to unregister the servlet when the *
> * * service tracker is asked to shut down the HttpService*
> * */*
> * public void removedService(ServiceReference reference, Object service)*
> * {*
> * HttpService httpService = (HttpService)service;*
> * *
> * System.out.println("Unregistering /simple");*
> * httpService.unregister("/simple");*
> * *
> * super.removedService(reference, service);*
> * }*
> *}*
> -------------------------------------------------------------------------------------------------------------------------------------------
>
> *ServletActivator *with the following code:
>
> -------------------------------------------------------------------------------------------------------------------------------------------
> *package com.krynnlance.backapps.client.servlet;*
> *
> *
> *import org.osgi.framework.BundleActivator;*
> *import org.osgi.framework.BundleContext;*
> *
> *
> *public class ServletActivator implements BundleActivator*
> *{*
> * private ServletServiceTracker servletServiceTracker;*
> * *
> * *
> * @Override*
> * public void start(BundleContext ctx) throws Exception *
> * {*
> * servletServiceTracker = new ServletServiceTracker(ctx);*
> * servletServiceTracker.open();*
> * }*
> *
> *
> * @Override*
> * public void stop(BundleContext ctx) throws Exception *
> * {*
> * servletServiceTracker.close();*
> * servletServiceTracker = null;*
> * }*
> *
> *
> *}*
>
> -------------------------------------------------------------------------------------------------------------------------------------------
>
> And the *pom.xml* woth the following code:
>
> -------------------------------------------------------------------------------------------------------------------------------------------
> *<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance"*
> * xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
> http://maven.apache.org/maven-v4_0_0.xsd">*
> * *
> *<!-- PARENT POM.XML -->*
> *<parent>*
> *<artifactId>backapps</artifactId>*
> *<groupId>com.krynnlance</groupId>*
> *<version>0.0.1</version>*
> *</parent>*
> *<modelVersion>4.0.0</modelVersion>*
> *<groupId>com.krynnlance</groupId>*
> *<artifactId>BackAppsOSGiSevletPOC</artifactId>*
> *<packaging>bundle</packaging>*
> *<version>1.0</version>*
> *<name>BackApps OSGi Servlet Proof of Concept</name>*
> *<description></description>*
> *<organization>*
> *<name>Krynnlance LTD</name>*
> *<url>http://www.krynnlance.com</url>*
> *</organization>*
> *<dependencies>*
> *
> *
> *<!--  JUNIT DEPENDENCY FOR TESTING -->*
> *<dependency>*
> *<groupId>junit</groupId>*
> *<artifactId>junit</artifactId>*
> *<version>3.8.2</version>*
> *<scope>test</scope>*
> *</dependency>*
> * *
> *<!-- Logging -->*
> *<!--<dependency>*
> *<groupId>log4j</groupId>*
> *<artifactId>log4j</artifactId>*
> *<version>1.2.16</version>*
> *</dependency>-->*
> * *
> *<!-- This JAR suppose to be in the war package -->*
> *<!-- BackApps Beans Dependencies -->*
> *<dependency>*
> *<groupId>com.krynnlance.backapps.common</groupId>*
> *<artifactId>BackAppsCommon</artifactId>*
> *<version>1.0</version>*
> *</dependency>*
> *    *
> *<!-- SERVLET -->*
> *<dependency>*
> *<groupId>org.glassfish</groupId>*
> *<artifactId>javax.servlet</artifactId>*
> *<version>3.1</version>*
> *</dependency>*
> * *
> *<!-- OSGi modules dependencies -->*
> * *
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>felix</artifactId>*
> *<version>3.2.2</version>*
> *</dependency>*
> * *
> *<!-- HTTP SERVICE -->*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.api</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.base</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.bridge</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.bundle</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.jetty</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.proxy</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<dependency>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>org.apache.felix.http.whiteboard</artifactId>*
> *<version>2.0.4</version>*
> *</dependency>*
> *<!-- END OF HTTP SERVICE -->*
> * *
> *</dependencies>*
> *<build>*
> *<resources>*
> *<resource>*
> *<filtering>false</filtering>*
> *<directory>src/main/resources</directory>*
> *</resource>*
> *<resource>*
> *<filtering>false</filtering>*
> *<directory>src/main/java</directory>*
> *<includes>*
> *<include>**</include>*
> *</includes>*
> *<excludes>*
> *<exclude>**/*.java</exclude>*
> *</excludes>*
> *</resource>*
> *</resources>*
> *<testResources>*
> *<testResource>*
> *<filtering>false</filtering>*
> *<directory>src/test/java</directory>*
> *<includes>*
> *<include>**</include>*
> *</includes>*
> *<excludes>*
> *<exclude>**/*.java</exclude>*
> *</excludes>*
> *</testResource>*
> *</testResources>*
> *<plugins>*
> *<plugin>*
> *<inherited>true</inherited>*
> *<groupId>org.apache.maven.plugins</groupId>*
> *<artifactId>maven-compiler-plugin</artifactId>*
> *<configuration>*
> *<source>1.5</source>*
> *<target>1.5</target>*
> *<optimize>true</optimize>*
> *<debug>true</debug>*
> *</configuration>*
> *</plugin>*
> *<plugin>*
> *<groupId>org.apache.felix</groupId>*
> *<artifactId>maven-bundle-plugin</artifactId>*
> *<extensions>true</extensions>*
> *<configuration>*
> *<instructions>*
> *
> <!--<Export-Package>com.krynnlance.backapps.common.logging</Export-Package>-->
> *
> *
> <Bundle-Activator>com.krynnlance.backapps.client.servlet.ServletActivator</Bundle-Activator>
> *
> *</instructions>*
> *</configuration>*
> *</plugin>*
> *<plugin>*
> *<groupId>org.mortbay.jetty</groupId>*
> *<artifactId>maven-jetty-plugin</artifactId>*
> *</plugin>  *
> *</plugins>*
> *</build>*
> *<properties>*
> *<jetty.version>6.1.4</jetty.version>*
> *</properties>*
> *</project>*
> -------------------------------------------------------------------------------------------------------------------------------------------
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org