camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1076163 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/ camel-servlet/ camel-servlet/src/main/java/org/apache/camel/component/servlet/ camel-servlet/src/main/java/org/apache/camel/component/servlet/o...
Date Wed, 02 Mar 2011 10:17:29 GMT
Author: davsclaus
Date: Wed Mar  2 10:17:28 2011
New Revision: 1076163

URL: http://svn.apache.org/viewvc?rev=1076163&view=rev
Log:
CAMEL-3549: camel-servlet is not agnostic to how CamelContext is started in web apps. For
example you can startup using 100% spring style using a listener.

Added:
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/DefaultCamelServletService.java
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
    camel/trunk/components/camel-servlet/pom.xml
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelServletService.java
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultiServletConsumerTest.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
    camel/trunk/components/camel-servlet/src/test/resources/log4j.properties
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
Wed Mar  2 10:17:28 2011
@@ -18,7 +18,6 @@ package org.apache.camel.component.http;
 
 import java.io.IOException;
 import java.util.concurrent.ConcurrentHashMap;
-
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -50,12 +49,14 @@ public class CamelServlet extends HttpSe
         // Is there a consumer registered for the request.
         HttpConsumer consumer = resolve(request);
         if (consumer == null) {
+            log.debug("No consumer to service request {}", request);
             response.sendError(HttpServletResponse.SC_NOT_FOUND);
             return;
         }
 
         // are we suspended?
         if (consumer.isSuspended()) {
+            log.debug("Consumer suspended, cannot service request {}", request);
             response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
             return;
         }
@@ -117,10 +118,12 @@ public class CamelServlet extends HttpSe
     }
 
     public void connect(HttpConsumer consumer) {
+        log.debug("Connecting consumer: {}", consumer);
         consumers.put(consumer.getPath(), consumer);
     }
 
     public void disconnect(HttpConsumer consumer) {
+        log.debug("Disconnecting consumer: {}", consumer);
         consumers.remove(consumer.getPath());
     }
     

Modified: camel/trunk/components/camel-servlet/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/pom.xml?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/pom.xml (original)
+++ camel/trunk/components/camel-servlet/pom.xml Wed Mar  2 10:17:28 2011
@@ -42,6 +42,7 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
     </dependency>
+    <!-- TODO: Get tid of spring dependency -->
     <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
@@ -88,6 +89,11 @@
       <artifactId>httpunit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-web</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   
   <build>

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
(original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
Wed Mar  2 10:17:28 2011
@@ -16,59 +16,64 @@
  */
 package org.apache.camel.component.servlet;
 
-import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 
 import org.apache.camel.component.http.CamelServlet;
+import org.apache.camel.component.http.HttpConsumer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.context.support.AbstractApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-public class CamelHttpTransportServlet extends CamelServlet implements CamelServletService
{
+/**
+ * Camel HTTP servlet which can be used in Camel routes to route servlet invocations in routes.
+ */
+public class CamelHttpTransportServlet extends CamelServlet {
     private static final transient Logger LOG = LoggerFactory.getLogger(CamelHttpTransportServlet.class);
-    private static final Map<String, CamelServlet> CAMEL_SERVLET_MAP = new ConcurrentHashMap<String,
CamelServlet>();
+    private static final Map<String, CamelServletService> CAMEL_SERVLET_MAP = new ConcurrentHashMap<String,
CamelServletService>();
     private String servletName;
-    private AbstractApplicationContext applicationContext;
-    
+
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
-        servletName = config.getServletName();
-        // parser the servlet init parameters
-        CAMEL_SERVLET_MAP.put(servletName, this);
-        String contextConfigLocation = config.getInitParameter("contextConfigLocation");
-        if (contextConfigLocation != null) {
-            //Create a spring application context for it
-            applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation.split(","));
-            LOG.info("Started the application context rightly");
+        this.servletName = config.getServletName();
+        // do we already know this servlet?
+        CamelServletService service = CAMEL_SERVLET_MAP.get(servletName);
+
+        // we cannot control the startup ordering, sometimes the Camel routes start first
+        // other times the servlet, so we need to cater for both situations
+
+        if (service == null) {
+            // no we don't so create a new early service with this servlet
+            service = new DefaultCamelServletService(servletName, this);
+            CAMEL_SERVLET_MAP.put(servletName, service);
+        } else {
+            // use this servlet
+            service.setCamelServlet(this);
+            // and start the existing consumers we already have registered
+            for (HttpConsumer consumer : service.getConsumers()) {
+                connect(consumer);
+            }
         }
+        LOG.info("Initialized CamelHttpTransportServlet[" + servletName + "]");
     }
     
-    public void destroy() {        
-        if (applicationContext != null) {
-            applicationContext.stop();
-        }
-        // Need to remove the servlet from map after 
-        // the ApplicationContext is removed
+    public void destroy() {
         CAMEL_SERVLET_MAP.remove(servletName);
+        LOG.info("Destroyed CamelHttpTransportServlet[" + servletName + "]");
     }
     
-    public static CamelServlet getCamelServlet(String servletName) {
-        CamelServlet answer = null;
-        if (servletName != null) {
-            answer = CAMEL_SERVLET_MAP.get(servletName);
+    public static synchronized CamelServletService getCamelServletService(String servletName,
HttpConsumer consumer) {
+        // we cannot control the startup ordering, sometimes the Camel routes start first
+        // other times the servlet, so we need to cater for both situations
+
+        CamelServletService answer = CAMEL_SERVLET_MAP.get(servletName);
+        if (answer == null) {
+            answer = new DefaultCamelServletService(servletName, consumer);
+            CAMEL_SERVLET_MAP.put(servletName, answer);
         } else {
-            if (CAMEL_SERVLET_MAP.size() > 0) {
-                // return the first one servlet
-                Iterator<CamelServlet> iterator = CAMEL_SERVLET_MAP.values().iterator();
-                answer = iterator.next();
-                LOG.info("Since no servlet name is specified, using the first element of
camelServlet map [" + answer.getServletName() + "]");
-            }
-        }        
+            answer.addConsumer(consumer);
+        }
         return answer;
     }
 
@@ -76,8 +81,6 @@ public class CamelHttpTransportServlet e
     public String getServletName() {
         return servletName;
     }
-    
-    
-    
+
 }
 

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelServletService.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelServletService.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelServletService.java
(original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelServletService.java
Wed Mar  2 10:17:28 2011
@@ -16,12 +16,56 @@
  */
 package org.apache.camel.component.servlet;
 
+import java.util.Set;
+
+import org.apache.camel.component.http.CamelServlet;
 import org.apache.camel.component.http.HttpConsumer;
 
+/**
+ * Service which binds {@link CamelServlet} to the consumers it should service.
+ */
 public interface CamelServletService {
-    
-    void connect(HttpConsumer consumer); 
 
+    /**
+     * Adds the given consumer to this service.
+     *
+     * @param consumer the consumer
+     */
+    void addConsumer(HttpConsumer consumer);
+
+    /**
+     * Gets the known consumers this service services.
+     *
+     * @return the consumers.
+     */
+    Set<HttpConsumer> getConsumers();
+
+    /**
+     * Sets the servlet to use.
+     *
+     * @param camelServlet the servlet to use.
+     */
+    void setCamelServlet(CamelServlet camelServlet);
+
+    /**
+     * Connect the given consumer to the servlet.
+     *
+     * @param consumer the consumer
+     */
+    void connect(HttpConsumer consumer);
+
+    /**
+     * Disconnects the given consumer from the servlet.
+     *
+     * @param consumer the consumer
+     */
     void disconnect(HttpConsumer consumer);
 
+    /**
+     * Gets the name of the servlet used.
+     *
+     * @return the name of the servlet used.
+     */
+    String getServletName();
+
 }

Added: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/DefaultCamelServletService.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/DefaultCamelServletService.java?rev=1076163&view=auto
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/DefaultCamelServletService.java
(added)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/DefaultCamelServletService.java
Wed Mar  2 10:17:28 2011
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.servlet;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.camel.component.http.CamelServlet;
+import org.apache.camel.component.http.HttpConsumer;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Default {@link CamelServletService}
+ */
+public class DefaultCamelServletService implements CamelServletService {
+
+    private final String servletName;
+    private final Set<HttpConsumer> consumers = new HashSet<HttpConsumer>();
+    private CamelServlet camelServlet;
+
+    public DefaultCamelServletService(String servletName, HttpConsumer consumer) {
+        ObjectHelper.notEmpty(servletName, "ServletName");
+        ObjectHelper.notNull(consumer, "HttpConsumer");
+        this.servletName = servletName;
+        addConsumer(consumer);
+    }
+
+    public DefaultCamelServletService(String servletName, CamelServlet camelServlet) {
+        ObjectHelper.notEmpty(servletName, "ServletName");
+        ObjectHelper.notNull(camelServlet, "CamelServlet");
+        this.servletName = servletName;
+        this.camelServlet = camelServlet;
+    }
+
+    public String getServletName() {
+        return servletName;
+    }
+
+    public void addConsumer(HttpConsumer consumer) {
+        consumers.add(consumer);
+    }
+
+    public void setCamelServlet(CamelServlet camelServlet) {
+        this.camelServlet = camelServlet;
+    }
+
+    public void connect(HttpConsumer consumer) {
+        if (camelServlet != null) {
+            camelServlet.connect(consumer);
+        }
+    }
+
+    public void disconnect(HttpConsumer consumer) {
+        if (camelServlet != null) {
+            camelServlet.disconnect(consumer);
+        }
+    }
+
+    public Set<HttpConsumer> getConsumers() {
+        return Collections.unmodifiableSet(consumers);
+    }
+}

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
(original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
Wed Mar  2 10:17:28 2011
@@ -23,7 +23,6 @@ import java.util.Set;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.component.http.AuthMethod;
-import org.apache.camel.component.http.CamelServlet;
 import org.apache.camel.component.http.HttpBinding;
 import org.apache.camel.component.http.HttpClientConfigurer;
 import org.apache.camel.component.http.HttpComponent;
@@ -37,35 +36,29 @@ import org.apache.commons.httpclient.Htt
 import org.apache.commons.httpclient.params.HttpClientParams;
 
 public class ServletComponent extends HttpComponent {
-    
-    private CamelServlet camelServlet;
-    
-    private CamelServletService camelServletService;
-        
-    public void setCamelServlet(CamelServlet servlet) {
-        camelServlet = servlet;
+
+    private String servletName = "CamelServlet";
+
+    public String getServletName() {
+        return servletName;
     }
-    
-    public void setCamelServletService(CamelServletService service) {
-        camelServletService = service;
+
+    public void setServletName(String servletName) {
+        this.servletName = servletName;
     }
 
-    public CamelServlet getCamelServlet(String servletName) {
-        CamelServlet answer;
-        if (camelServlet == null) {
-            answer = CamelHttpTransportServlet.getCamelServlet(servletName);
-        } else {
-            answer = camelServlet;
-        }
-        if (answer == null) {
-            throw new IllegalArgumentException("Cannot find the deployed servlet, please
configure the ServletComponent"
-                + " or configure a org.apache.camel.component.servlet.CamelHttpTransportServlet
servlet in web.xml ");
+    /**
+     * Strategy to get the {@link CamelServletService} for the given endpoint.
+     *
+     * @param endpoint  the http endpoint.
+     * @return the service
+     */
+    public CamelServletService getCamelServletService(ServletEndpoint endpoint, HttpConsumer
consumer) {
+        CamelServletService service = CamelHttpTransportServlet.getCamelServletService(endpoint.getServletName(),
consumer);
+        if (service == null) {
+            throw new IllegalArgumentException("Servlet: " + getServletName() + " not found.");
         }
-        return answer;
-    }
-    
-    public CamelServletService getCamelServletService() {
-        return camelServletService;
+        return service;
     }
     
     @Override
@@ -85,12 +78,13 @@ public class ServletComponent extends Ht
         Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class);
         HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef",
HttpBinding.class);
         Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix",
Boolean.class);
+        String servletName = getAndRemoveParameter(parameters, "servletName", String.class,
getServletName());
 
         // restructure uri to be based on the parameters left as we dont want to include
the Camel internal options
         URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encode(uri)),
CastUtils.cast(parameters));
         uri = httpUri.toString();
 
-        ServletEndpoint endpoint = createServletEndpoint(uri, this, httpUri, params, getHttpConnectionManager(),
configurer);
+        ServletEndpoint endpoint = createServletEndpoint(servletName, uri, this, httpUri,
params, getHttpConnectionManager(), configurer);
         setEndpointHeaderFilterStrategy(endpoint);
 
         // prefer to use endpoint configured over component configured
@@ -120,34 +114,28 @@ public class ServletComponent extends Ht
         return endpoint;
     }
 
-    protected ServletEndpoint createServletEndpoint(String endpointUri,
+    /**
+     * Strategy to create the servlet endpoint.
+     */
+    protected ServletEndpoint createServletEndpoint(String servletName, String endpointUri,
             ServletComponent component, URI httpUri, HttpClientParams params,
             HttpConnectionManager httpConnectionManager,
             HttpClientConfigurer clientConfigurer) throws Exception {
-        return new ServletEndpoint(endpointUri, component, httpUri, params,
-                httpConnectionManager, clientConfigurer);
+        return new ServletEndpoint(servletName, endpointUri, component, httpUri, params,
httpConnectionManager, clientConfigurer);
     }
     
     public void connect(HttpConsumer consumer) throws Exception {
-        if (getCamelServletService() != null) {
-            getCamelServletService().connect(consumer);
-        } else {
-            ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
-            CamelServlet servlet = getCamelServlet(endpoint.getServletName());
-            ObjectHelper.notNull(servlet, "CamelServlet");
-            servlet.connect(consumer);
-        }
+        ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
+        CamelServletService servlet = getCamelServletService(endpoint, consumer);
+        ObjectHelper.notNull(servlet, "CamelServlet");
+        servlet.connect(consumer);
     }
 
     public void disconnect(HttpConsumer consumer) throws Exception {
-        if (getCamelServletService() != null) {
-            getCamelServletService().disconnect(consumer);
-        } else {
-            ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
-            CamelServlet servlet = getCamelServlet(endpoint.getServletName());
-            ObjectHelper.notNull(servlet, "CamelServlet");
-            servlet.disconnect(consumer);
-        }
+        ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
+        CamelServletService servlet = getCamelServletService(endpoint, consumer);
+        ObjectHelper.notNull(servlet, "CamelServlet");
+        servlet.disconnect(consumer);
     }
 
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java
(original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java
Wed Mar  2 10:17:28 2011
@@ -29,25 +29,27 @@ import org.apache.commons.httpclient.Htt
 import org.apache.commons.httpclient.params.HttpClientParams;
 
 public class ServletEndpoint extends HttpEndpoint {
+
     private String servletName;
-    
+
     public ServletEndpoint() {
         super();
     }
-    
-    public ServletEndpoint(String endPointURI, ServletComponent component, URI httpUri, HttpClientParams
params,
+
+    public ServletEndpoint(String servletName, String endPointURI, ServletComponent component,
URI httpUri, HttpClientParams params,
                            HttpConnectionManager httpConnectionManager, HttpClientConfigurer
clientConfigurer) throws URISyntaxException {
         super(endPointURI, component, httpUri, params, httpConnectionManager, clientConfigurer);
+        this.servletName = servletName;
     }
     
     public void setServletName(String name) {
         servletName = name;
     }
-    
+
     public String getServletName() {
         return servletName;
     }
-    
+
     @Override
     public Producer createProducer() throws Exception {
         return super.createProducer();

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java
(original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java
Wed Mar  2 10:17:28 2011
@@ -18,11 +18,8 @@ package org.apache.camel.component.servl
 
 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;
@@ -34,6 +31,9 @@ import org.springframework.context.Lifec
  * HttpService</a>
  */
 public class OsgiServletRegisterer implements Lifecycle, InitializingBean {
+
+    // TODO: There must be a better way than depend on spring for registering a servlet into
OSGi!
+
     /**
      * 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 

Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
(original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
Wed Mar  2 10:17:28 2011
@@ -19,6 +19,7 @@ package org.apache.camel.component.servl
 import org.junit.Before;
 
 public class HttpClientRouteSpringTest extends HttpClientRouteTest {
+
     @Before
     public void setUp() throws Exception {
         startCamelContext = false;

Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
(original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
Wed Mar  2 10:17:28 2011
@@ -17,7 +17,6 @@
 package org.apache.camel.component.servlet;
 
 import java.io.ByteArrayInputStream;
-
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -25,11 +24,9 @@ import com.meterware.httpunit.PostMethod
 import com.meterware.httpunit.WebRequest;
 import com.meterware.httpunit.WebResponse;
 import com.meterware.servletunit.ServletUnitClient;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class HttpClientRouteTest extends ServletCamelRouterTestSupport {

Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultiServletConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultiServletConsumerTest.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultiServletConsumerTest.java
(original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/MultiServletConsumerTest.java
Wed Mar  2 10:17:28 2011
@@ -21,73 +21,55 @@ import com.meterware.httpunit.HttpNotFou
 import com.meterware.httpunit.WebRequest;
 import com.meterware.httpunit.WebResponse;
 import com.meterware.servletunit.ServletUnitClient;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.http.CamelServlet;
 import org.junit.Test;
 
 public class MultiServletConsumerTest extends ServletCamelRouterTestSupport {
-    /**
-     * @return The web.xml to use for testing.
-     */
+
     protected String getConfiguration() {
         return "/org/apache/camel/component/servlet/multiServletWeb.xml";
     }
-    
-    protected void loadServlets() throws Exception {
+
+    @Test
+    public void testMultiServletsConsumers() throws Exception {
+        String result = getService("/services1/hello?name=Camel");
+        assertEquals("Hello Camel", result);
+
+        result = getService("/services2/echo?name=Camel");
+        assertEquals("Camel Camel", result);
+    }
+
+    @Test
+    public void testMultiServletsConsumersCannotAccessEachOther() throws Exception {
         try {
-            sr.newClient().getResponse(CONTEXT_URL + "/services1");
+            getService("/services2/hello?name=Camel");
+            fail("Should have thrown an exception");
         } catch (HttpNotFoundException e) {
-            // ignore, we just want to boot up the servlet
+            assertEquals(404, e.getResponseCode());
         }
-        
+
         try {
-            sr.newClient().getResponse(CONTEXT_URL + "/services2");
+            getService("/services1/echo?name=Camel");
+            fail("Should have thrown an exception");
         } catch (HttpNotFoundException e) {
-            // ignore, we just want to boot up the servlet
+            assertEquals(404, e.getResponseCode());
         }
     }
-    
-    @Test
-    public void testMultiServletsConsumers() throws Exception {
-        // this bit is needed because the default servlet chosen (when none is specified)
-        // differs in various JDK versions
-        CamelServlet camelServlet = CamelHttpTransportServlet.getCamelServlet(null);
-        String helloServiceServlet = camelServlet.getServletName().contains("2") ? "2" :
"1";
-        
-        String result = getService("/services" + helloServiceServlet + "/hello");
-        assertEquals("Get a wrong response", "/mycontext/services" + helloServiceServlet
+ "/hello", result);
-        
-        result = getService("/services1/echo");
-        assertEquals("Get a wrong response", "/mycontext/services1/echo", result);
-        
-        result = getService("/services2/echo");
-        assertEquals("Get a wrong response", "/mycontext/services2/echo", result);
-    }
-    
+
     public String getService(String path) throws Exception {
         WebRequest req = new GetMethodWebRequest(CONTEXT_URL + path);
         ServletUnitClient client = newClient();
         WebResponse response = client.getResponse(req);
-        
+
         return response.getText();
     }
-    
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
-            Processor echoRequestURIProcessor = new Processor() {
-                public void process(Exchange exchange) throws Exception {
-                    String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
-                    exchange.getOut().setBody(uri);
-                }
-            };
-            
             public void configure() {
-                errorHandler(noErrorHandler());
-                from("servlet:///hello").process(echoRequestURIProcessor);
-                from("servlet:///echo?servletName=CamelServlet1").process(echoRequestURIProcessor);
-                from("servlet:///echo?servletName=CamelServlet2").process(echoRequestURIProcessor);
+                from("servlet:/hello?servletName=CamelServlet1").transform(simple("Hello
${header.name}"));
+
+                from("servlet:/echo?servletName=CamelServlet2").transform(simple("${header.name}
${header.name}"));
             }
         };
     }

Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
(original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
Wed Mar  2 10:17:28 2011
@@ -17,7 +17,7 @@
 package org.apache.camel.component.servlet;
 
 import java.io.InputStream;
-import com.meterware.httpunit.HttpNotFoundException;
+
 import com.meterware.httpunit.HttpUnitOptions;
 import com.meterware.servletunit.ServletRunner;
 import com.meterware.servletunit.ServletUnitClient;
@@ -37,8 +37,6 @@ public class ServletCamelRouterTestSuppo
         assertNotNull("The configuration input stream should not be null", is);
         sr = new ServletRunner(is, CONTEXT);
         
-        loadServlets();
-        
         HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
         if (startCamelContext) {        
             super.setUp();
@@ -53,14 +51,6 @@ public class ServletCamelRouterTestSuppo
         sr.shutDown();
     }
     
-    protected void loadServlets() throws Exception {
-        try {
-            sr.newClient().getResponse(CONTEXT_URL + "/services");
-        } catch (HttpNotFoundException e) {
-            // ignore, we just want to boot up the servlet
-        } 
-    }
-
     /**
      * @return The web.xml to use for testing.
      */
@@ -72,5 +62,4 @@ public class ServletCamelRouterTestSuppo
         return sr.newClient();
     }
 
-
 }

Modified: camel/trunk/components/camel-servlet/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/log4j.properties?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/log4j.properties (original)
+++ camel/trunk/components/camel-servlet/src/test/resources/log4j.properties Wed Mar  2 10:17:28
2011
@@ -21,6 +21,7 @@
 log4j.rootLogger=INFO, file
 
 #log4j.logger.org.apache.camel=DEBUG
+#log4j.logger.org.apache.camel.component.http.CamelServlet=DEBUG
 
 # CONSOLE appender not used by default
 log4j.appender.out=org.apache.log4j.ConsoleAppender

Modified: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
(original)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
Wed Mar  2 10:17:28 2011
@@ -28,17 +28,13 @@
   <servlet>
     <servlet-name>CamelServlet1</servlet-name>
     <display-name>Camel Http Transport Servlet1</display-name>
-    <servlet-class>
-        org.apache.camel.component.servlet.CamelHttpTransportServlet
-    </servlet-class>
+    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
   </servlet>
   
   <servlet>
     <servlet-name>CamelServlet2</servlet-name>
     <display-name>Camel Http Transport Servlet2</display-name>
-    <servlet-class>
-        org.apache.camel.component.servlet.CamelHttpTransportServlet
-    </servlet-class>
+    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
   </servlet>
 
   <servlet-mapping>
@@ -51,5 +47,4 @@
     <url-pattern>/services2/*</url-pattern>
   </servlet-mapping>
 
-
 </web-app>
\ No newline at end of file

Modified: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
(original)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
Wed Mar  2 10:17:28 2011
@@ -1,9 +1,9 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
 
 <!DOCTYPE web-app
-    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-    "http://java.sun.com/dtd/web-app_2_3.dtd">
-    
+        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+        "http://java.sun.com/dtd/web-app_2_3.dtd">
+
 <!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements. See the NOTICE file
@@ -22,33 +22,36 @@
   specific language governing permissions and limitations
   under the License.
 -->
-<!-- START SNIPPET: web -->    
+<!-- START SNIPPET: web -->
 <web-app>
 
-  <servlet>
-    <servlet-name>CamelServlet</servlet-name>
-    <display-name>Camel Http Transport Servlet</display-name>
-    <servlet-class>
-        org.apache.camel.component.servlet.CamelHttpTransportServlet
-    </servlet-class>
-    <init-param> 
-      <param-name>matchOnUriPrefix</param-name> 
-      <param-value>true</param-value>
-    </init-param>
-    <!-- Set the camel context application file location here. 
-         From Camel 2.3.0, you can specify more than one application context configure file

-         which is separated by ','.
-    -->
-    <init-param>
-      <param-name>contextConfigLocation</param-name>
-	  <param-value>/org/apache/camel/component/servlet/camelContext.xml</param-value>
-    </init-param>
-  </servlet>
-
-  <servlet-mapping>
-    <servlet-name>CamelServlet</servlet-name>
-    <url-pattern>/services/*</url-pattern>
-  </servlet-mapping>
-  
+    <!-- tell Spring where it should load the XML file -->
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>classpath:org/apache/camel/component/servlet/camelContext.xml</param-value>
+    </context-param>
+
+    <!-- spring context listener which loads the XML file -->
+    <listener>
+        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+    </listener>
+
+    <!-- Camel Servlet -->
+    <servlet>
+        <servlet-name>CamelServlet</servlet-name>
+        <display-name>Camel Http Transport Servlet</display-name>
+        <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
+        <init-param>
+            <param-name>matchOnUriPrefix</param-name>
+            <param-value>true</param-value>
+        </init-param>
+    </servlet>
+
+    <!-- Camel Servlet mappings -->
+    <servlet-mapping>
+        <servlet-name>CamelServlet</servlet-name>
+        <url-pattern>/services/*</url-pattern>
+    </servlet-mapping>
+
 </web-app>
 <!-- END SNIPPET: web -->
\ No newline at end of file

Modified: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml?rev=1076163&r1=1076162&r2=1076163&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml
(original)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml
Wed Mar  2 10:17:28 2011
@@ -28,10 +28,7 @@
   <servlet>
     <servlet-name>CamelServlet</servlet-name>
     <display-name>Camel Http Transport Servlet</display-name>
-    <servlet-class>
-        org.apache.camel.component.servlet.CamelHttpTransportServlet
-    </servlet-class>
-    
+    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
   </servlet>
 
   <servlet-mapping>



Mime
View raw message