Author: davsclaus
Date: Sun Jan  9 10:51:14 2011
New Revision: 1056911

URL: http://svn.apache.org/viewvc?rev=1056911&view=rev
Log:
CAMEL-3339: Added Main class to camel-core. Thanks to Ben for patch.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Main.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/MainExample.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/MainTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/Main.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Main.java?rev=1056911&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Main.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Main.java Sun Jan  9 
10:51:14 2011
@@ -0,0 +1,82 @@
+/**
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.xml.bind.JAXBException;
+
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.MainSupport;
+import org.apache.camel.view.ModelFileGenerator;
+
+/**
+ * A command line tool for booting up a CamelContext
+ *
+ * @version $Revision: 1025866 $
+ */
+public class Main extends MainSupport {
+    protected static Main instance;
+
+    public Main() {
+    }
+
+    public static void main(String... args) throws Exception {
+        Main main = new Main();
+        instance = main;
+        main.enableHangupSupport();
+        main.run(args);
+    }
+
+    /**
+     * Returns the currently executing main
+     *
+     * @return the current running instance
+     */
+    public static Main getInstance() {
+        return instance;
+    }
+    
+    // Implementation methods
+    // 
-------------------------------------------------------------------------
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        postProcessContext();
+        getCamelContexts().get(0).start();
+    }
+
+    protected void doStop() throws Exception {
+        super.doStop();
+        getCamelContexts().get(0).stop();
+    }
+
+    protected ProducerTemplate findOrCreateCamelTemplate() {
+        return getCamelContexts().get(0).createProducerTemplate();
+    }
+
+    protected Map<String, CamelContext> getCamelContextMap() {
+        Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
+        answer.put("camel-1", new DefaultCamelContext());
+        return answer;
+    }
+
+    protected ModelFileGenerator createModelFileGenerator() throws 
JAXBException {
+        return null;
+    }
+}

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java?rev=1056911&r1=1056910&r2=1056911&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MainSupport.java 
Sun Jan  9 10:51:14 2011
@@ -58,6 +58,28 @@ public abstract class MainSupport extend
     protected final List<CamelContext> camelContexts = new 
ArrayList<CamelContext>();
     protected ProducerTemplate camelTemplate;
 
