camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1428336 - in /camel/trunk/components/camel-servletlistener/src: main/java/org/apache/camel/component/servletlistener/ test/java/org/apache/camel/component/servletlistener/ test/resources/ test/resources/routes/
Date Thu, 03 Jan 2013 13:25:47 GMT
Author: davsclaus
Date: Thu Jan  3 13:25:46 2013
New Revision: 1428336

URL: http://svn.apache.org/viewvc?rev=1428336&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/PropertyPlaceholderRouteTest.java
      - copied, changed from r1428319, camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java
    camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
  (with props)
    camel/trunk/components/camel-servletlistener/src/test/resources/myweb6.xml
      - copied, changed from r1428319, camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml
    camel/trunk/components/camel-servletlistener/src/test/resources/routes/myOtherRoute.xml
      - copied, changed from r1428319, camel/trunk/components/camel-servletlistener/src/test/resources/routes/myRoutes.xml
Removed:
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/CamelContextServletListenerTest.java
Modified:
    camel/trunk/components/camel-servletlistener/src/main/java/org/apache/camel/component/servletlistener/CamelContextServletListener.java
    camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java

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=1428336&r1=1428335&r2=1428336&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 13:25:46 2013
@@ -31,9 +31,12 @@ import javax.servlet.ServletContextEvent
 import javax.servlet.ServletContextListener;
 
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.ErrorHandlerBuilderRef;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
@@ -59,6 +62,10 @@ public class CamelContextServletListener
     private CamelContextLifecycle camelContextLifecycle;
     private boolean test;
 
+    // TODO: Maybe some way of having CamelContextLifecycleSupport using bean parameter binding
+    // so ppl can build their Camel app without camel-servletlistener dependency on the codebase
+    // TODO: add example and add documentation page
+
     @Override
     public void contextInitialized(ServletContextEvent sce) {
         LOG.info("CamelContextServletListener initializing ...");
@@ -68,7 +75,6 @@ public class CamelContextServletListener
             jndiContext = new JndiContext();
             camelContext = new ServletCamelContext(jndiContext, sce.getServletContext());
         } catch (Exception e) {
-            LOG.error("Error creating CamelContext.", e);
             throw new RuntimeException("Error creating CamelContext.", e);
         }
 
@@ -83,12 +89,14 @@ public class CamelContextServletListener
         LOG.trace("In test mode? {}", this.test);
 
         // set properties on the camel context from the init parameters
-        if (!map.isEmpty()) {
-            try {
+        try {
+            initPropertyPlaceholder(camelContext, map);
+            initCamelContext(camelContext, map);
+            if (!map.isEmpty()) {
                 IntrospectionSupport.setProperties(camelContext, map);
-            } catch (Exception e) {
-                throw new RuntimeException("Error setting init parameters on CamelContext.",
e);
             }
+        } catch (Exception e) {
+            throw new RuntimeException("Error setting init parameters on CamelContext.",
e);
         }
 
         // get the routes and add to the CamelContext
@@ -185,6 +193,10 @@ public class CamelContextServletListener
         LOG.info("CamelContextServletListener destroyed");
     }
 
+    /**
+     * Extracts all the init parameters, and will do reference lookup in {@link JndiContext}
+     * if the value starts with a # sign.
+     */
     private Map<String, Object> extractInitParameters(ServletContextEvent sce) {
         // configure CamelContext with the init parameter
         Map<String, Object> map = new LinkedHashMap<String, Object>();
@@ -207,6 +219,72 @@ public class CamelContextServletListener
     }
 
     /**
+     * Initializes the property placeholders by registering the {@link PropertiesComponent}
with
+     * the configuration from the given init parameters.
+     */
+    private void initPropertyPlaceholder(ServletCamelContext camelContext, Map<String,
Object> parameters) throws Exception {
+        // setup property placeholder first
+        Map<String, Object> properties = IntrospectionSupport.extractProperties(parameters,
"propertyPlaceholder.");
+        if (properties != null && !properties.isEmpty()) {
+            PropertiesComponent pc = new PropertiesComponent();
+            IntrospectionSupport.setProperties(pc, properties);
+            // validate we could set all parameters
+            if (!properties.isEmpty()) {
+                throw new IllegalArgumentException("Error setting propertyPlaceholder parameters
on CamelContext."
+                        + " There are " + properties.size() + " unknown parameters. [" +
properties + "]");
+            }
+            // register the properties component
+            camelContext.addComponent("properties", pc);
+        }
+    }
+
+    /**
+     * Initializes the {@link ServletCamelContext} by setting the supported init parameters.
+     */
+    private void initCamelContext(ServletCamelContext camelContext, Map<String, Object>
parameters) throws Exception {
+        String streamCache = (String) parameters.remove("streamCache");
+        if (streamCache != null) {
+            camelContext.setStreamCaching(CamelContextHelper.parseBoolean(camelContext, streamCache));
+        }
+        String trace = (String) parameters.remove("trace");
+        if (trace != null) {
+            camelContext.setTracing(CamelContextHelper.parseBoolean(camelContext, trace));
+        }
+        String delayer = (String) parameters.remove("delayer");
+        if (delayer != null) {
+            camelContext.setDelayer(CamelContextHelper.parseLong(camelContext, delayer));
+        }
+        String handleFault = (String) parameters.remove("handleFault");
+        if (handleFault != null) {
+            camelContext.setHandleFault(CamelContextHelper.parseBoolean(camelContext, handleFault));
+        }
+        String errorHandlerRef = (String) parameters.remove("errorHandlerRef");
+        if (errorHandlerRef != null) {
+            camelContext.setErrorHandlerBuilder(new ErrorHandlerBuilderRef(errorHandlerRef));
+        }
+        String autoStartup = (String) parameters.remove("autoStartup");
+        if (autoStartup != null) {
+            camelContext.setAutoStartup(CamelContextHelper.parseBoolean(camelContext, autoStartup));
+        }
+        String useMDCLogging = (String) parameters.remove("useMDCLogging");
+        if (useMDCLogging != null) {
+            camelContext.setUseMDCLogging(CamelContextHelper.parseBoolean(camelContext, useMDCLogging));
+        }
+        String useBreadcrumb = (String) parameters.remove("useBreadcrumb");
+        if (useBreadcrumb != null) {
+            camelContext.setUseBreadcrumb(CamelContextHelper.parseBoolean(camelContext, useBreadcrumb));
+        }
+        String managementNamePattern = (String) parameters.remove("managementNamePattern");
+        if (managementNamePattern != null) {
+            camelContext.getManagementNameStrategy().setNamePattern(managementNamePattern);
+        }
+        String threadNamePattern = (String) parameters.remove("threadNamePattern");
+        if (threadNamePattern != null) {
+            camelContext.getExecutorServiceManager().setThreadNamePattern(threadNamePattern);
+        }
+    }
+
+    /**
      * Extract the routes from the parameters.
      *
      * @param map  parameters

Modified: 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=1428336&r1=1428335&r2=1428336&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
(original)
+++ camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/MyLifecycle.java
Thu Jan  3 13:25:46 2013
@@ -18,10 +18,13 @@ package org.apache.camel.component.servl
 
 import org.apache.camel.util.jndi.JndiContext;
 
+// START SNIPPET: e1
 /**
- *
+ * Our custom {@link CamelContextLifecycle} which allows us to enlist beans in the {@link
JndiContext}
+ * so the Camel application can lookup the beans in the {@link org.apache.camel.spi.Registry}.
+ * <p/>
+ * We can of course also do other kind of custom logic as well.
  */
-// START SNIPPET: e1
 public class MyLifecycle extends CamelContextLifecycleSupport {
 
     @Override
@@ -29,5 +32,11 @@ public class MyLifecycle extends CamelCo
         // enlist our bean(s) in the registry
         jndi.bind("myBean", new HelloBean());
     }
+
+    @Override
+    public void afterStop(ServletCamelContext camelContext, JndiContext jndi) throws Exception
{
+        // unbind our bean when Camel has been stopped
+        jndi.unbind("myBean");
+    }
 }
 // END SNIPPET: e1

