camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1428278 - in /camel/trunk/components/camel-servletlistener/src: main/java/org/apache/camel/component/servletlistener/ test/java/org/apache/camel/component/servletlistener/ test/resources/
Date Thu, 03 Jan 2013 11:08:15 GMT
Author: davsclaus
Date: Thu Jan  3 11:08:15 2013
New Revision: 1428278

URL: http://svn.apache.org/viewvc?rev=1428278&view=rev
Log:
CAMEL-5906: Added new camel-servletlistener component for bootstrapping Camel in web app without
using spring etc. Work in progress.

Added:
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/HelloBean.java
  (with props)
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/LifecycleTest.java
      - copied, changed from r1428272, camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyBeanRoute.java
      - copied, changed from r1428215, camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyRoute.java
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
  (with props)
    camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml
      - copied, changed from r1428215, camel/trunk/components/camel-servletlistener/src/test/resources/myweb.xml
Modified:
    camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycle.java
    camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java
    camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/ServletCamelContext.java

Modified: camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycle.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycle.java?rev=1428278&r1=1428277&r2=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycle.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycle.java
Thu Jan  3 11:08:15 2013
@@ -24,12 +24,40 @@ import org.apache.camel.util.jndi.JndiCo
  */
 public interface CamelContextLifecycle {
 
+    /**
+     * Callback before starting {@link ServletCamelContext}.
+     *
+     * @param camelContext the Camel context
+     * @param jndi         the JNDI context.
+     * @throws Exception is thrown if any error.
+     */
     void beforeStart(ServletCamelContext camelContext, JndiContext jndi) throws Exception;
 
+    /**
+     * Callback after {@link ServletCamelContext} has been started.
+     *
+     * @param camelContext the Camel context
+     * @param jndi         the JNDI context.
+     * @throws Exception is thrown if any error.
+     */
     void afterStart(ServletCamelContext camelContext, JndiContext jndi) throws Exception;
 
+    /**
+     * Callback before stopping {@link ServletCamelContext}.
+     *
+     * @param camelContext the Camel context
+     * @param jndi         the JNDI context.
+     * @throws Exception is thrown if any error.
+     */
     void beforeStop(ServletCamelContext camelContext, JndiContext jndi) throws Exception;
 
+    /**
+     * Callback after {@link ServletCamelContext} has been stopped.
+     *
+     * @param camelContext the Camel context
+     * @param jndi         the JNDI context.
+     * @throws Exception is thrown if any error.
+     */
     void afterStop(ServletCamelContext camelContext, JndiContext jndi) throws Exception;
 
 }

Modified: camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java?rev=1428278&r1=1428277&r2=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java
Thu Jan  3 11:08:15 2013
@@ -60,7 +60,6 @@ public class CamelContextServletListener
     private boolean test;
 
     @Override
-    @SuppressWarnings("unchecked")
     public void contextInitialized(ServletContextEvent sce) {
         LOG.info("CamelContextServletListener initializing ...");
 
@@ -128,7 +127,7 @@ public class CamelContextServletListener
         }
 
         // any custom CamelContextLifecycle
-        String lifecycle = (String) map.remove(CamelContextLifecycle.class.getName());
+        String lifecycle = (String) map.remove("CamelContextLifecycle");
         if (lifecycle != null) {
             try {
                 Class<CamelContextLifecycle> clazz = camelContext.getClassResolver().resolveMandatoryClass(lifecycle,
CamelContextLifecycle.class);
@@ -164,6 +163,55 @@ public class CamelContextServletListener
         LOG.info("CamelContextServletListener initialized");
     }
 
+    @Override
+    public void contextDestroyed(ServletContextEvent sce) {
+        LOG.info("CamelContextServletListener destroying ...");
+        if (camelContext != null) {
+            try {
+                if (camelContextLifecycle != null) {
+                    camelContextLifecycle.beforeStop(camelContext, jndiContext);
+                }
+                camelContext.stop();
+                if (camelContextLifecycle != null) {
+                    camelContextLifecycle.afterStop(camelContext, jndiContext);
+                }
+            } catch (Exception e) {
+                LOG.warn("Error stopping CamelContext. This exception will be ignored.",
e);
+            }
+        }
+        camelContext = null;
+        jndiContext = null;
+        instance = null;
+        LOG.info("CamelContextServletListener destroyed");
+    }
+
+    private Map<String, Object> extractInitParameters(ServletContextEvent sce) {
+        // configure CamelContext with the init parameter
+        Map<String, Object> map = new LinkedHashMap<String, Object>();
+        Enumeration names = sce.getServletContext().getInitParameterNames();
+        while (names.hasMoreElements()) {
+            String name = (String) names.nextElement();
+            String value = sce.getServletContext().getInitParameter(name);
+
+            if (ObjectHelper.isNotEmpty(value)) {
+                Object target = value;
+                if (value.startsWith("#")) {
+                    // a reference lookup in jndi
+                    value = value.substring(1);
+                    target = lookupJndi(jndiContext, value);
+                }
+                map.put(name, target);
+            }
+        }
+        return map;
+    }
+
+    /**
+     * Extract the routes from the parameters.
+     *
+     * @param map  parameters
+     * @return a list of routes, which can be of different types. See source code for more
details.
+     */
     private List<Object> extractRoutes(Map<String, Object> map) {
         List<Object> answer = new ArrayList<Object>();
         List<String> names = new ArrayList<String>();
@@ -237,27 +285,6 @@ public class CamelContextServletListener
         return answer;
     }
 
-    private Map<String, Object> extractInitParameters(ServletContextEvent sce) {
-        // configure CamelContext with the init parameter
-        Map<String, Object> map = new LinkedHashMap<String, Object>();
-        Enumeration names = sce.getServletContext().getInitParameterNames();
-        while (names.hasMoreElements()) {
-            String name = (String) names.nextElement();
-            String value = sce.getServletContext().getInitParameter(name);
-
-            if (ObjectHelper.isNotEmpty(value)) {
-                Object target = value;
-                if (value.startsWith("#")) {
-                    // a reference lookup in jndi
-                    value = value.substring(1);
-                    target = lookupJndi(jndiContext, value);
-                }
-                map.put(name, target);
-            }
-        }
-        return map;
-    }
-
     private static Object lookupJndi(JndiContext jndiContext, String name) {
         try {
             return jndiContext.lookup(name);
@@ -266,26 +293,4 @@ public class CamelContextServletListener
         }
     }
 
-    @Override
-    public void contextDestroyed(ServletContextEvent sce) {
-        LOG.info("CamelContextServletListener destroying ...");
-        if (camelContext != null) {
-            try {
-                if (camelContextLifecycle != null) {
-                    camelContextLifecycle.beforeStop(camelContext, jndiContext);
-                }
-                camelContext.stop();
-                if (camelContextLifecycle != null) {
-                    camelContextLifecycle.afterStop(camelContext, jndiContext);
-                }
-            } catch (Exception e) {
-                LOG.warn("Error stopping CamelContext. This exception will be ignored.",
e);
-            }
-        }
-        camelContext = null;
-        jndiContext = null;
-        instance = null;
-        LOG.info("CamelContextServletListener destroyed");
-    }
-
 }

Modified: camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/ServletCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/ServletCamelContext.java?rev=1428278&r1=1428277&r2=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/ServletCamelContext.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/ServletCamelContext.java
Thu Jan  3 11:08:15 2013
@@ -26,10 +26,20 @@ import org.apache.camel.impl.DefaultCame
  */
 public class ServletCamelContext extends DefaultCamelContext {
 
+    private final Context jndiContext;
     private final ServletContext servletContext;
 
     public ServletCamelContext(Context jndiContext, ServletContext servletContext) {
         super(jndiContext);
+        this.jndiContext = jndiContext;
         this.servletContext = servletContext;
     }
+
+    public Context getJndiContext() {
+        return jndiContext;
+    }
+
+    public ServletContext getServletContext() {
+        return servletContext;
+    }
 }