+    /**
+     * A class for intercepting the hang up signal and do a graceful shutdown 
of the Camel.
+     */
+    private final class HangupInterceptor extends Thread {
+        Log log = LogFactory.getLog(this.getClass());
+        MainSupport mainInstance;
+
+        public HangupInterceptor(MainSupport main) {
+            mainInstance = main;
+        }
+
+        @Override
+        public void run() {
+            log.info("Received hang up - stopping the main instance.");
+            try {
+                mainInstance.stop();
+            } catch (Exception ex) {
+                log.warn(ex);
+            }
+        }
+    }
+
     protected MainSupport() {
         addOption(new Option("h", "help", "Displays the help screen") {
             protected void doProcess(String arg, LinkedList<String> 
remainingArgs) {
@@ -106,7 +128,7 @@ public abstract class MainSupport extend
     }
 
     /**
-     * Runs this process with the given arguments
+     * Runs this process with the given arguments, and will wait until 
completed, or the JVM terminates.
      */
     public void run() throws Exception {
         if (!completed.get()) {
@@ -125,6 +147,15 @@ public abstract class MainSupport extend
     }
 
     /**
+     * Enables the hangup support. Gracefully stops by calling stop() on a
+     * Hangup signal.
+     */
+    public void enableHangupSupport() {
+        HangupInterceptor interceptor = new HangupInterceptor(this);
+        Runtime.getRuntime().addShutdownHook(interceptor);
+    }
+
+    /**
      * Callback to run custom logic after CamelContext has been started
      */
     protected void afterStart() {

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/MainExample.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/MainExample.java?rev=1056911&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/MainExample.java 
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/MainExample.java Sun 
Jan  9 10:51:14 2011
@@ -0,0 +1,63 @@
+/**
+ * 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;
+
+import java.util.Date;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.Main;
+
+/**
+ * @version $Revision: 909028 $
+ */
+// START SNIPPET: e1
+public class MainExample {
+
+    private Main main;
+
+    public static void main(String[] args) throws Exception {
+        MainExample example = new MainExample();
+        example.boot();
+    }
+
+    public void boot() throws Exception {
+        // create a Main instance
+        main = new Main();
+        // enable hangup support so you can press ctrl + c to terminate the JVM
+        main.enableHangupSupport();
+        // add routes
+        main.addRouteBuilder(new MyRouteBuilder());
+
+        // run until you terminate the JVM
+        System.out.println("Starting Camel. Use ctrl + c to terminate the 
JVM.\n");
+        main.run();
+    }
+
+    private static class MyRouteBuilder extends RouteBuilder {
+        @Override
+        public void configure() throws Exception {
+            from("timer:foo?delay=2000")
+                .process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        System.out.println("Invoked timer at " + new Date());
+                    }
+                });
+        }
+    }
+
+}
+// END SNIPPET: e1

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/MainTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/MainTest.java?rev=1056911&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/MainTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/MainTest.java Sun Jan 
 9 10:51:14 2011
@@ -0,0 +1,56 @@
+/**
+ * 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;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.Main;
+
+/**
+ * @version $Revision: 909028 $
+ */
+public class MainTest extends TestCase {
+
+    public void testMain() throws Exception {
+        // lets make a simple route
+        Main main = new Main();
+        main.addRouteBuilder(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("mock:results");
+            }
+        });
+        main.start();
+
+        main.getCamelTemplate().sendBody("direct:start", 
"<message>1</message>");
+        
+        List<CamelContext> contextList = main.getCamelContexts();
+        assertNotNull(contextList);
+        assertEquals("size", 1, contextList.size());
+        CamelContext camelContext = contextList.get(0);
+
+        MockEndpoint endpoint = camelContext.getEndpoint("mock:results", 
MockEndpoint.class);
+        endpoint.expectedMinimumMessageCount(1);
+        endpoint.assertIsSatisfied();
+
+        main.stop();
+    }
+}

Modified: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=1056911&r1=1056910&r2=1056911&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 (original)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 Sun Jan  9 10:51:14 2011
@@ -27,8 +27,6 @@ import org.apache.camel.ProducerTemplate
 import org.apache.camel.impl.MainSupport;
 import org.apache.camel.spring.handler.CamelNamespaceHandler;
 import org.apache.camel.view.ModelFileGenerator;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -66,30 +64,7 @@ public class Main extends MainSupport {
         });
 
     }
-    
-    /**
-     * A class for intercepting the hang up signal and do a graceful shutdown 
of
-     * the Camel.
-     */
-    private class HangupInterceptor extends Thread {
-        Log log = LogFactory.getLog(this.getClass());
-        Main mainInstance;
-
-        public HangupInterceptor(Main main) {
-            mainInstance = main;
-        }
 
-        @Override
-        public void run() {
-            log.info("Received hang up - stopping the main instance.");
-            try {
-                mainInstance.stop();
-            } catch (Exception ex) {
-                log.warn(ex);
-            }
-        }
-    }
-    
     public static void main(String... args) throws Exception {
         Main main = new Main();
         instance = main;
@@ -106,15 +81,6 @@ public class Main extends MainSupport {
         return instance;
     }
     
-    /**
-     * Enables the hangup support. Gracefully stops by calling stop() on a
-     * Hangup signal.
-     */
-    public void enableHangupSupport() {
-        HangupInterceptor interceptor = new HangupInterceptor(this);
-        Runtime.getRuntime().addShutdownHook(interceptor);
-    }
-
     // Properties
     // 
-------------------------------------------------------------------------
     public AbstractApplicationContext getApplicationContext() {


Reply via email to