Author: davsclaus
Date: Thu Jan  3 09:36:18 2013
New Revision: 1428238

URL: http://svn.apache.org/viewvc?rev=1428238&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/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycleSupport.java
   (with props)
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

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=1428238&r1=1428237&r2=1428238&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 09:36:18 2013
@@ -24,12 +24,12 @@ import org.apache.camel.util.jndi.JndiCo
  */
 public interface CamelContextLifecycle {
 
-    void preStart(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
+    void beforeStart(ServletCamelContext camelContext, JndiContext jndi) 
throws Exception;
 
-    void postStart(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
+    void afterStart(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
 
-    void preStop(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
+    void beforeStop(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
 
-    void postStop(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
+    void afterStop(ServletCamelContext camelContext, JndiContext jndi) throws 
Exception;
 
 }

Added: 
camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycleSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycleSupport.java?rev=1428238&view=auto
==============================================================================
--- 
camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycleSupport.java
 (added)
+++ 
camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextLifecycleSupport.java
 Thu Jan  3 09:36:18 2013
@@ -0,0 +1,46 @@
+/**
+ * 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;
+
+/**
+ * Support class for {@link CamelContextLifecycle} to make sub-classing easier,
+ * allowing to only override methods needed.
+ */
+public class CamelContextLifecycleSupport implements CamelContextLifecycle {
+
+    @Override
+    public void beforeStart(ServletCamelContext camelContext, JndiContext 
jndi) throws Exception {
+        // noop
+    }
+
+    @Override
+    public void afterStart(ServletCamelContext camelContext, JndiContext jndi) 
throws Exception {
+        // noop
+    }
+
+    @Override
+    public void beforeStop(ServletCamelContext camelContext, JndiContext jndi) 
throws Exception {
+        // noop
+    }
+
+    @Override
+    public void afterStop(ServletCamelContext camelContext, JndiContext jndi) 
throws Exception {
+        // noop
+    }
+}

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

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

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=1428238&r1=1428237&r2=1428238&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 09:36:18 2013
@@ -17,16 +17,13 @@
 package org.apache.camel.component.servletlistener;
 
 import java.util.Enumeration;
-import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
-import java.util.Set;
 import javax.naming.NamingException;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
-import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
@@ -40,14 +37,10 @@ import org.slf4j.LoggerFactory;
  */
 public class CamelContextServletListener implements ServletContextListener {
 
-    // TODO: Refactor the init method into smaller methods
     // TODO: Allow to lookup and configure some of the stuff we do in 
camel-core-xml
     // more easily with this listener as well
     // TODO: Add more tests
-    // TODO: Lookup jndi and have expected type, and fail if not matching type
-    // TODO: Consider CamelContextLifecycle as a SPI in camel-core to make it 
easier for
-    // everyone to do some custom code when Camel starts/stops. Though you can 
use an EventListener
-    // but maybe this SPI is even easier and more common to use
+    // TODO: Allow to lookup route builders using package scanning
 
     public static ServletCamelContext instance;
 
@@ -61,6 +54,7 @@ public class CamelContextServletListener
     public void contextInitialized(ServletContextEvent sce) {
         LOG.info("CamelContextServletListener initializing ...");
 
+        // create jndi and camel context
         try {
             jndiContext = new JndiContext();
             camelContext = new ServletCamelContext(jndiContext, 
sce.getServletContext());
@@ -69,23 +63,8 @@ public class CamelContextServletListener
             throw new RuntimeException("Error creating CamelContext.", e);
         }
 
-        // 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);
-            }
-        }
+        // get the init parameters
+        Map<String, Object> map = extractInitParameters(sce);
 
         // special for test parameter
         String test = (String) map.remove("test");
@@ -94,6 +73,7 @@ public class CamelContextServletListener
         }
         LOG.trace("In test mode? {}", this.test);
 
+        // set properties on the camel context from the init parameters
         if (!map.isEmpty()) {
             try {
                 IntrospectionSupport.setProperties(camelContext, map);
@@ -102,42 +82,17 @@ public class CamelContextServletListener
             }
         }
 
-        Set<String> routeBuilders = new HashSet<String>();
-        for (Map.Entry<String, Object> entry : map.entrySet()) {
-            if 
(entry.getKey().toLowerCase(Locale.UK).startsWith("routebuilder")) {
-                routeBuilders.add(entry.getKey());
-                String value = (String) entry.getValue();
-                if (ObjectHelper.isNotEmpty(value)) {
-                    Object target;
-                    if (value.startsWith("#")) {
-                        // a reference lookup in jndi
-                        value = value.substring(1);
-                        target = lookupJndi(jndiContext, value);
-                    } else {
-                        try {
-                            Class<RouteBuilder> clazz = 
camelContext.getClassResolver().resolveMandatoryClass(value, 
RouteBuilder.class);
-                            target = 
camelContext.getInjector().newInstance(clazz);
-                        } catch (Exception e) {
-                            throw new RuntimeException("Error creating 
RouteBuilder " + value, e);
-                        }
-                    }
-                    if (target instanceof RouteBuilder) {
-                        LOG.debug("Adding RouteBuilder {} -> {}", 
entry.getKey(), target);
-                        try {
-                            camelContext.addRoutes((RoutesBuilder) target);
-                        } catch (Exception e) {
-                            throw new RuntimeException("Error adding 
RouteBuilder " + value, e);
-                        }
-                    }
-                }
+        // get the route builders and add to the CamelContext
+        Map<String, RouteBuilder> routeBuilders = extractRouteBuilders(map);
+        for (Map.Entry<String, RouteBuilder> entry : routeBuilders.entrySet()) 
{
+            LOG.debug("Adding RouteBuilder {} -> {}", entry.getKey(), 
entry.getValue());
+            try {
+                camelContext.addRoutes(entry.getValue());
+            } catch (Exception e) {
+                throw new RuntimeException("Error adding RouteBuilder " + 
entry.getKey(), e);
             }
         }
 
-        // after adding the route builders we should remove them from the map
-        for (String name : routeBuilders) {
-            map.remove(name);
-        }
-
         // any custom CamelContextLifecycle
         String lifecycle = (String) 
map.remove(CamelContextLifecycle.class.getName());
         if (lifecycle != null) {
@@ -157,11 +112,11 @@ public class CamelContextServletListener
 
         try {
             if (camelContextLifecycle != null) {
-                camelContextLifecycle.preStart(camelContext, jndiContext);
+                camelContextLifecycle.beforeStart(camelContext, jndiContext);
             }
             camelContext.start();
             if (camelContextLifecycle != null) {
-                camelContextLifecycle.postStart(camelContext, jndiContext);
+                camelContextLifecycle.afterStart(camelContext, jndiContext);
             }
         } catch (Exception e) {
             LOG.error("Error starting CamelContext.", e);
@@ -175,6 +130,64 @@ public class CamelContextServletListener
         LOG.info("CamelContextServletListener initialized");
     }
 
+    private Map<String, RouteBuilder> extractRouteBuilders(Map<String, Object> 
map) {
+        Map<String, RouteBuilder> routeBuilders = new LinkedHashMap<String, 
RouteBuilder>();
+        for (Map.Entry<String, Object> entry : map.entrySet()) {
+            if 
(entry.getKey().toLowerCase(Locale.UK).startsWith("routebuilder")) {
+                String value = (String) entry.getValue();
+                if (ObjectHelper.isNotEmpty(value)) {
+                    Object target;
+                    if (value.startsWith("#")) {
+                        // a reference lookup in jndi
+                        value = value.substring(1);
+                        target = lookupJndi(jndiContext, value);
+                    } else {
+                        try {
+                            Class<RouteBuilder> clazz = 
camelContext.getClassResolver().resolveMandatoryClass(value, 
RouteBuilder.class);
+                            target = 
camelContext.getInjector().newInstance(clazz);
+                        } catch (Exception e) {
+                            throw new RuntimeException("Error creating 
RouteBuilder " + value, e);
+                        }
+                    }
+                    if (target instanceof RouteBuilder) {
+                        routeBuilders.put(entry.getKey(), (RouteBuilder) 
target);
+                    } else {
+                        throw new IllegalArgumentException("Parameter " + 
entry.getKey() + " is expected to be a " + RouteBuilder.class.getName()
+                                + " type but was " + 
target.getClass().getName() + " type.");
+                    }
+                }
+            }
+        }
+
+        // after adding the route builders we should remove them from the map
+        for (String name : routeBuilders.keySet()) {
+            map.remove(name);
+        }
+
+        return routeBuilders;
+    }
+
+    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);
@@ -189,11 +202,11 @@ public class CamelContextServletListener
         if (camelContext != null) {
             try {
                 if (camelContextLifecycle != null) {
-                    camelContextLifecycle.preStop(camelContext, jndiContext);
+                    camelContextLifecycle.beforeStop(camelContext, 
jndiContext);
                 }
                 camelContext.stop();
                 if (camelContextLifecycle != null) {
-                    camelContextLifecycle.postStop(camelContext, jndiContext);
+                    camelContextLifecycle.afterStop(camelContext, jndiContext);
                 }
             } catch (Exception e) {
                 LOG.warn("Error stopping CamelContext. This exception will be 
ignored.", e);


Reply via email to