You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/08/13 10:24:15 UTC

svn commit: r803805 - in /camel/trunk/components/camel-servlet: ./ src/main/java/org/apache/camel/component/servlet/ src/test/java/org/apache/camel/component/servlet/ src/test/resources/org/apache/camel/component/servlet/

Author: ningjiang
Date: Thu Aug 13 08:24:15 2009
New Revision: 803805

URL: http://svn.apache.org/viewvc?rev=803805&view=rev
Log:
CAMEL-1910 CamelHttpTransportServlet supports to init the camel context itself

Added:
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java   (with props)
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml   (with props)
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml   (with props)
Modified:
    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/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java

Modified: camel/trunk/components/camel-servlet/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/pom.xml?rev=803805&r1=803804&r2=803805&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/pom.xml (original)
+++ camel/trunk/components/camel-servlet/pom.xml Thu Aug 13 08:24:15 2009
@@ -55,5 +55,21 @@
 	  <scope>test</scope>
     </dependency>
   </dependencies>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <forkMode>pertest</forkMode>
+          <childDelegation>false</childDelegation>
+          <useFile>true</useFile>
+          <includes>
+            <include>**/*Test.*</include>
+          </includes>
+        </configuration>
+      </plugin>
+     </plugins>
+   </build>
     
 </project>

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=803805&r1=803804&r2=803805&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 Thu Aug 13 08:24:15 2009
@@ -26,11 +26,14 @@
 import org.apache.camel.component.http.CamelServlet;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class CamelHttpTransportServlet extends CamelServlet {
     private static final transient Log LOG = LogFactory.getLog(CamelHttpTransportServlet.class);
     private static final Map<String, CamelServlet> CAMEL_SERVLET_MAP = new ConcurrentHashMap<String, CamelServlet>();
     private String servletName;
+    private AbstractApplicationContext applicationContext;
     
     public CamelHttpTransportServlet() {
         super(false);        
@@ -43,6 +46,18 @@
         this.setMatchOnUriPrefix(Boolean.valueOf(matchOnUriPrefix));
         // 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(new String[]{contextConfigLocation});
+        }
+    }
+    
+    public void destroy() {
+        CAMEL_SERVLET_MAP.remove(servletName);
+        if (applicationContext != null) {
+            applicationContext.stop();
+        }
     }
     
     public static CamelServlet getCamelServlet(String servletName) {

Added: 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=803805&view=auto
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java (added)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java Thu Aug 13 08:24:15 2009
@@ -0,0 +1,32 @@
+/**
+ * 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 org.junit.Before;
+
+public class HttpClientRouteSpringTest extends HttpClientRouteTest {
+    @Before
+    public void setUp() throws Exception {
+        startCamelContext = false;
+        super.setUp();
+    }
+   
+    protected String getConfiguration() {
+        return "/org/apache/camel/component/servlet/web-spring.xml";
+    }
+
+}

Propchange: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteSpringTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=803805&r1=803804&r2=803805&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 Thu Aug 13 08:24:15 2009
@@ -54,27 +54,32 @@
         assertEquals("The response message is wrong ", "OK", response.getResponseMessage());
         client.setExceptionsThrownOnErrorStatus(false);
     }
+    
+    public static class MyServletRoute extends RouteBuilder {
+
+        @Override
+        public void configure() throws Exception {
+            errorHandler(noErrorHandler());
+            // START SNIPPET: route
+            from("servlet:///hello").process(new Processor() {
+                public void process(Exchange exchange) throws Exception {
+                    String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
+                    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
+                    assertEquals("Get a wrong content type", CONTENT_TYPE, contentType);
+                    String charsetEncoding = exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
+                    assertEquals("Get a wrong charset name", "UTF-8", charsetEncoding);
+                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");                        
+                    exchange.getOut().setHeader("PATH", path);
+                    exchange.getOut().setBody("<b>Hello World</b>");
+                }
+            });
+            // END SNIPPET: route
+        }
+        
+    }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() {
-                errorHandler(noErrorHandler());
-                // START SNIPPET: route
-                from("servlet:///hello").process(new Processor() {
-                    public void process(Exchange exchange) throws Exception {
-                        String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
-                        String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
-                        assertEquals("Get a wrong content type", CONTENT_TYPE, contentType);
-                        String charsetEncoding = exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
-                        assertEquals("Get a wrong charset name", "UTF-8", charsetEncoding);
-                        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");                        
-                        exchange.getOut().setHeader("PATH", path);
-                        exchange.getOut().setBody("<b>Hello World</b>");
-                    }
-                });
-                // END SNIPPET: route
-            }
-        };
+        return new MyServletRoute();
     }    
   
 

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=803805&r1=803804&r2=803805&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 Thu Aug 13 08:24:15 2009
@@ -28,12 +28,14 @@
 import com.meterware.servletunit.ServletRunner;
 import com.meterware.servletunit.ServletUnitClient;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
 import org.junit.Before;
 
 public class ServletCamelRouterTestSupport extends CamelTestSupport {
     public static final String CONTEXT = "/mycontext";
     public static final String CONTEXT_URL = "http://localhost/mycontext";
     protected ServletRunner sr;
+    protected boolean startCamelContext = true;
 
     @Before
     public void setUp() throws Exception {
@@ -44,8 +46,16 @@
         loadServlets();
         
         HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
-        
-        super.setUp();
+        if (startCamelContext) {        
+            super.setUp();
+        }
+    }
+    
+    @After
+    public void tearDown() throws Exception {
+        if (startCamelContext) {
+            super.tearDown();
+        }
     }
     
     protected void loadServlets() throws Exception {

Added: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml?rev=803805&view=auto
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml (added)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml Thu Aug 13 08:24:15 2009
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:camel="http://camel.apache.org/schema/spring"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+   <camel:camelContext id="camel">
+	  <camel:routeBuilder ref="myServletRouteBuilder"/>	
+   </camel:camelContext>
+   
+   <bean id="myServletRouteBuilder" class="org.apache.camel.component.servlet.HttpClientRouteTest$MyServletRoute" />
+   
+
+</beans>
\ No newline at end of file

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/camelContext.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: 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=803805&view=auto
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml (added)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml Thu Aug 13 08:24:15 2009
@@ -0,0 +1,50 @@
+<?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">
+    
+<!--
+  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.
+-->
+<!-- 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>
+    <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>
+  
+</web-app>
+<!-- END SNIPPET: web -->
\ No newline at end of file

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-spring.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml