You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/08/15 17:30:09 UTC

svn commit: r566220 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/view/ components/camel-spring/src/main/java/org/apache/camel/spring/ tooling/maven/camel-maven-plugin/src/main/ja...

Author: jstrachan
Date: Wed Aug 15 08:30:08 2007
New Revision: 566220

URL: http://svn.apache.org/viewvc?view=rev&rev=566220
Log:
improvements to the DOT generator along with integration to the Spring Main and the camel:run plugin to make it easy to auto-generate a DOT file for a Spring Camel rulebase (and enable it by default for camel:run)

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FromType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ToType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/RouteDotGenerator.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
    activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunCamelMojo.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FromType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FromType.java?view=diff&rev=566220&r1=566219&r2=566220
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FromType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FromType.java Wed Aug 15 08:30:08 2007
@@ -16,15 +16,16 @@
  */
 package org.apache.camel.model;
 
+import org.apache.camel.Endpoint;
+import org.apache.camel.impl.RouteContext;
+import org.apache.camel.util.ObjectHelper;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
-import org.apache.camel.Endpoint;
-import org.apache.camel.impl.RouteContext;
-
 /**
  * Represents an XML <to/> element
  * 
@@ -98,6 +99,19 @@
 
     public void setEndpoint(Endpoint endpoint) {
         this.endpoint = endpoint;
+    }
+
+    /**
+     * Returns the endpoint URI or the name of the reference to it
+     */
+    public Object getUriOrRef() {
+        if (ObjectHelper.isNullOrBlank(uri)) {
+            return uri;
+        }
+        else if (endpoint != null) {
+            return endpoint.getEndpointUri();
+        }
+        return ref;
     }
 
     // Implementation methods

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ToType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ToType.java?view=diff&rev=566220&r1=566219&r2=566220
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ToType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ToType.java Wed Aug 15 08:30:08 2007
@@ -30,6 +30,7 @@
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Processor;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.processor.SendProcessor;
 
@@ -126,5 +127,19 @@
 
     public void setInterceptors(List<InterceptorType> interceptors) {
         this.interceptors = interceptors;
+    }
+
+
+    /**
+     * Returns the endpoint URI or the name of the reference to it
+     */
+    public Object getUriOrRef() {
+        if (ObjectHelper.isNullOrBlank(uri)) {
+            return uri;
+        }
+        else if (endpoint != null) {
+            return endpoint.getEndpointUri();
+        }
+        return ref;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/RouteDotGenerator.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/RouteDotGenerator.java?view=diff&rev=566220&r1=566219&r2=566220
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/RouteDotGenerator.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/view/RouteDotGenerator.java Wed Aug 15 08:30:08 2007
@@ -28,6 +28,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -43,9 +44,18 @@
  */
 public class RouteDotGenerator {
     private static final transient Log LOG = LogFactory.getLog(RouteDotGenerator.class);
-    private String file = "CamelRoutes.dot";
+    private String file;
     private String imagePrefix = "http://www.enterpriseintegrationpatterns.com/img/";
     private Map<Object, NodeData> nodeMap = new HashMap<Object, NodeData>();
+    private boolean makeParentDirs = true;
+
+    public RouteDotGenerator() {
+        this("CamelRoutes.dot");
+    }
+
+    public RouteDotGenerator(String file) {
+        this.file = file;
+    }
 
     public String getFile() {
         return file;
@@ -59,7 +69,11 @@
     }
 
     public void drawRoutes(CamelContext context) throws IOException {
-        PrintWriter writer = new PrintWriter(new FileWriter(file));
+        File fileValue = new File(file);
+        if (makeParentDirs) {
+            fileValue.getParentFile().mkdirs();
+        }
+        PrintWriter writer = new PrintWriter(new FileWriter(fileValue));
         try {
             generateFile(writer, context);
         }
@@ -68,12 +82,23 @@
         }
     }
 
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+    protected class NodeData {
+        public String id;
+        public String image;
+        public String label;
+        public String edgeLabel;
+        public String tooltop;
+        public String nodeType;
+        public boolean nodeWritten;
+    }
+
     protected void generateFile(PrintWriter writer, CamelContext context) {
         writer.println("digraph \"Camel Routes\" {");
         writer.println();
-        /*writer.println("label=\"Camel Context: " + context + "\"];");
-        writer.println();
-        */
+
         writer.println("node [style = \"rounded,filled\", fillcolor = yellow, "
                 + "fontname=\"Helvetica-Oblique\"];");
         writer.println();
@@ -92,15 +117,10 @@
         }
     }
 
