Christian I think you must put the osgi code in a sub package, eg

org.apache.camel.component.servlet.osgi

This ensures the classloader in non osgi environments will not barf
about not able to find OSGI JARs.
We do the same in camel-core, see the Activator class in the core, as
its in a .osgi sub package.


On Sun, Feb 6, 2011 at 10:28 AM,  <[email protected]> wrote:
> Author: cschneider
> Date: Sun Feb  6 09:28:38 2011
> New Revision: 1067621
>
> URL: http://svn.apache.org/viewvc?rev=1067621&view=rev
> Log:
> CAMEL-3490 Add OsgiServletRegisterer to make it easier for users to add the 
> camel servlet in osgi
>
> Added:
>    
> camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/OsgiServletRegisterer.java
> Modified:
>    camel/trunk/components/camel-servlet/pom.xml
>
> Modified: camel/trunk/components/camel-servlet/pom.xml
> URL: 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/pom.xml?rev=1067621&r1=1067620&r2=1067621&view=diff
> ==============================================================================
> --- camel/trunk/components/camel-servlet/pom.xml (original)
> +++ camel/trunk/components/camel-servlet/pom.xml Sun Feb  6 09:28:38 2011
> @@ -19,25 +19,42 @@
>   <properties>
>       
> <camel.osgi.export.pkg>org.apache.camel.component.servlet.*</camel.osgi.export.pkg>
>   </properties>
> -
> +
>   <dependencies>
>     <dependency>
> +      <groupId>org.apache.felix</groupId>
> +      <artifactId>org.osgi.compendium</artifactId>
> +      <version>1.2.0</version>
> +      <scope>provided</scope>
> +      <exclusions>
> +        <exclusion>
> +          <groupId>org.apache.felix</groupId>
> +          <artifactId>javax.servlet</artifactId>
> +        </exclusion>
> +        <exclusion>
> +          <groupId>org.apache.felix</groupId>
> +          <artifactId>org.osgi.foundation</artifactId>
> +        </exclusion>
> +      </exclusions>
> +    </dependency>
> +
> +    <dependency>
>       <groupId>org.apache.camel</groupId>
> -      <artifactId>camel-core</artifactId>
> +      <artifactId>camel-core</artifactId>
>     </dependency>
>     <dependency>
>       <groupId>org.apache.camel</groupId>
> -      <artifactId>camel-spring</artifactId>
> +      <artifactId>camel-spring</artifactId>
>     </dependency>
>     <dependency>
>       <groupId>org.apache.camel</groupId>
> -      <artifactId>camel-http</artifactId>
> +      <artifactId>camel-http</artifactId>
>     </dependency>
> -
> +
>     <dependency>
>       <groupId>org.apache.camel</groupId>
>       <artifactId>camel-test</artifactId>
> -      <scope>test</scope>
> +      <scope>test</scope>
>     </dependency>
>     <dependency>
>       <groupId>junit</groupId>
> @@ -55,9 +72,9 @@
>       <scope>test</scope>
>     </dependency>
>     <dependency>
> -         <groupId>httpunit</groupId>
> -         <artifactId>httpunit</artifactId>
> -         <scope>test</scope>
> +      <groupId>httpunit</groupId>
> +      <artifactId>httpunit</artifactId>
> +      <scope>test</scope>
>     </dependency>
>   </dependencies>
>
>
> Added: 
> camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/OsgiServletRegisterer.java
> URL: 
> http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/OsgiServletRegisterer.java?rev=1067621&view=auto
> ==============================================================================
> --- 
> camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/OsgiServletRegisterer.java
>  (added)
> +++ 
> camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/OsgiServletRegisterer.java
>  Sun Feb  6 09:28:38 2011
> @@ -0,0 +1,81 @@
> +package org.apache.camel.component.servlet;
> +
> +import java.util.Dictionary;
> +import java.util.Hashtable;
> +
> +import javax.servlet.Servlet;
> +import javax.servlet.http.HttpServlet;
> +
> +import org.apache.camel.component.servlet.CamelHttpTransportServlet;
> +import org.osgi.service.http.HttpContext;
> +import org.osgi.service.http.HttpService;
> +import org.springframework.beans.factory.InitializingBean;
> +import org.springframework.context.Lifecycle;
> +
> +/**
> + * Register the given (CamelHttpTransport) Servlet with the OSGI
> + * <a 
> href="http://www.osgi.org/javadoc/r4v42/org/osgi/service/http/HttpService.html";>
> + * HttpService</a>
> + */
> +public class OsgiServletRegisterer implements Lifecycle, InitializingBean {
> +    /**
> +     * The alias is the name in the URI namespace of the Http Service at 
> which the registration will be mapped
> +     * An alias must begin with slash ('/') and must not end with slash 
> ('/'), with the exception that an alias
> +     * of the form "/" is used to denote the root alias.
> +     */
> +    private String alias;
> +
> +    /**
> +     * Servlet to be registered
> +     */
> +    private HttpServlet servlet;
> +
> +    /**
> +     * HttpService to register with. Get this with osgi:reference in the 
> spring
> +     * context
> +     */
> +    private HttpService httpService;
> +
> +    private boolean alreadyRegistered;
> +
> +    public void setHttpService(HttpService httpService) {
> +        this.httpService = httpService;
> +    }
> +    public void setAlias(String alias) {
> +        this.alias = alias;
> +    }
> +
> +    public void setServlet(HttpServlet servlet) {
> +        this.servlet = servlet;
> +    }
> +
> +    @Override
> +    public void afterPropertiesSet() throws Exception {
> +        final HttpContext httpContext = 
> httpService.createDefaultHttpContext();
> +        final Dictionary<String, String> initParams = new Hashtable<String, 
> String>();
> +        // The servlet will always have to match on uri prefix as some 
> endpoints may do so
> +        initParams.put("matchOnUriPrefix", "true");
> +        initParams.put("servlet-name", servlet.getServletName());
> +
> +        httpService.registerServlet(alias, servlet, initParams, httpContext);
> +        alreadyRegistered = true;
> +    }
> +
> +    @Override
> +    public void start() {
> +    }
> +
> +    @Override
> +    public void stop() {
> +        if (alreadyRegistered) {
> +            httpService.unregister(alias);
> +            alreadyRegistered = false;
> +        }
> +    }
> +
> +    @Override
> +    public boolean isRunning() {
> +        return alreadyRegistered;
> +    }
> +
> +}
>
>
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: [email protected]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Reply via email to