Added: camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/HelloBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/HelloBean.java?rev=1428278&view=auto
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/HelloBean.java
(added)
+++ camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/HelloBean.java
Thu Jan  3 11:08:15 2013
@@ -0,0 +1,27 @@
+/**
+ * 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.servletlistener;
+
+/**
+ *
+ */
+public class HelloBean {
+
+    public String hello(String name) {
+        return "Hello " + name;
+    }
+}

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

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

Copied: camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/LifecycleTest.java
(from r1428272, camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/LifecycleTest.java?p2=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/LifecycleTest.java&p1=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java&r1=1428272&r2=1428278&rev=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/LifecycleTest.java
Thu Jan  3 11:08:15 2013
@@ -24,10 +24,10 @@ import org.junit.Test;
 /**
  *
  */
-public class SingleRouteTest extends ServletCamelTestSupport {
+public class LifecycleTest extends ServletCamelTestSupport {
 
     protected String getConfiguration() {
-        return "/myweb.xml";
+        return "/myweb5.xml";
     }
 
     @Test
@@ -40,9 +40,9 @@ public class SingleRouteTest extends Ser
         ProducerTemplate template = context.createProducerTemplate();
 
         MockEndpoint mock = context.getEndpoint("mock:foo", MockEndpoint.class);
-        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World");
 
-        template.sendBody("seda:foo", "Hello World");
+        template.sendBody("seda:foo", "World");
 
         mock.assertIsSatisfied();
         template.stop();

Copied: camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyBeanRoute.java
(from r1428215, camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyRoute.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyBeanRoute.java?p2=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyBeanRoute.java&p1=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyRoute.java&r1=1428215&r2=1428278&rev=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyRoute.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyBeanRoute.java
Thu Jan  3 11:08:15 2013
@@ -21,11 +21,12 @@ import org.apache.camel.builder.RouteBui
 /**
  *
  */
-public class MyRoute extends RouteBuilder {
+public class MyBeanRoute extends RouteBuilder {
 
     @Override
     public void configure() throws Exception {
         from("seda:foo").routeId("foo")
+            .to("bean:myBean")
             .to("mock:foo");
     }
 }

Added: camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java?rev=1428278&view=auto
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
(added)
+++ camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
Thu Jan  3 11:08:15 2013
@@ -0,0 +1,33 @@
+/**
+ * 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.servletlistener;
+
+import org.apache.camel.util.jndi.JndiContext;
+
+/**
+ *
+ */
+// START SNIPPET: e1
+public class MyLifecycle extends CamelContextLifecycleSupport {
+
+    @Override
+    public void beforeStart(ServletCamelContext camelContext, JndiContext jndi) throws Exception
{
+        // enlist our bean(s) in the registry
+        jndi.bind("myBean", new HelloBean());
+    }
+}
+// END SNIPPET: e1

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

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

Copied: camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml (from r1428215,
camel/trunk/components/camel-servletlistener/src/test/resources/myweb.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml?p2=camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml&p1=camel/trunk/components/camel-servletlistener/src/test/resources/myweb.xml&r1=1428215&r2=1428278&rev=1428278&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/resources/myweb.xml (original)
+++ camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml Thu Jan  3
11:08:15 2013
@@ -41,7 +41,12 @@
   <!-- Camel will pickup any parameter names that start with routeBuilder (case ignored)
-->
   <context-param>
     <param-name>routeBuilder-MyRoute</param-name>
-    <param-value>org.apache.camel.component.servletlistener.MyRoute</param-value>
+    <param-value>org.apache.camel.component.servletlistener.MyBeanRoute</param-value>
+  </context-param>
+  <!-- configure our lifecycle class, which allows us to do custom logic before/after
starting CamelContext -->
+  <context-param>
+    <param-name>CamelContextLifecycle</param-name>
+    <param-value>org.apache.camel.component.servletlistener.MyLifecycle</param-value>
   </context-param>
 
   <listener>



Mime
View raw message