-    protected void printRoute(PrintWriter writer, RouteType route, FromType input) {
+    protected void printRoute(PrintWriter writer, final RouteType route, FromType input) {
         NodeData nodeData = getNodeData(input);
 
-        writer.println();
-        writer.print(nodeData.id);
-        writer.println(" [");
-        writer.println("label = \"" + nodeData.label + "\"");
-        writer.println("];");
-        writer.println();
+        printNode(writer, nodeData);
 
         // TODO we should add a transactional client / event driven consumer / polling client
 
@@ -113,23 +133,20 @@
     protected NodeData printNode(PrintWriter writer, NodeData fromData, ProcessorType node) {
         NodeData toData = getNodeData(node);
 
-        writer.println();
-        writer.print(toData.id);
-        writer.println(" [");
-        RouteDotGenerator.NodeData nodeData = printNodeAttributes(writer, fromData, toData);
-        writer.println("];");
-        writer.println();
+        printNode(writer, toData);
 
-        writer.print(fromData.id);
-        writer.print(" -> ");
-        writer.print(toData.id);
-        writer.println(" [");
-
-        String label = fromData.edgeLabel;
-        if (isNotNullAndNonEmpty(label)) {
-            writer.println("label = \"" + label + "\"");
+        if (fromData != null) {
+            writer.print(fromData.id);
+            writer.print(" -> ");
+            writer.print(toData.id);
+            writer.println(" [");
+
+            String label = fromData.edgeLabel;
+            if (isNotNullAndNonEmpty(label)) {
+                writer.println("label = \"" + label + "\"");
+            }
+            writer.println("];");
         }
-        writer.println("];");
 
         // now lets write any children
         List<ProcessorType> outputs = node.getOutputs();
@@ -142,26 +159,25 @@
         return toData;
     }
 
-    protected class NodeData {
-        public String id;
-        public String image;
-        public String label;
-        public String edgeLabel;
-        public String tooltop;
-        public String nodeType;
-    }
+    protected void printNode(PrintWriter writer, NodeData data) {
+        if (!data.nodeWritten) {
+            data.nodeWritten = true;
 
-    protected NodeData printNodeAttributes(PrintWriter writer, NodeData fromData, NodeData nodeData) {
-        writer.println("label = \"" + nodeData.label + "\"");
-        writer.println("tooltip = \"" + nodeData.tooltop + "\"");
-
-        String image = nodeData.image;
-        if (image != null) {
-            writer.println("shapefile = \"" + image + "\"");
-            writer.println("shape = custom");
-            writer.println("peripheries=0");
+            writer.println();
+            writer.print(data.id);
+            writer.println(" [");
+            writer.println("label = \"" + data.label + "\"");
+            writer.println("tooltip = \"" + data.tooltop + "\"");
+
+            String image = data.image;
+            if (image != null) {
+                writer.println("shapefile = \"" + image + "\"");
+                writer.println("shape = custom");
+                writer.println("peripheries=0");
+            }
+            writer.println("];");
+            writer.println();
         }
-        return nodeData;
     }
 
     protected void configureNodeData(Object node, NodeData nodeData) {
@@ -271,12 +287,21 @@
     }
 
     protected NodeData getNodeData(Object node) {
-        NodeData answer = nodeMap.get(node);
+        Object key = node;
+        if (node instanceof FromType) {
+            FromType fromType = (FromType) node;
+            key = fromType.getUriOrRef();
+        }
+        else if (node instanceof ToType) {
+            ToType toType = (ToType) node;
+            key = toType.getUriOrRef();
+        }
+        NodeData answer = nodeMap.get(key);
         if (answer == null) {
             answer = new NodeData();
             answer.id = "node" + (nodeMap.size() + 1);
             configureNodeData(node, answer);
-            nodeMap.put(node, answer);
+            nodeMap.put(key, answer);
         }
         return answer;
     }

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?view=diff&rev=566220&r1=566219&r2=566220
==============================================================================
--- 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 Aug 15 08:30:08 2007
@@ -16,24 +16,27 @@
  */
 package org.apache.camel.spring;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.ServiceSupport;
+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.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;
 
-import org.apache.camel.impl.ServiceSupport;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import org.springframework.context.support.AbstractApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
 /**
  * A command line tool for booting up a CamelContext using an optional Spring
  * ApplicationContext
- * 
+ *
  * @version $Revision: $
  */
 public class Main extends ServiceSupport {
@@ -43,6 +46,9 @@
     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 dotFileName;
 
     public Main() {
         addOption(new Option("h", "help", "Displays the help screen") {
@@ -57,6 +63,21 @@
                 setApplicationContextUri(parameter);
             }
         });
+        addOption(new ParameterOption("f", "file", "Sets the DOT file name which is generated to show a visual representation of the routes", "dot") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setDotFileName(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));
+            }
+        });
     }
 
     public static void main(String[] args) {
@@ -79,9 +100,11 @@
         if (!completed.get()) {
             try {
                 start();
+                postProcessContext();
                 waitUntilCompleted();
                 stop();
-            } catch (Exception e) {
+            }
+            catch (Exception e) {
                 LOG.error("Failed: " + e, e);
             }
         }
@@ -188,7 +211,8 @@
                 System.err.println("Expected fileName for ");
                 showOptions();
                 completed();
-            } else {
+            }
+            else {
                 String parameter = remainingArgs.removeFirst();
                 doProcess(arg, parameter, remainingArgs);
             }
@@ -215,6 +239,43 @@
         this.applicationContextUri = applicationContextUri;
     }
 
+    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 getDotFileName() {
+        return dotFileName;
+    }
+
+    /**
+     * Sets the file name of the DOT file generated to show the visual representation of the routes.
+     * A null value disables the dot file generation
+     */
+    public void setDotFileName(String dotFileName) {
+        this.dotFileName = dotFileName;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
     protected void doStart() throws Exception {
@@ -240,10 +301,25 @@
     protected void waitUntilCompleted() {
         while (!completed.get()) {
             try {
-                latch.await();
-            } catch (InterruptedException e) {
-                // ignore
+                if (duration > 0) {
+                    latch.await(duration, getTimeUnit());
+                }
+                else {
+                    latch.await();
+                }
+            }
+            catch (InterruptedException e) {
+                LOG.debug("Caught: " + e);
             }
+        }
+    }
+
+    protected void postProcessContext() throws Exception {
+        if (ObjectHelper.isNotNullAndNonEmpty(dotFileName)) {
+            RouteDotGenerator generator = new RouteDotGenerator(dotFileName);
+            CamelContext camelContext = SpringCamelContext.springCamelContext(applicationContext);
+            LOG.info("Generating DOT file for routes: " + dotFileName + " for: " + camelContext);
+            generator.drawRoutes(camelContext);
         }
     }
 

Modified: activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunCamelMojo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunCamelMojo.java?view=diff&rev=566220&r1=566219&r2=566220
==============================================================================
--- activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunCamelMojo.java (original)
+++ activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunCamelMojo.java Wed Aug 15 08:30:08 2007
@@ -16,20 +16,6 @@
  */
 package org.apache.camel.maven;
 
-import java.io.File;
-import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.util.Set;
-
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
@@ -47,11 +33,25 @@
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectBuilder;
 import org.apache.maven.project.artifact.MavenMetadataSource;
-
 import org.codehaus.mojo.exec.AbstractExecMojo;
 import org.codehaus.mojo.exec.ExecutableDependency;
 import org.codehaus.mojo.exec.Property;
 
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+
 /**
  * Runs a CamelContext using any Spring XML configuration files found in
  * <code>META-INF/spring/*.xml</code> and starting up the context.
@@ -79,6 +79,23 @@
     protected MavenProject project;
 
     /**
+     * The duration to run the application for which by default is in milliseconds.
+     * A value <= 0 will 
+     *
+     * @parameter expression="-1"
+     * @readonly
+     */
+    protected String duration;
+
+    /**
+     * The DOT File name used to generate the DOT diagram of the route definitions
+     *
+     * @parameter expression="${project.build.directory}/site/camel-dot/Routes.dot"
+     * @readonly
+     */
+    protected String dotFile;
+
+    /**
      * @component
      */
     private ArtifactResolver artifactResolver;
@@ -268,9 +285,19 @@
             getLog().warn("Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.");
         }
 
-        if (null == arguments) {
-            arguments = new String[0];
+        // lets create the command line arguments to pass in...
+        List<String> args = new ArrayList<String>();
+        if (dotFile != null) {
+            args.add("-f");
+            args.add(dotFile);
+        }
+        args.add("-d");
+        args.add(duration);
+        if (arguments != null) {
+        args.addAll(Arrays.asList(arguments));
         }
+        arguments = new String[args.size()];
+        args.toArray(arguments);
 
         if (getLog().isDebugEnabled()) {
             StringBuffer msg = new StringBuffer("Invoking : ");