camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r627405 - in /activemq/camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/ main/java/org/apache/camel/spring/util/ test/java/org/apache/camel/spring/
Date Wed, 13 Feb 2008 13:25:29 GMT
Author: jstrachan
Date: Wed Feb 13 05:25:28 2008
New Revision: 627405

URL: http://svn.apache.org/viewvc?rev=627405&view=rev
Log:
added a helper class for easier building of Camel routes reusing the Main and a SimpleRouteBuilder

Added:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java
  (with props)
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java
  (with props)
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=627405&r1=627404&r2=627405&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
(original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
Wed Feb 13 05:25:28 2008
@@ -16,7 +16,16 @@
  */
 package org.apache.camel.spring;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.view.RouteDotGenerator;
@@ -25,14 +34,6 @@
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-
 /**
  * A command line tool for booting up a CamelContext using an optional Spring
  * ApplicationContext
@@ -49,6 +50,8 @@
     private long duration = -1;
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
     private String dotOutputDir;
+    private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
+    private SpringCamelContext camelContext;
 
     public Main() {
         addOption(new Option("h", "help", "Displays the help screen") {
@@ -84,7 +87,6 @@
         new Main().run(args);
     }
 
-
     /**
      * Parses the command line arguments then runs the program
      */
@@ -100,7 +102,6 @@
         if (!completed.get()) {
             try {
                 start();
-                postProcessContext();
                 waitUntilCompleted();
                 stop();
             }
@@ -118,6 +119,10 @@
         latch.countDown();
     }
 
+    public void addRouteBuilder(RouteBuilder routeBuilder) {
+        getRouteBuilders().add(routeBuilder);
+    }
+
     /**
      * Displays the command line options
      */
@@ -239,6 +244,10 @@
         this.applicationContextUri = applicationContextUri;
     }
 
+    public SpringCamelContext getCamelContext() {
+        return camelContext;
+    }
+
     public long getDuration() {
         return duration;
     }
@@ -277,6 +286,14 @@
         this.dotOutputDir = dotOutputDir;
     }
 
