camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r704951 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/util/ camel-core/src/main/java/org/apache/camel/view/ components/camel-guice/src/main/java/org/apache/camel/guice/ components/camel-spring/src/main/java/org/apach...
Date Wed, 15 Oct 2008 16:26:00 GMT
Author: jstrachan
Date: Wed Oct 15 09:25:56 2008
New Revision: 704951

URL: http://svn.apache.org/viewvc?rev=704951&view=rev
Log:
added a refactor of the Main in camel-spring so that it can be more easily reused in other contexts; such as when using Guice to perform IoC of Camel for https://issues.apache.org/activemq/browse/CAMEL-989

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java   (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java   (contents, props changed)
      - copied, changed from r704911, activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ModelFileGenerator.java
    activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java   (contents, props changed)
      - copied, changed from r704911, activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
Removed:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ModelFileGenerator.java
Modified:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java?rev=704951&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java Wed Oct 15 09:25:56 2008
@@ -0,0 +1,490 @@
+/**
+ *
+ * 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.util;
+
+import org.apache.camel.impl.ServiceSupport;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.processor.interceptor.Debugger;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.view.RouteDotGenerator;
+import org.apache.camel.view.ModelFileGenerator;
+import org.apache.camel.model.RouteType;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.bind.JAXBException;
+import java.util.LinkedList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.io.IOException;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public abstract class MainSupport extends ServiceSupport {
+    protected static final Log LOG = LogFactory.getLog(MainSupport.class);
+    private List<Option> options = new ArrayList<Option>();
+    private CountDownLatch latch = new CountDownLatch(1);
+    private AtomicBoolean completed = new AtomicBoolean(false);
+    private long duration = -1;
+    private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
+    protected String dotOutputDir;
+    private String routesOutputFile;
+    private boolean aggregateDot;
+    private boolean debug;
+    private boolean trace;
+    private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
+    private List<CamelContext> camelContexts = new ArrayList<CamelContext>();
+    private ProducerTemplate camelTemplate;
+
+    protected MainSupport() {
+        addOption(new Option("h", "help", "Displays the help screen") {
+            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+                showOptions();
+                completed();
+            }
+        });
+        addOption(new ParameterOption("o", "outdir",
+                "Sets the DOT output directory where the visual representations of the routes are generated",
+                "dot") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setDotOutputDir(parameter);
+            }
+        });
+        addOption(new ParameterOption("ad", "aggregate-dot",
+                "Aggregates all routes (in addition to individual route generation) into one context to create one monolithic DOT file for visual representations the entire system.",
+                "aggregate-dot") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setAggregateDot("true".equals(parameter));
+            }
+        });
+        addOption(new ParameterOption("d", "duration",
+                "Sets the time duration that the applicaiton will run for, by default in milliseconds. You can use '10s' for 10 seconds etc",
+                "duration") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                String value = parameter.toUpperCase();
+                if (value.endsWith("S")) {
+                    value = value.substring(0, value.length() - 1);
+                    setTimeUnit(TimeUnit.SECONDS);
+                }
+                setDuration(Integer.parseInt(value));
+            }
+        });
+
+        addOption(new Option("x", "debug", "Enables the debugger") {
+            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+                enableDebug();
+            }
+        });
+        addOption(new Option("t", "trace", "Enables tracing") {
+            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+                enableTrace();
+            }
+        });
+        addOption(new ParameterOption("out", "output", "Output all routes to the specified XML file", "filename") {
+            protected void doProcess(String arg, String parameter,
+                    LinkedList<String> remainingArgs) {
+                setRoutesOutputFile(parameter);
+            }
+        });
+    }
+
+    /**
+     * Runs this process with the given arguments
+     */
+    public void run() {
+        if (!completed.get()) {
+            try {
+                start();
+                waitUntilCompleted();
+                stop();
+            } catch (Exception e) {
+                LOG.error("Failed: " + e, e);
+            }
+        }
+    }
+
+    /**
+     * Marks this process as being completed
+     */
+    public void completed() {
+        completed.set(true);
+        latch.countDown();
+    }
+
+    /**
+     * Displays the command line options
+     */
+    public void showOptions() {
+        showOptionsHeader();
+
+        for (Option option : options) {
+            System.out.println(option.getInformation());
+        }
+    }
+
+    /**
+     * Parses the command line arguments
+     */
+    public void parseArguments(String[] arguments) {
+        LinkedList<String> args = new LinkedList<String>(Arrays.asList(arguments));
+
+        boolean valid = true;
+        while (!args.isEmpty()) {
+            String arg = args.removeFirst();
+
+            boolean handled = false;
+            for (Option option : options) {
+                if (option.processOption(arg, args)) {
+                    handled = true;
+                    break;
+                }
+            }
+            if (!handled) {
+                System.out.println("Unknown option: " + arg);
+                System.out.println();
+                valid = false;
+                break;
+            }
+        }
+        if (!valid) {
+            showOptions();
+            completed();
+        }
+    }
+
+    public void addOption(Option option) {
+        options.add(option);
+    }
+
+    public long getDuration() {
+        return duration;
+    }
+
+    /**
+     * Sets the duration to run the application for in milliseconds until it
+     * should be terminated. Defaults to -1. Any value <= 0 will run forever.
+     *
+     * @param duration
+     */
+    public void setDuration(long duration) {
+        this.duration = duration;
+    }
+
+    public TimeUnit getTimeUnit() {
+        return timeUnit;
+    }
+
+    /**
+     * Sets the time unit duration
+     */
+    public void setTimeUnit(TimeUnit timeUnit) {
+        this.timeUnit = timeUnit;
+    }
+
+    public String getDotOutputDir() {
+        return dotOutputDir;
+    }
+
+    /**
+     * Sets the output directory of the generated DOT Files to show the visual
+     * representation of the routes. A null value disables the dot file
+     * generation
+     */
+    public void setDotOutputDir(String dotOutputDir) {
+        this.dotOutputDir = dotOutputDir;
+    }
+
+    public void setAggregateDot(boolean aggregateDot) {
+        this.aggregateDot = aggregateDot;
+    }
+
+    public boolean isAggregateDot() {
+        return aggregateDot;
+    }
+
+    public boolean isDebug() {
+        return debug;
+    }
+
+    public void enableDebug() {
+        this.debug = true;
+    }
+
+    public boolean isTrace() {
+        return trace;
+    }
+
+    public void enableTrace() {
+        this.trace = true;
+    }
+
+    public void setRoutesOutputFile(String routesOutputFile) {
+        this.routesOutputFile = routesOutputFile;
+    }
+
+    public String getRoutesOutputFile() {
+        return routesOutputFile;
+    }
+
+    /**
+     * Returns the currently active debugger if one is enabled
+     *
+     * @return the current debugger or null if none is active
+     * @see #enableDebug()
+     */
+    public Debugger getDebugger() {
+        for (CamelContext camelContext : camelContexts) {
+            Debugger debugger = Debugger.getDebugger(camelContext);
+            if (debugger != null) {
+                return debugger;
+            }
+        }
+        return null;
+    }
+
+    protected void doStart() throws Exception {
+        LOG.info("Apache Camel " + getVersion() + " starting");
+    }
+
+    protected void waitUntilCompleted() {
+        while (!completed.get()) {
+            try {
+                if (duration > 0) {
+                    TimeUnit unit = getTimeUnit();
+                    LOG.info("Waiting for: " + duration + " " + unit);
+                    latch.await(duration, unit);
+                    completed.set(true);
+                } else {
+                    latch.await();
+                }
+            } catch (InterruptedException e) {
+                LOG.debug("Caught: " + e);
+            }
+        }
+    }
+
+    protected String getVersion() {
+        Package aPackage = Package.getPackage("org.apache.camel");
+        if (aPackage != null) {
+            String version = aPackage.getImplementationVersion();
+            if (version == null) {
+                version = aPackage.getSpecificationVersion();
+                if (version == null) {
+                    version = "";
+                }
+            }
+            return version;
+        }
+        return "";
+    }
+
+    /**
+     * Parses the command line arguments then runs the program
+     */
+    public void run(String[] args) {
+        parseArguments(args);
+        run();
+    }
+
+    /**
+     * Displays the header message for the command line options
+     */
+    public void showOptionsHeader() {
+      System.out.println("Apache Camel Runner takes the following options");
+      System.out.println();
+    }
+
+    public List<CamelContext> getCamelContexts() {
+        return camelContexts;
+    }
+
+    public List<RouteBuilder> getRouteBuilders() {
+        return routeBuilders;
+    }
+
+    public void setRouteBuilders(List<RouteBuilder> routeBuilders) {
+        this.routeBuilders = routeBuilders;
+    }
+
+    public List<RouteType> getRouteDefinitions() {
+        List<RouteType> answer = new ArrayList<RouteType>();
+        for (CamelContext camelContext : camelContexts) {
+            answer.addAll(camelContext.getRouteDefinitions());
+        }
+        return answer;
+    }
+
+    /**
+     * Returns a {@link org.apache.camel.ProducerTemplate} from the Spring {@link org.springframework.context.ApplicationContext} instances
+     * or lazily creates a new one dynamically
+     */
+    public ProducerTemplate getCamelTemplate() {
+        if (camelTemplate == null) {
+            camelTemplate = findOrCreateCamelTemplate();
+        }
+        return camelTemplate;
+    }
+
+    protected abstract ProducerTemplate findOrCreateCamelTemplate();
+
+    protected abstract Map<String,CamelContext> getCamelContextMap();
+
+    protected void postProcessContext() throws Exception {
+        Map<String, CamelContext> map = getCamelContextMap();
+        Set<Map.Entry<String, CamelContext>> entries = map.entrySet();
+        int size = entries.size();
+        for (Map.Entry<String, CamelContext> entry : entries) {
+            String name = entry.getKey();
+            CamelContext camelContext = entry.getValue();
+            camelContexts.add(camelContext);
+            generateDot(name, camelContext, size);
+            postProcesCamelContext(camelContext);
+        }
+
+        if (isAggregateDot()) {
+            generateDot("aggregate", aggregateCamelContext(), 1);
+        }
+
+        if (!"".equals(getRoutesOutputFile())) {
+            outputRoutesToFile();
+        }
+    }
+
+    protected void outputRoutesToFile() throws IOException, JAXBException {
+        if (ObjectHelper.isNotNullAndNonEmpty(getRoutesOutputFile())) {
+            LOG.info("Generating routes as XML in the file named: " + getRoutesOutputFile());
+            ModelFileGenerator generator = createModelFileGenerator();
+            generator.marshalRoutesUsingJaxb(getRoutesOutputFile(), getRouteDefinitions());
+        }
+    }
+
+    protected abstract ModelFileGenerator createModelFileGenerator() throws JAXBException;
+
+    protected void generateDot(String name, CamelContext camelContext, int size) throws IOException {
+        String outputDir = dotOutputDir;
+        if (ObjectHelper.isNotNullAndNonEmpty(outputDir)) {
+            if (size > 1) {
+                outputDir += "/" + name;
+            }
+            RouteDotGenerator generator = new RouteDotGenerator(outputDir);
+            LOG.info("Generating DOT file for routes: " + outputDir + " for: " + camelContext + " with name: " + name);
+            generator.drawRoutes(camelContext);
+        }
+    }
+
+    /**
+     * Used for aggregate dot generation, generate a single camel context containing all of the available contexts
+     */
+    private CamelContext aggregateCamelContext() throws Exception {
+        if (camelContexts.size() == 1) {
+            return camelContexts.get(0);
+        }
+        else {
+            DefaultCamelContext answer = new DefaultCamelContext();
+            for (CamelContext camelContext : camelContexts) {
+                answer.addRouteDefinitions(camelContext.getRouteDefinitions());
+            }
+            return answer;
+        }
+    }
+
+    protected void postProcesCamelContext(CamelContext camelContext) throws Exception {
+        for (RouteBuilder routeBuilder : routeBuilders) {
+            camelContext.addRoutes(routeBuilder);
+        }
+    }
+
+    public void addRouteBuilder(RouteBuilder routeBuilder) {
+        getRouteBuilders().add(routeBuilder);
+    }
+
+    public abstract class Option {
+        private String abbreviation;
+        private String fullName;
+        private String description;
+
+        protected Option(String abbreviation, String fullName, String description) {
+            this.abbreviation = "-" + abbreviation;
+            this.fullName = "-" + fullName;
+            this.description = description;
+        }
+
+        public boolean processOption(String arg, LinkedList<String> remainingArgs) {
+            if (arg.equalsIgnoreCase(abbreviation) || fullName.startsWith(arg)) {
+                doProcess(arg, remainingArgs);
+                return true;
+            }
+            return false;
+        }
+
+        public String getAbbreviation() {
+            return abbreviation;
+        }
+
+        public String getDescription() {
+            return description;
+        }
+
+        public String getFullName() {
+            return fullName;
+        }
+
+        public String getInformation() {
+            return "  " + getAbbreviation() + " or " + getFullName() + " = " + getDescription();
+        }
+
+        protected abstract void doProcess(String arg, LinkedList<String> remainingArgs);
+    }
+
+    public abstract class ParameterOption extends Option {
+        private String parameterName;
+
+        protected ParameterOption(String abbreviation, String fullName, String description,
+                String parameterName) {
+            super(abbreviation, fullName, description);
+            this.parameterName = parameterName;
+        }
+
+        protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+            if (remainingArgs.isEmpty()) {
+                System.err.println("Expected fileName for ");
+                showOptions();
+                completed();
+            } else {
+                String parameter = remainingArgs.removeFirst();
+                doProcess(arg, parameter, remainingArgs);
+            }
+        }
+
+        public String getInformation() {
+            return "  " + getAbbreviation() + " or " + getFullName()
+                    + " <" + parameterName + "> = " + getDescription();
+        }
+
+        protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MainSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java (from r704911, activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ModelFileGenerator.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java?p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java&p1=activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ModelFileGenerator.java&r1=704911&r2=704951&rev=704951&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/ModelFileGenerator.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java Wed Oct 15 09:25:56 2008
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.spring.handler;
+package org.apache.camel.view;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -26,6 +26,8 @@
 import java.util.Properties;
 
 import javax.xml.bind.JAXBException;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Binder;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
@@ -48,9 +50,15 @@
 import org.apache.camel.util.ObjectHelper;
 
 
-public class ModelFileGenerator extends CamelNamespaceHandler {
+public class ModelFileGenerator {
 
     private static final String DEFAULT_ROOT_ELEMENT_NAME = "routes";
+    private final JAXBContext jaxbContext;
+    private Binder<Node> binder;
+
+    public ModelFileGenerator(JAXBContext jaxbContext) {
+        this.jaxbContext = jaxbContext;
+    }
 
     /**
      * Write the specified 'routeTypes' to 'fileName' as XML using JAXB.
@@ -108,7 +116,9 @@
      */
     private void addJaxbElementToNode(Node node, Object jaxbElement) {
         try {
-            binder = getJaxbContext().createBinder();
+            if (binder == null) {
+                binder = jaxbContext.createBinder();
+            }
             binder.marshal(jaxbElement, node);
         } catch (JAXBException e) {
             throw new RuntimeCamelException(e);

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/ModelFileGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java (from r704911, 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-guice/src/main/java/org/apache/camel/guice/Main.java?p2=activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java&p1=activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java&r1=704911&r2=704951&rev=704951&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java (original)
+++ activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java Wed Oct 15 09:25:56 2008
@@ -14,129 +14,47 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.spring;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
+package org.apache.camel.guice;
 
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
+import com.google.inject.Binding;
+import com.google.inject.Injector;
+import com.google.inject.Key;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.ServiceSupport;
-import org.apache.camel.model.RouteType;
-import org.apache.camel.processor.interceptor.Debugger;
-import org.apache.camel.spring.handler.ModelFileGenerator;
-import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.view.RouteDotGenerator;
-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;
-import org.springframework.context.support.FileSystemXmlApplicationContext;
+import org.apache.camel.view.ModelFileGenerator;
+import org.apache.camel.util.MainSupport;
+import org.guiceyfruit.Injectors;
+
+import javax.naming.InitialContext;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import java.util.Map;
+import java.util.Set;
 
 /**
- * A command line tool for booting up a CamelContext using an optional Spring
- * ApplicationContext
+ * A command line tool for booting up a CamelContext using a Guice Injector via JNDI
+ * assuming that a valid jndi.properties is on the classpath
  *
  * @version $Revision$
  */
-public class Main extends ServiceSupport {
-    private static final Log LOG = LogFactory.getLog(Main.class);
+public class Main extends MainSupport {
     private static Main instance;
+    private InitialContext context;
+    private Injector injector;
 
-    private String applicationContextUri = "META-INF/spring/*.xml";
-    private String fileApplicationContextUri;
-    private AbstractApplicationContext applicationContext;
-    private List<Option> options = new ArrayList<Option>();
-    private CountDownLatch latch = new CountDownLatch(1);
-    private AtomicBoolean completed = new AtomicBoolean(false);
-    private long duration = -1;
-    private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
-    private String dotOutputDir;
-    private String routesOutputFile;
-    private boolean aggregateDot;
-    private boolean debug;
-    private boolean trace;
-    private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
-    private List<SpringCamelContext> camelContexts = new ArrayList<SpringCamelContext>();
-    private AbstractApplicationContext parentApplicationContext;
-    private String parentApplicationContextUri;
-    private ProducerTemplate camelTemplate;
 
     public Main() {
-        addOption(new Option("h", "help", "Displays the help screen") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                showOptions();
-                completed();
-            }
-        });
 
+/*
         addOption(new ParameterOption("a", "applicationContext",
                 "Sets the classpath based spring ApplicationContext", "applicationContext") {
             protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
                 setApplicationContextUri(parameter);
             }
         });
-
-        addOption(new ParameterOption("fa", "fileApplicationContext",
-                "Sets the filesystem based spring ApplicationContext", "fileApplicationContext") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setFileApplicationContextUri(parameter);
-            }
-        });
-
-        addOption(new ParameterOption("o", "outdir",
-                "Sets the DOT output directory where the visual representations of the routes are generated",
-                "dot") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setDotOutputDir(parameter);
-            }
-        });
-        addOption(new ParameterOption("ad", "aggregate-dot",
-                "Aggregates all routes (in addition to individual route generation) into one context to create one monolithic DOT file for visual representations the entire system.",
-                "aggregate-dot") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setAggregateDot("true".equals(parameter));
-            }
-        });
-        addOption(new ParameterOption("d", "duration",
-                "Sets the time duration that the applicaiton will run for, by default in milliseconds. You can use '10s' for 10 seconds etc",
-                "duration") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                String value = parameter.toUpperCase();
-                if (value.endsWith("S")) {
-                    value = value.substring(0, value.length() - 1);
-                    setTimeUnit(TimeUnit.SECONDS);
-                }
-                setDuration(Integer.parseInt(value));
-            }
-        });
-
-        addOption(new Option("x", "debug", "Enables the debugger") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                enableDebug();
-            }
-        });
-        addOption(new Option("t", "trace", "Enables tracing") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                enableTrace();
-            }
-        });
-        addOption(new ParameterOption("out", "output", "Output all routes to the specified XML file", "filename") {
-            protected void doProcess(String arg, String parameter,
-                    LinkedList<String> remainingArgs) {
-                setRoutesOutputFile(parameter);
-            }
-        });
+*/
     }
 
     public static void main(String... args) {
@@ -154,490 +72,66 @@
         return instance;
     }
 
-    /**
-     * Parses the command line arguments then runs the program
-     */
-    public void run(String[] args) {
-        parseArguments(args);
-        run();
-    }
-
-    /**
-     * Runs this process with the given arguments
-     */
-    public void run() {
-        if (!completed.get()) {
-            try {
-                start();
-                waitUntilCompleted();
-                stop();
-            } catch (Exception e) {
-                LOG.error("Failed: " + e, e);
-            }
-        }
-    }
-
-    /**
-     * Marks this process as being completed
-     */
-    public void completed() {
-        completed.set(true);
-        latch.countDown();
-    }
-
-    public void addRouteBuilder(RouteBuilder routeBuilder) {
-        getRouteBuilders().add(routeBuilder);
-    }
-
-    /**
-     * Displays the command line options
-     */
-    public void showOptions() {
-        showOptionsHeader();
-
-        for (Option option : options) {
-            System.out.println(option.getInformation());
-        }
-    }
-
-    /**
-     * Displays the header message for the command line options
-     */
-    public void showOptionsHeader() {
-      System.out.println("Apache Camel Runner takes the following options");
-      System.out.println();
-    }
-
-    /**
-     * Parses the command line arguments
-     */
-    public void parseArguments(String[] arguments) {
-        LinkedList<String> args = new LinkedList<String>(Arrays.asList(arguments));
-
-        boolean valid = true;
-        while (!args.isEmpty()) {
-            String arg = args.removeFirst();
-
-            boolean handled = false;
-            for (Option option : options) {
-                if (option.processOption(arg, args)) {
-                    handled = true;
-                    break;
-                }
-            }
-            if (!handled) {
-                System.out.println("Unknown option: " + arg);
-                System.out.println();
-                valid = false;
-                break;
-            }
-        }
-        if (!valid) {
-            showOptions();
-            completed();
-        }
-    }
-
-    public void addOption(Option option) {
-        options.add(option);
-    }
-
-    public abstract class Option {
-        private String abbreviation;
-        private String fullName;
-        private String description;
-
-        protected Option(String abbreviation, String fullName, String description) {
-            this.abbreviation = "-" + abbreviation;
-            this.fullName = "-" + fullName;
-            this.description = description;
-        }
-
-        public boolean processOption(String arg, LinkedList<String> remainingArgs) {
-            if (arg.equalsIgnoreCase(abbreviation) || fullName.startsWith(arg)) {
-                doProcess(arg, remainingArgs);
-                return true;
-            }
-            return false;
-        }
-
-        public String getAbbreviation() {
-            return abbreviation;
-        }
-
-        public String getDescription() {
-            return description;
-        }
-
-        public String getFullName() {
-            return fullName;
-        }
-
-        public String getInformation() {
-            return "  " + getAbbreviation() + " or " + getFullName() + " = " + getDescription();
-        }
-
-        protected abstract void doProcess(String arg, LinkedList<String> remainingArgs);
-    }
-
-    public abstract class ParameterOption extends Option {
-        private String parameterName;
-
-        protected ParameterOption(String abbreviation, String fullName, String description,
-                String parameterName) {
-            super(abbreviation, fullName, description);
-            this.parameterName = parameterName;
-        }
-
-        protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-            if (remainingArgs.isEmpty()) {
-                System.err.println("Expected fileName for ");
-                showOptions();
-                completed();
-            } else {
-                String parameter = remainingArgs.removeFirst();
-                doProcess(arg, parameter, remainingArgs);
-            }
-        }
-
-        public String getInformation() {
-            return "  " + getAbbreviation() + " or " + getFullName()
-                    + " <" + parameterName + "> = " + getDescription();
-        }
-
-        protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
-    }
 
     // Properties
     // -------------------------------------------------------------------------
-    public AbstractApplicationContext getApplicationContext() {
-        return applicationContext;
-    }
-
-    public void setApplicationContext(AbstractApplicationContext applicationContext) {
-        this.applicationContext = applicationContext;
-    }
 
-    public String getApplicationContextUri() {
-        return applicationContextUri;
-    }
-
-    public void setApplicationContextUri(String applicationContextUri) {
-        this.applicationContextUri = applicationContextUri;
-    }
-
-    public String getFileApplicationContextUri() {
-        return fileApplicationContextUri;
-    }
-
-    public void setFileApplicationContextUri(String fileApplicationContextUri) {
-        this.fileApplicationContextUri = fileApplicationContextUri;
-    }
-
-    public AbstractApplicationContext getParentApplicationContext() {
-        if (parentApplicationContext == null) {
-            if (parentApplicationContextUri != null) {
-                parentApplicationContext = new ClassPathXmlApplicationContext(parentApplicationContextUri);
-                parentApplicationContext.start();
-            }
-        }
-        return parentApplicationContext;
-    }
-
-    public void setParentApplicationContext(AbstractApplicationContext parentApplicationContext) {
-        this.parentApplicationContext = parentApplicationContext;
-    }
-
-    public String getParentApplicationContextUri() {
-        return parentApplicationContextUri;
-    }
-
-    public void setParentApplicationContextUri(String parentApplicationContextUri) {
-        this.parentApplicationContextUri = parentApplicationContextUri;
-    }
-
-    public List<SpringCamelContext> getCamelContexts() {
-        return camelContexts;
-    }
-
-    public long getDuration() {
-        return duration;
-    }
-
-    /**
-     * Sets the duration to run the application for in milliseconds until it
-     * should be terminated. Defaults to -1. Any value <= 0 will run forever.
-     *
-     * @param duration
-     */
-    public void setDuration(long duration) {
-        this.duration = duration;
-    }
-
-    public TimeUnit getTimeUnit() {
-        return timeUnit;
-    }
-
-    /**
-     * Sets the time unit duration
-     */
-    public void setTimeUnit(TimeUnit timeUnit) {
-        this.timeUnit = timeUnit;
-    }
-
-    public String getDotOutputDir() {
-        return dotOutputDir;
-    }
-
-    /**
-     * Sets the output directory of the generated DOT Files to show the visual
-     * representation of the routes. A null value disables the dot file
-     * generation
-     */
-    public void setDotOutputDir(String dotOutputDir) {
-        this.dotOutputDir = dotOutputDir;
-    }
-
-    public List<RouteBuilder> getRouteBuilders() {
-        return routeBuilders;
-    }
-
-    public void setRouteBuilders(List<RouteBuilder> routeBuilders) {
-        this.routeBuilders = routeBuilders;
-    }
-
-    public void setAggregateDot(boolean aggregateDot) {
-        this.aggregateDot = aggregateDot;
-    }
-
-    public boolean isAggregateDot() {
-        return aggregateDot;
-    }
-
-    public boolean isDebug() {
-        return debug;
-    }
-
-    public void enableDebug() {
-        this.debug = true;
-        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/debug.xml");
-    }
-
-    public boolean isTrace() {
-        return trace;
-    }
-
-    public void enableTrace() {
-        this.trace = true;
-        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/trace.xml");
-    }
-
-    public void setRoutesOutputFile(String routesOutputFile) {
-        this.routesOutputFile = routesOutputFile;         
-    }
-    
-    public String getRoutesOutputFile() {
-        return routesOutputFile;         
-    }    
-    
-    /**
-     * Returns the currently active debugger if one is enabled
-     *
-     * @return the current debugger or null if none is active
-     * @see #enableDebug()
-     */
-    public Debugger getDebugger() {
-        for (SpringCamelContext camelContext : camelContexts) {
-            Debugger debugger = Debugger.getDebugger(camelContext);
-            if (debugger != null) {
-                return debugger;
-            }
-        }
-        return null;
-    }
-
-    public List<RouteType> getRouteDefinitions() {
-        List<RouteType> answer = new ArrayList<RouteType>();
-        for (SpringCamelContext camelContext : camelContexts) {
-            answer.addAll(camelContext.getRouteDefinitions());
-        }
-        return answer;
-    }
-
-    /**
-     * Returns a {@link ProducerTemplate} from the Spring {@link ApplicationContext} instances
-     * or lazily creates a new one dynamically
-     */
-    public ProducerTemplate getCamelTemplate() {
-        if (camelTemplate == null) {
-            camelTemplate = findOrCreateCamelTemplate();
-        }
-        return camelTemplate;
-    }
 
     // Implementation methods
     // -------------------------------------------------------------------------
-    protected ProducerTemplate findOrCreateCamelTemplate() {
-        String[] names = getApplicationContext().getBeanNamesForType(ProducerTemplate.class);
-        if (names != null && names.length > 0) {
-            return (ProducerTemplate) getApplicationContext().getBean(names[0], ProducerTemplate.class);
-        }
-        for (SpringCamelContext camelContext : camelContexts) {
-            return camelContext.createProducerTemplate();
-        }
-        throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!");
-    }
 
+    @Override
     protected void doStart() throws Exception {
-        LOG.info("Apache Camel " + getVersion() + " starting");
-        if (applicationContext == null) {
-            applicationContext = createDefaultApplicationContext();
-        }
-        applicationContext.start();
+        super.doStart();
 
-        postProcessContext();
-    }
+        context = new InitialContext();
 
-    protected AbstractApplicationContext createDefaultApplicationContext() {
-        // file based
-        if (getFileApplicationContextUri() != null) {
-            String[] args = getFileApplicationContextUri().split(";");
-
-            ApplicationContext parentContext = getParentApplicationContext();
-            if (parentContext != null) {
-                return new FileSystemXmlApplicationContext(args, parentContext);
-            } else {
-                return new FileSystemXmlApplicationContext(args);
-            }
-        }
-        
-        // default to classpath based
-        String[] args = getApplicationContextUri().split(";");
-        ApplicationContext parentContext = getParentApplicationContext();
-        if (parentContext != null) {
-            return new ClassPathXmlApplicationContext(args, parentContext);
-        } else {
-            return new ClassPathXmlApplicationContext(args);
-        }
+        injector = (Injector) context.lookup(Injector.class.getName());
+        postProcessContext();
     }
 
     protected void doStop() throws Exception {
         LOG.info("Apache Camel terminating");
 
-        if (applicationContext != null) {
-            applicationContext.close();
+        if (injector != null) {
+            injector.close();
         }
     }
 
-    protected void waitUntilCompleted() {
-        while (!completed.get()) {
-            try {
-                if (duration > 0) {
-                    TimeUnit unit = getTimeUnit();
-                    LOG.info("Waiting for: " + duration + " " + unit);
-                    latch.await(duration, unit);
-                    completed.set(true);
-                } else {
-                    latch.await();
-                }
-            } catch (InterruptedException e) {
-                LOG.debug("Caught: " + e);
+    protected ProducerTemplate findOrCreateCamelTemplate() {
+        if (injector != null) {
+            Set<ProducerTemplate> set = Injectors.getInstancesOf(injector, ProducerTemplate.class);
+            if (!set.isEmpty()) {
+                return Iterables.get(set, 0);
             }
         }
-    }
-
-    protected void postProcessContext() throws Exception {
-        Map<String, SpringCamelContext> map = applicationContext.getBeansOfType(SpringCamelContext.class);
-        Set<Map.Entry<String, SpringCamelContext>> entries = map.entrySet();
-        int size = entries.size();
-        for (Map.Entry<String, SpringCamelContext> entry : entries) {
-            String name = entry.getKey();
-            SpringCamelContext camelContext = entry.getValue();
-            camelContexts.add(camelContext);
-            generateDot(name, camelContext, size);
-            postProcesCamelContext(camelContext);
-        }
-
-        if (isAggregateDot()) {
-            generateDot("aggregate", aggregateSpringCamelContext(applicationContext), 1);
-        }
-        
-        if (!"".equals(getRoutesOutputFile())) {
-            outputRoutesToFile();
-        }
-    }
-
-    private void outputRoutesToFile() throws IOException {
-        if (ObjectHelper.isNotNullAndNonEmpty(getRoutesOutputFile())) {
-            LOG.info("Generating routes as XML in the file named: " + getRoutesOutputFile());
-            ModelFileGenerator generator = new ModelFileGenerator();
-            generator.marshalRoutesUsingJaxb(getRoutesOutputFile(), getRouteDefinitions());
-        }
-    }
-
-    protected void generateDot(String name, SpringCamelContext camelContext, int size) throws IOException {
-        String outputDir = dotOutputDir;
-        if (ObjectHelper.isNotNullAndNonEmpty(outputDir)) {
-            if (size > 1) {
-                outputDir += "/" + name;
-            }
-            RouteDotGenerator generator = new RouteDotGenerator(outputDir);
-            LOG.info("Generating DOT file for routes: " + outputDir + " for: " + camelContext + " with name: " + name);
-            generator.drawRoutes(camelContext);
+        for (CamelContext camelContext : getCamelContexts()) {
+            return camelContext.createProducerTemplate();
         }
+        throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!");
     }
 
-    /**
-     * Used for aggregate dot generation
-     *
-     * @param applicationContext
-     * @return
-     * @throws Exception
-     */
-    private static SpringCamelContext aggregateSpringCamelContext(ApplicationContext applicationContext) throws Exception {
-        SpringCamelContext aggregateCamelContext = new SpringCamelContext() {
-            /**
-             *  Don't actually start this, it is merely fabricated for dot generation.
-             * @see org.apache.camel.impl.DefaultCamelContext#shouldStartRoutes()
-             */
-            protected boolean shouldStartRoutes() {
-
-                return false;
+    protected Map<String, CamelContext> getCamelContextMap() {
+        Map<String, CamelContext> answer = Maps.newHashMap();
+        if (injector != null) {
+            Set<Map.Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
+            for (Map.Entry<Key<?>, Binding<?>> entry : entries) {
+              Key<?> key = entry.getKey();
+              Class<?> keyType = Injectors.getKeyType(key);
+              if (keyType != null && CamelContext.class.isAssignableFrom(keyType)) {
+                Binding<?> binding = entry.getValue();
+                Object value = binding.getProvider().get();
+                if (value != null) {
+                  CamelContext castValue = CamelContext.class.cast(value);
+                  answer.put(key.toString(), castValue);
+                }
+              }
             }
-        };
-
-        // look up all configured camel contexts
-        String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
-        for (String name : names) {
-
-            SpringCamelContext next = (SpringCamelContext) applicationContext.getBean(name, SpringCamelContext.class);
-            //            aggregateCamelContext.addRoutes( next.getRoutes() );
-            aggregateCamelContext.addRouteDefinitions(next.getRouteDefinitions());
-        }
-        // Don't actually start this, it is merely fabricated for dot generation.
-        //        answer.setApplicationContext( applicationContext );
-        //        answer.afterPropertiesSet();
-        return aggregateCamelContext;
-    }
-
-    protected void postProcesCamelContext(CamelContext camelContext) throws Exception {
-        for (RouteBuilder routeBuilder : routeBuilders) {
-            camelContext.addRoutes(routeBuilder);
         }
+        return answer;
     }
 
-    protected String getVersion() {
-        Package aPackage = Package.getPackage("org.apache.camel");
-        if (aPackage != null) {
-            String version = aPackage.getImplementationVersion();
-            if (version == null) {
-                version = aPackage.getSpecificationVersion();
-                if (version == null) {
-                    version = "";
-                }
-            }
-            return version;
-        }
-        return "";
+    protected ModelFileGenerator createModelFileGenerator() throws JAXBException {
+        return new ModelFileGenerator(JAXBContext.newInstance("org.apache.camel.model:org.apache.camel.model.config:org.apache.camel.model.dataformat:org.apache.camel.model.language:org.apache.camel.model.loadbalancer"));
     }
-}
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

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=704951&r1=704950&r2=704951&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 Oct 15 09:25:56 2008
@@ -16,69 +16,39 @@
  */
 package org.apache.camel.spring;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.HashMap;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.ServiceSupport;
-import org.apache.camel.model.RouteType;
-import org.apache.camel.processor.interceptor.Debugger;
-import org.apache.camel.spring.handler.ModelFileGenerator;
-import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.view.RouteDotGenerator;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.camel.spring.handler.CamelNamespaceHandler;
+import org.apache.camel.view.ModelFileGenerator;
+import org.apache.camel.util.MainSupport;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 
+import javax.xml.bind.JAXBException;
+
 /**
  * A command line tool for booting up a CamelContext using an optional Spring
  * ApplicationContext
  *
  * @version $Revision$
  */
-public class Main extends ServiceSupport {
-    private static final Log LOG = LogFactory.getLog(Main.class);
+public class Main extends MainSupport {
     private static Main instance;
 
     private String applicationContextUri = "META-INF/spring/*.xml";
     private String fileApplicationContextUri;
     private AbstractApplicationContext applicationContext;
-    private List<Option> options = new ArrayList<Option>();
-    private CountDownLatch latch = new CountDownLatch(1);
-    private AtomicBoolean completed = new AtomicBoolean(false);
-    private long duration = -1;
-    private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
-    private String dotOutputDir;
-    private String routesOutputFile;
-    private boolean aggregateDot;
-    private boolean debug;
-    private boolean trace;
-    private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
-    private List<SpringCamelContext> camelContexts = new ArrayList<SpringCamelContext>();
     private AbstractApplicationContext parentApplicationContext;
     private String parentApplicationContextUri;
-    private ProducerTemplate camelTemplate;
 
     public Main() {
-        addOption(new Option("h", "help", "Displays the help screen") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                showOptions();
-                completed();
-            }
-        });
 
         addOption(new ParameterOption("a", "applicationContext",
                 "Sets the classpath based spring ApplicationContext", "applicationContext") {
@@ -94,49 +64,6 @@
             }
         });
 
-        addOption(new ParameterOption("o", "outdir",
-                "Sets the DOT output directory where the visual representations of the routes are generated",
-                "dot") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setDotOutputDir(parameter);
-            }
-        });
-        addOption(new ParameterOption("ad", "aggregate-dot",
-                "Aggregates all routes (in addition to individual route generation) into one context to create one monolithic DOT file for visual representations the entire system.",
-                "aggregate-dot") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setAggregateDot("true".equals(parameter));
-            }
-        });
-        addOption(new ParameterOption("d", "duration",
-                "Sets the time duration that the applicaiton will run for, by default in milliseconds. You can use '10s' for 10 seconds etc",
-                "duration") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                String value = parameter.toUpperCase();
-                if (value.endsWith("S")) {
-                    value = value.substring(0, value.length() - 1);
-                    setTimeUnit(TimeUnit.SECONDS);
-                }
-                setDuration(Integer.parseInt(value));
-            }
-        });
-
-        addOption(new Option("x", "debug", "Enables the debugger") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                enableDebug();
-            }
-        });
-        addOption(new Option("t", "trace", "Enables tracing") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                enableTrace();
-            }
-        });
-        addOption(new ParameterOption("out", "output", "Output all routes to the specified XML file", "filename") {
-            protected void doProcess(String arg, String parameter,
-                    LinkedList<String> remainingArgs) {
-                setRoutesOutputFile(parameter);
-            }
-        });
     }
 
     public static void main(String... args) {
@@ -154,158 +81,17 @@
         return instance;
     }
 
-    /**
-     * Parses the command line arguments then runs the program
-     */
-    public void run(String[] args) {
-        parseArguments(args);
-        run();
-    }
-
-    /**
-     * Runs this process with the given arguments
-     */
-    public void run() {
-        if (!completed.get()) {
-            try {
-                start();
-                waitUntilCompleted();
-                stop();
-            } catch (Exception e) {
-                LOG.error("Failed: " + e, e);
-            }
-        }
-    }
-
-    /**
-     * Marks this process as being completed
-     */
-    public void completed() {
-        completed.set(true);
-        latch.countDown();
-    }
-
-    public void addRouteBuilder(RouteBuilder routeBuilder) {
-        getRouteBuilders().add(routeBuilder);
-    }
-
-    /**
-     * Displays the command line options
-     */
-    public void showOptions() {
-        showOptionsHeader();
-
-        for (Option option : options) {
-            System.out.println(option.getInformation());
-        }
-    }
-
-    /**
-     * Displays the header message for the command line options
-     */
-    public void showOptionsHeader() {
-      System.out.println("Apache Camel Runner takes the following options");
-      System.out.println();
-    }
-
-    /**
-     * Parses the command line arguments
-     */
-    public void parseArguments(String[] arguments) {
-        LinkedList<String> args = new LinkedList<String>(Arrays.asList(arguments));
-
-        boolean valid = true;
-        while (!args.isEmpty()) {
-            String arg = args.removeFirst();
-
-            boolean handled = false;
-            for (Option option : options) {
-                if (option.processOption(arg, args)) {
-                    handled = true;
-                    break;
-                }
-            }
-            if (!handled) {
-                System.out.println("Unknown option: " + arg);
-                System.out.println();
-                valid = false;
-                break;
-            }
-        }
-        if (!valid) {
-            showOptions();
-            completed();
-        }
-    }
-
-    public void addOption(Option option) {
-        options.add(option);
-    }
-
-    public abstract class Option {
-        private String abbreviation;
-        private String fullName;
-        private String description;
-
-        protected Option(String abbreviation, String fullName, String description) {
-            this.abbreviation = "-" + abbreviation;
-            this.fullName = "-" + fullName;
-            this.description = description;
-        }
-
-        public boolean processOption(String arg, LinkedList<String> remainingArgs) {
-            if (arg.equalsIgnoreCase(abbreviation) || fullName.startsWith(arg)) {
-                doProcess(arg, remainingArgs);
-                return true;
-            }
-            return false;
-        }
-
-        public String getAbbreviation() {
-            return abbreviation;
-        }
-
-        public String getDescription() {
-            return description;
-        }
-
-        public String getFullName() {
-            return fullName;
-        }
-
-        public String getInformation() {
-            return "  " + getAbbreviation() + " or " + getFullName() + " = " + getDescription();
-        }
-
-        protected abstract void doProcess(String arg, LinkedList<String> remainingArgs);
+    @Override
+    public void enableDebug() {
+        super.enableDebug();
+        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/debug.xml");
     }
 
-    public abstract class ParameterOption extends Option {
-        private String parameterName;
-
-        protected ParameterOption(String abbreviation, String fullName, String description,
-                String parameterName) {
-            super(abbreviation, fullName, description);
-            this.parameterName = parameterName;
-        }
-
-        protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-            if (remainingArgs.isEmpty()) {
-                System.err.println("Expected fileName for ");
-                showOptions();
-                completed();
-            } else {
-                String parameter = remainingArgs.removeFirst();
-                doProcess(arg, parameter, remainingArgs);
-            }
-        }
-
-        public String getInformation() {
-            return "  " + getAbbreviation() + " or " + getFullName()
-                    + " <" + parameterName + "> = " + getDescription();
-        }
-
-        protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
+    @Override
+    public void enableTrace() {
+        // TODO
+        super.enableTrace();
+        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/trace.xml");
     }
 
     // Properties
@@ -356,148 +142,31 @@
         this.parentApplicationContextUri = parentApplicationContextUri;
     }
 
-    public List<SpringCamelContext> getCamelContexts() {
-        return camelContexts;
-    }
-
-    public long getDuration() {
-        return duration;
-    }
-
-    /**
-     * Sets the duration to run the application for in milliseconds until it
-     * should be terminated. Defaults to -1. Any value <= 0 will run forever.
-     *
-     * @param duration
-     */
-    public void setDuration(long duration) {
-        this.duration = duration;
-    }
-
-    public TimeUnit getTimeUnit() {
-        return timeUnit;
-    }
-
-    /**
-     * Sets the time unit duration
-     */
-    public void setTimeUnit(TimeUnit timeUnit) {
-        this.timeUnit = timeUnit;
-    }
-
-    public String getDotOutputDir() {
-        return dotOutputDir;
-    }
-
-    /**
-     * Sets the output directory of the generated DOT Files to show the visual
-     * representation of the routes. A null value disables the dot file
-     * generation
-     */
-    public void setDotOutputDir(String dotOutputDir) {
-        this.dotOutputDir = dotOutputDir;
-    }
-
-    public List<RouteBuilder> getRouteBuilders() {
-        return routeBuilders;
-    }
-
-    public void setRouteBuilders(List<RouteBuilder> routeBuilders) {
-        this.routeBuilders = routeBuilders;
-    }
-
-    public void setAggregateDot(boolean aggregateDot) {
-        this.aggregateDot = aggregateDot;
-    }
-
-    public boolean isAggregateDot() {
-        return aggregateDot;
-    }
-
-    public boolean isDebug() {
-        return debug;
-    }
-
-    public void enableDebug() {
-        this.debug = true;
-        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/debug.xml");
-    }
-
-    public boolean isTrace() {
-        return trace;
-    }
-
-    public void enableTrace() {
-        this.trace = true;
-        setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/trace.xml");
-    }
-
-    public void setRoutesOutputFile(String routesOutputFile) {
-        this.routesOutputFile = routesOutputFile;         
-    }
-    
-    public String getRoutesOutputFile() {
-        return routesOutputFile;         
-    }    
-    
-    /**
-     * Returns the currently active debugger if one is enabled
-     *
-     * @return the current debugger or null if none is active
-     * @see #enableDebug()
-     */
-    public Debugger getDebugger() {
-        for (SpringCamelContext camelContext : camelContexts) {
-            Debugger debugger = Debugger.getDebugger(camelContext);
-            if (debugger != null) {
-                return debugger;
-            }
-        }
-        return null;
-    }
+    // Implementation methods
+    // -------------------------------------------------------------------------
 
-    public List<RouteType> getRouteDefinitions() {
-        List<RouteType> answer = new ArrayList<RouteType>();
-        for (SpringCamelContext camelContext : camelContexts) {
-            answer.addAll(camelContext.getRouteDefinitions());
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        if (applicationContext == null) {
+            applicationContext = createDefaultApplicationContext();
         }
-        return answer;
-    }
+        applicationContext.start();
 
-    /**
-     * Returns a {@link ProducerTemplate} from the Spring {@link ApplicationContext} instances
-     * or lazily creates a new one dynamically
-     */
-    public ProducerTemplate getCamelTemplate() {
-        if (camelTemplate == null) {
-            camelTemplate = findOrCreateCamelTemplate();
-        }
-        return camelTemplate;
+        postProcessContext();
     }
 
-    // Implementation methods
-    // -------------------------------------------------------------------------
     protected ProducerTemplate findOrCreateCamelTemplate() {
         String[] names = getApplicationContext().getBeanNamesForType(ProducerTemplate.class);
         if (names != null && names.length > 0) {
             return (ProducerTemplate) getApplicationContext().getBean(names[0], ProducerTemplate.class);
         }
-        for (SpringCamelContext camelContext : camelContexts) {
+        for (CamelContext camelContext : getCamelContexts()) {
             return camelContext.createProducerTemplate();
         }
         throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!");
     }
 
-    protected void doStart() throws Exception {
-        LOG.info("Apache Camel " + getVersion() + " starting");
-        if (applicationContext == null) {
-            applicationContext = createDefaultApplicationContext();
-        }
-        applicationContext.start();
-
-        postProcessContext();
-    }
-
     protected AbstractApplicationContext createDefaultApplicationContext() {
         // file based
         if (getFileApplicationContextUri() != null) {
@@ -529,115 +198,19 @@
         }
     }
 
-    protected void waitUntilCompleted() {
-        while (!completed.get()) {
-            try {
-                if (duration > 0) {
-                    TimeUnit unit = getTimeUnit();
-                    LOG.info("Waiting for: " + duration + " " + unit);
-                    latch.await(duration, unit);
-                    completed.set(true);
-                } else {
-                    latch.await();
-                }
-            } catch (InterruptedException e) {
-                LOG.debug("Caught: " + e);
-            }
-        }
-    }
-
-    protected void postProcessContext() throws Exception {
+    protected Map<String,CamelContext> getCamelContextMap() {
         Map<String, SpringCamelContext> map = applicationContext.getBeansOfType(SpringCamelContext.class);
         Set<Map.Entry<String, SpringCamelContext>> entries = map.entrySet();
-        int size = entries.size();
+        Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
         for (Map.Entry<String, SpringCamelContext> entry : entries) {
             String name = entry.getKey();
-            SpringCamelContext camelContext = entry.getValue();
-            camelContexts.add(camelContext);
-            generateDot(name, camelContext, size);
-            postProcesCamelContext(camelContext);
-        }
-
-        if (isAggregateDot()) {
-            generateDot("aggregate", aggregateSpringCamelContext(applicationContext), 1);
-        }
-        
-        if (!"".equals(getRoutesOutputFile())) {
-            outputRoutesToFile();
-        }
-    }
-
-    private void outputRoutesToFile() throws IOException {
-        if (ObjectHelper.isNotNullAndNonEmpty(getRoutesOutputFile())) {
-            LOG.info("Generating routes as XML in the file named: " + getRoutesOutputFile());
-            ModelFileGenerator generator = new ModelFileGenerator();
-            generator.marshalRoutesUsingJaxb(getRoutesOutputFile(), getRouteDefinitions());
-        }
-    }
-
-    protected void generateDot(String name, SpringCamelContext camelContext, int size) throws IOException {
-        String outputDir = dotOutputDir;
-        if (ObjectHelper.isNotNullAndNonEmpty(outputDir)) {
-            if (size > 1) {
-                outputDir += "/" + name;
-            }
-            RouteDotGenerator generator = new RouteDotGenerator(outputDir);
-            LOG.info("Generating DOT file for routes: " + outputDir + " for: " + camelContext + " with name: " + name);
-            generator.drawRoutes(camelContext);
-        }
-    }
-
-    /**
-     * Used for aggregate dot generation
-     *
-     * @param applicationContext
-     * @return
-     * @throws Exception
-     */
-    private static SpringCamelContext aggregateSpringCamelContext(ApplicationContext applicationContext) throws Exception {
-        SpringCamelContext aggregateCamelContext = new SpringCamelContext() {
-            /**
-             *  Don't actually start this, it is merely fabricated for dot generation.
-             * @see org.apache.camel.impl.DefaultCamelContext#shouldStartRoutes()
-             */
-            protected boolean shouldStartRoutes() {
-
-                return false;
-            }
-        };
-
-        // look up all configured camel contexts
-        String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
-        for (String name : names) {
-
-            SpringCamelContext next = (SpringCamelContext) applicationContext.getBean(name, SpringCamelContext.class);
-            //            aggregateCamelContext.addRoutes( next.getRoutes() );
-            aggregateCamelContext.addRouteDefinitions(next.getRouteDefinitions());
-        }
-        // Don't actually start this, it is merely fabricated for dot generation.
-        //        answer.setApplicationContext( applicationContext );
-        //        answer.afterPropertiesSet();
-        return aggregateCamelContext;
-    }
-
-    protected void postProcesCamelContext(CamelContext camelContext) throws Exception {
-        for (RouteBuilder routeBuilder : routeBuilders) {
-            camelContext.addRoutes(routeBuilder);
+            CamelContext camelContext = entry.getValue();
+            answer.put(name, camelContext);
         }
+        return answer;
     }
 
-    protected String getVersion() {
-        Package aPackage = Package.getPackage("org.apache.camel");
-        if (aPackage != null) {
-            String version = aPackage.getImplementationVersion();
-            if (version == null) {
-                version = aPackage.getSpecificationVersion();
-                if (version == null) {
-                    version = "";
-                }
-            }
-            return version;
-        }
-        return "";
+    protected ModelFileGenerator createModelFileGenerator() throws JAXBException {
+        return new ModelFileGenerator(new CamelNamespaceHandler().getJaxbContext());
     }
 }

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java?rev=704951&r1=704950&r2=704951&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java Wed Oct 15 09:25:56 2008
@@ -47,6 +47,7 @@
 import org.apache.camel.spring.remoting.CamelProxyFactoryBean;
 import org.apache.camel.spring.remoting.CamelServiceExporter;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.view.ModelFileGenerator;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -69,6 +70,11 @@
     private Map<String, BeanDefinitionParser> parserMap = new HashMap<String, BeanDefinitionParser>();
 
 
+    public ModelFileGenerator createModelFileGenerator() throws JAXBException {
+        return new ModelFileGenerator(getJaxbContext());
+    }
+
+
     public void init() {
         // remoting
         addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class);
@@ -143,7 +149,7 @@
         }
     }
 
-    protected JAXBContext getJaxbContext() throws JAXBException {
+    public JAXBContext getJaxbContext() throws JAXBException {
         if (jaxbContext == null) {
             jaxbContext = createJaxbContext();
         }

Modified: 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=704951&r1=704950&r2=704951&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java Wed Oct 15 09:25:56 2008
@@ -19,6 +19,7 @@
 import java.util.List;
 
 import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.spring.util.SimpleRouteBuilder;
@@ -42,10 +43,10 @@
         main.addRouteBuilder(builder);
         main.start();
 
-        List<SpringCamelContext> contextList = main.getCamelContexts();
+        List<CamelContext> contextList = main.getCamelContexts();
         assertNotNull(contextList);
         assertEquals("size", 1, contextList.size());
-        SpringCamelContext camelContext = contextList.get(0);
+        CamelContext camelContext = contextList.get(0);
 
         MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
         endpoint.expectedMessageCount(2);



Mime
View raw message