Copied: camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/PropertyPlaceholderRouteTest.java
(from r1428319, 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/PropertyPlaceholderRouteTest.java?p2=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/PropertyPlaceholderRouteTest.java&p1=camel/trunk/components/camel-servletlistener/src/test/java/org/apache/camel/component/servletlistener/SingleRouteTest.java&r1=1428319&r2=1428336&rev=1428336&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/PropertyPlaceholderRouteTest.java
Thu Jan  3 13:25:46 2013
@@ -24,10 +24,10 @@ import org.junit.Test;
 /**
  *
  */
-public class SingleRouteTest extends ServletCamelTestSupport {
+public class PropertyPlaceholderRouteTest extends ServletCamelTestSupport {
 
     protected String getConfiguration() {
-        return "/myweb.xml";
+        return "/myweb6.xml";
     }
 
     @Test
@@ -39,10 +39,10 @@ public class SingleRouteTest extends Ser
 
         ProducerTemplate template = context.createProducerTemplate();
 
-        MockEndpoint mock = context.getEndpoint("mock:foo", MockEndpoint.class);
-        mock.expectedMessageCount(1);
+        MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class);
+        mock.expectedBodiesReceived("Hello World");
 
-        template.sendBody("seda:foo", "Hello World");
+        template.sendBody("direct:foo", "World");
 
         mock.assertIsSatisfied();
         template.stop();

Added: camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties?rev=1428336&view=auto
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
(added)
+++ camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
Thu Jan  3 13:25:46 2013
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+greeting=Hello ${body}
+end=mock:result

Propchange: camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-servletlistener/src/test/resources/myproperties.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: camel/trunk/components/camel-servletlistener/src/test/resources/myweb6.xml (from r1428319,
camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/resources/myweb6.xml?p2=camel/trunk/components/camel-servletlistener/src/test/resources/myweb6.xml&p1=camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml&r1=1428319&r2=1428336&rev=1428336&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/resources/myweb5.xml (original)
+++ camel/trunk/components/camel-servletlistener/src/test/resources/myweb6.xml Thu Jan  3
13:25:46 2013
@@ -37,16 +37,23 @@
     <param-value>MyCamel</param-value>
   </context-param>
 
+  <!-- setup property placeholder to load properties from classpath -->
+  <!-- we do this by setting the param-name with propertyPlaceholder. as prefix and then
any options such as location, cache etc -->
+  <context-param>
+    <param-name>propertyPlaceholder.location</param-name>
+    <param-value>classpath:myproperties.properties</param-value>
+  </context-param>
+  <!-- for example to disable cache on properties component, you do -->
+  <context-param>
+    <param-name>propertyPlaceholder.cache</param-name>
+    <param-value>false</param-value>
+  </context-param>
+
   <!-- configure a route builder to use -->
   <!-- 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.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>
+    <param-value>classpath:routes/myOtherRoute.xml</param-value>
   </context-param>
 
   <listener>

Copied: camel/trunk/components/camel-servletlistener/src/test/resources/routes/myOtherRoute.xml
(from r1428319, camel/trunk/components/camel-servletlistener/src/test/resources/routes/myRoutes.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servletlistener/src/test/resources/routes/myOtherRoute.xml?p2=camel/trunk/components/camel-servletlistener/src/test/resources/routes/myOtherRoute.xml&p1=camel/trunk/components/camel-servletlistener/src/test/resources/routes/myRoutes.xml&r1=1428319&r2=1428336&rev=1428336&view=diff
==============================================================================
--- camel/trunk/components/camel-servletlistener/src/test/resources/routes/myRoutes.xml (original)
+++ camel/trunk/components/camel-servletlistener/src/test/resources/routes/myOtherRoute.xml
Thu Jan  3 13:25:46 2013
@@ -19,12 +19,10 @@
 
   <route id="foo">
     <from uri="direct:foo"/>
-    <to uri="mock:foo"/>
-  </route>
-
-  <route id="bar">
-    <from uri="direct:bar"/>
-    <to uri="mock:bar"/>
+    <transform>
+      <simple>{{greeting}}</simple>
+    </transform>
+    <to uri="{{end}}"/>
   </route>
 
 </routes>



Mime
View raw message