+    public List<RouteBuilder> getRouteBuilders() {
+        return routeBuilders;
+    }
+
+    public void setRouteBuilders(List<RouteBuilder> routeBuilders) {
+        this.routeBuilders = routeBuilders;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
     protected void doStart() throws Exception {
@@ -285,6 +302,8 @@
             applicationContext = createDefaultApplicationContext();
         }
         applicationContext.start();
+
+        postProcessContext();
     }
 
     protected AbstractApplicationContext createDefaultApplicationContext() {
@@ -320,7 +339,7 @@
     }
 
     protected void postProcessContext() throws Exception {
-        CamelContext camelContext = SpringCamelContext.springCamelContext(applicationContext);
+        camelContext = SpringCamelContext.springCamelContext(applicationContext);
         if (ObjectHelper.isNotNullAndNonEmpty(dotOutputDir)) {
             RouteDotGenerator generator = new RouteDotGenerator(dotOutputDir);
             LOG.info("Generating DOT file for routes: " + dotOutputDir + " for: " + camelContext);
@@ -329,7 +348,10 @@
         postProcesCamelContext(camelContext);
     }
 
-    protected void postProcesCamelContext(CamelContext camelContext) {
+    protected void postProcesCamelContext(CamelContext camelContext) throws Exception {
+        for (RouteBuilder routeBuilder : routeBuilders) {
+            camelContext.addRoutes(routeBuilder);
+        }
     }
 
     protected String getVersion() {

Added: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java?rev=627405&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java
(added)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java
Wed Feb 13 05:25:28 2008
@@ -0,0 +1,146 @@
+/**
+ *
+ * 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.spring.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.ProcessorType;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * A simple {@link RouteBuilder} which can be configured directly with one or more from URIs,
zero or more to URIs
+ * and an optional bean processing step.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class SimpleRouteBuilder extends RouteBuilder {
+    private List<String> fromUris = new ArrayList<String>();
+    private List<String> toUris = new ArrayList<String>();
+    private Class beanType;
+    private String beanClass;
+    private String beanRef;
+    private String beanMethod;
+
+    public void configure() throws Exception {
+        if (fromUris.isEmpty()) {
+            throw new IllegalArgumentException("the fromUris property must contain at least
one valid URI");
+        }
+        for (String fromUri : fromUris) {
+            ProcessorType route = from(fromUri);
+
+            addBeanCall(route);
+            for (String toUri : toUris) {
+                route = route.to(toUri);
+            }
+        }
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public void setFromUri(String uri) {
+        setFromUris(singletonList(uri));
+    }
+
+    public void setToUri(String uri) {
+        setToUris(singletonList(uri));
+    }
+
+    public List<String> getFromUris() {
+        return fromUris;
+    }
+
+    public void setFromUris(List<String> fromUris) {
+        this.fromUris = fromUris;
+    }
+
+    public List<String> getToUris() {
+        return toUris;
+    }
+
+    public void setToUris(List<String> toUris) {
+        this.toUris = toUris;
+    }
+
+    public String getBeanClass() {
+        return beanClass;
+    }
+
+    public void setBeanClass(String beanClass) {
+        this.beanClass = beanClass;
+    }
+
+    public String getBeanRef() {
+        return beanRef;
+    }
+
+    public void setBeanRef(String beanRef) {
+        this.beanRef = beanRef;
+    }
+
+    public Class getBeanType() {
+        if (beanType == null) {
+            if (beanClass != null) {
+                beanType = ObjectHelper.loadClass(beanClass, getClass().getClassLoader());
+            }
+        }
+        return beanType;
+    }
+
+    public void setBeanType(Class beanType) {
+        this.beanType = beanType;
+    }
+
+    public String getBeanMethod() {
+        return beanMethod;
+    }
+
+    public void setBeanMethod(String beanMethod) {
+        this.beanMethod = beanMethod;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+    protected void addBeanCall(ProcessorType route) {
+        Class type = getBeanType();
+        if (type != null) {
+            if (beanMethod != null) {
+                route = route.bean(type, beanMethod);
+            }
+            else {
+                route = route.bean(type);
+            }
+        }
+        else if (beanRef != null) {
+            if (beanMethod != null) {
+                route = route.beanRef(beanRef, beanMethod);
+            }
+            else {
+                route = route.beanRef(beanRef);
+            }
+        }
+    }
+
+    protected List<String> singletonList(String value) {
+        List<String> uris = new ArrayList<String>();
+        uris.add(value);
+        return uris;
+    }
+}

Propchange: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/util/SimpleRouteBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java?rev=627405&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java
(added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java
Wed Feb 13 05:25:28 2008
@@ -0,0 +1,43 @@
+/**
+ *
+ * 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.spring;
+
+import junit.framework.TestCase;
+import org.apache.camel.spring.util.SimpleRouteBuilder;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class MainExampleTest extends TestCase {
+    public void testMain() throws Exception {
+        // lets make a simple route
+        SimpleRouteBuilder builder = new SimpleRouteBuilder();
+        builder.setFromUri("file://src/test/data?noop=true");
+        builder.setBeanClass("org.apache.camel.spring.example.MyProcessor");
+        builder.setToUri("file://target/mainTest");
+
+        Main main = new Main();
+        main.addRouteBuilder(builder);
+        main.start();
+
+        // then some time later
+        Thread.sleep(3000);
+        main.stop();
+
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainExampleTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java?rev=627405&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
(added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
Wed Feb 13 05:25:28 2008
@@ -0,0 +1,59 @@
+/**
+ *
+ * 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.spring;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+import org.apache.camel.spring.util.SimpleRouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.Exchange;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class MainTest extends TestCase {
+    private static final transient Log LOG = LogFactory.getLog(MainTest.class);
+
+    public void testMain() throws Exception {
+        // lets make a simple route
+        SimpleRouteBuilder builder = new SimpleRouteBuilder();
+        builder.setFromUri("file://src/test/data?noop=true");
+        builder.setBeanClass("org.apache.camel.spring.example.MyProcessor");
+        builder.setToUri("mock:results");
+
+        Main main = new Main();
+        main.addRouteBuilder(builder);
+        main.start();
+
+        SpringCamelContext camelContext = main.getCamelContext();
+        assertNotNull(camelContext);
+
+        MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
+        endpoint.expectedMessageCount(2);
+        endpoint.assertIsSatisfied();
+        List<Exchange> list = endpoint.getReceivedExchanges();
+        
+        LOG.debug("Received: " + list);
+
+        main.stop();
+
+    }
+}

Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message