camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r643284 - in /activemq/camel/trunk: components/camel-spring/src/main/java/org/apache/camel/spring/ tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/
Date Tue, 01 Apr 2008 05:21:05 GMT
Author: jstrachan
Date: Mon Mar 31 22:21:00 2008
New Revision: 643284

URL: http://svn.apache.org/viewvc?rev=643284&view=rev
Log:
applied patch for http://issues.apache.org/activemq/browse/CAMEL-413 with thanks!

Modified:
    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/DotMojo.java
    activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/EmbeddedMojo.java

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=643284&r1=643283&r2=643284&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
Mon Mar 31 22:21:00 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.spring;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedList;
@@ -33,6 +34,7 @@
 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;
 
@@ -52,6 +54,7 @@
     private long duration = -1;
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
     private String dotOutputDir;
+    private boolean aggregateDot;
     private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
     private List<SpringCamelContext> camelContexts = new ArrayList<SpringCamelContext>();
 
@@ -64,23 +67,30 @@
         });
 
         addOption(new ParameterOption("a", "applicationContext",
-            "Sets the classpath based pring ApplicationContext", "applicationContext") {
+                "Sets the classpath based pring ApplicationContext", "applicationContext")
{
             protected void doProcess(String arg, String parameter, LinkedList<String>
remainingArgs) {
                 setApplicationContextUri(parameter);
             }
         });
         addOption(new ParameterOption("o", "outdir",
-            "Sets the DOT output directory where the visual representations of the routes
are generated",
-            "dot") {
+                "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") {
+                "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")) {
@@ -113,7 +123,8 @@
                 start();
                 waitUntilCompleted();
                 stop();
-            } catch (Exception e) {
+            }
+            catch (Exception e) {
                 LOG.error("Failed: " + e, e);
             }
         }
@@ -140,7 +151,7 @@
 
         for (Option option : options) {
             System.out.println("  " + option.getAbbreviation() + " or " + option.getFullName()
+ " = "
-                               + option.getDescription());
+                    + option.getDescription());
         }
     }
 
@@ -216,7 +227,7 @@
         private String parameterName;
 
         protected ParameterOption(String abbreviation, String fullName, String description,
-                                  String parameterName) {
+                String parameterName) {
             super(abbreviation, fullName, description);
             this.parameterName = parameterName;
         }
@@ -226,7 +237,8 @@
                 System.err.println("Expected fileName for ");
                 showOptions();
                 completed();
-            } else {
+            }
+            else {
                 String parameter = remainingArgs.removeFirst();
                 doProcess(arg, parameter, remainingArgs);
             }
@@ -303,6 +315,14 @@
         this.routeBuilders = routeBuilders;
     }
 
+    public void setAggregateDot(boolean aggregateDot) {
+        this.aggregateDot = aggregateDot;
+    }
+
+    public boolean isAggregateDot() {
+        return aggregateDot;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
     protected void doStart() throws Exception {
@@ -336,10 +356,12 @@
                     LOG.info("Waiting for: " + duration + " " + unit);
                     latch.await(duration, unit);
                     completed.set(true);
-                } else {
+                }
+                else {
                     latch.await();
                 }
-            } catch (InterruptedException e) {
+            }
+            catch (InterruptedException e) {
                 LOG.debug("Caught: " + e);
             }
         }
@@ -353,19 +375,59 @@
             String name = entry.getKey();
             SpringCamelContext camelContext = entry.getValue();
             camelContexts.add(camelContext);
+            generateDot(name, camelContext, size);
+            postProcesCamelContext(camelContext);
+        }
 
-            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);
+        if (isAggregateDot()) {
+
+            generateDot("aggregate", aggregateSpringCamelContext(applicationContext), 1);
+        }
+    }
+
+    protected void generateDot(String name, SpringCamelContext camelContext, int size) throws
IOException {
+        String outputDir = dotOutputDir;
+        if (ObjectHelper.isNotNullAndNonEmpty(outputDir)) {
+            if (size > 1) {
+                outputDir += "/" + name;
             }
-            postProcesCamelContext(camelContext);
+            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 {

Modified: activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DotMojo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DotMojo.java?rev=643284&r1=643283&r2=643284&view=diff
==============================================================================
--- activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DotMojo.java
(original)
+++ activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DotMojo.java
Mon Mar 31 22:21:00 2008
@@ -60,9 +60,7 @@
      * Subdirectory for report.
      */
     protected static final String SUBDIRECTORY = "cameldoc";
-
     private String indexHtmlContent;
-
     //
     // For running Camel embedded
     //-------------------------------------------------------------------------
@@ -113,6 +111,13 @@
      */
     private File outputDirectory;
     /**
+     * In the case of multiple camel contexts, setting aggregate == true will aggregate all
+     * into a monolithic context, otherwise they will be processed independently.
+     *
+     * @parameter
+     */
+    private String aggregate;
+    /**
      * GraphViz executable location; visualization (images) will be
      * generated only if you install this program and set this property to the
      * executable dot (dot.exe on Win).
@@ -162,6 +167,14 @@
         return SUBDIRECTORY + "/index";
     }
 
+    public String getAggregate() {
+        return aggregate;
+    }
+
+    public void setAggregate(String aggregate) {
+        this.aggregate = aggregate;
+    }
+
     /**
      * @see org.apache.maven.plugin.Mojo#execute()
      */
@@ -205,6 +218,7 @@
      * @throws MojoExecutionException if there were any execution errors.
      */
     protected void execute(final File outputDir, final Locale locale) throws MojoExecutionException
{
+
         try {
             runCamelEmbedded(outputDir);
         }
@@ -230,42 +244,19 @@
                 String contextName = file.getParentFile().getName();
                 contextNames.add(contextName);
             }
+
             boolean multipleCamelContexts = contextNames.size() > 1;
 
             for (int i = 0, size = files.size(); i < size; i++) {
                 File file = files.get(i);
-
                 String contextName = null;
                 if (multipleCamelContexts) {
                     contextName = file.getParentFile().getName();
                 }
-                StringWriter buffer = new StringWriter();
-                PrintWriter out = new PrintWriter(buffer);
-                printHtmlHeader(out, contextName);
-                printHtmlFileHeader(out, file);
-                for (int j = 0; j < graphvizOutputTypes.length; j++) {
-                    String format = graphvizOutputTypes[j];
-                    String generated = convertFile(file, format);
-
-                    if (generated != null && format.equals("cmapx")) {
-                        // lets include the generated file inside the html
-                        addFileToBuffer(out, new File(generated));
-                    }
-                }
-                printHtmlFileFooter(out, file);
-                printHtmlFooter(out);
 
-                String content = buffer.toString();
-                String name = file.getName();
-                if (name.equalsIgnoreCase("routes.dot") || i == 0) {
-                    indexHtmlContent = content;
-                }
-                int idx = name.lastIndexOf(".");
-                if (idx >= 0) {
-                    name = name.substring(0, idx);
-                    name += ".html";
-                }
-                writeIndexHtmlFile(file.getParentFile(), name, content);
+                getLog().info("Generating contextName: " + contextName + " file: " + file
+ "");
+
+                generate(i, file, contextName);
             }
 
             if (multipleCamelContexts) {
@@ -297,6 +288,37 @@
         }
     }
 
+    private void generate(int index, File file, String contextName) throws CommandLineException,
MojoExecutionException, IOException {
+
+        StringWriter buffer = new StringWriter();
+        PrintWriter out = new PrintWriter(buffer);
+        printHtmlHeader(out, contextName);
+        printHtmlFileHeader(out, file);
+        for (int j = 0; j < graphvizOutputTypes.length; j++) {
+            String format = graphvizOutputTypes[j];
+            String generated = convertFile(file, format);
+
+            if (format.equals("cmapx") && generated != null) {
+                // lets include the generated file inside the html
+                addFileToBuffer(out, new File(generated));
+            }
+        }
+        printHtmlFileFooter(out, file);
+        printHtmlFooter(out);
+
+        String content = buffer.toString();
+        String name = file.getName();
+        if (name.equalsIgnoreCase("routes.dot") || index == 0) {
+            indexHtmlContent = content;
+        }
+        int idx = name.lastIndexOf(".");
+        if (idx >= 0) {
+            name = name.substring(0, idx);
+            name += ".html";
+        }
+        writeIndexHtmlFile(file.getParentFile(), name, content);
+    }
+
     protected void runCamelEmbedded(File outputDir) throws DependencyResolutionRequiredException
{
         if (runCamel) {
             getLog().info("Running Camel embedded to load META-INF/spring/*.xml files");
@@ -307,6 +329,10 @@
             EmbeddedMojo mojo = new EmbeddedMojo();
             mojo.setClasspathElements(list);
             mojo.setDotEnabled(true);
+            if ("true".equals(getAggregate())) {
+
+                mojo.setDotAggregationEnabled(true);
+            }
             mojo.setOutputDirectory(outputDirectory.getAbsolutePath());
             mojo.setDuration(duration);
             mojo.setLog(getLog());
@@ -390,7 +416,7 @@
             return null;
         }
         if (this.executable == null || this.executable.length() == 0) {
-            log.warn( "Parameter <executable/> was not set in the pom.xml.  Skipping
conversion." );
+            log.warn("Parameter <executable/> was not set in the pom.xml.  Skipping
conversion.");
             return null;
         }
 

Modified: activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/EmbeddedMojo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/EmbeddedMojo.java?rev=643284&r1=643283&r2=643284&view=diff
==============================================================================
--- activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/EmbeddedMojo.java
(original)
+++ activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/EmbeddedMojo.java
Mon Mar 31 22:21:00 2008
@@ -22,6 +22,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -61,6 +62,13 @@
      */
     protected boolean dotEnabled;
     /**
+     * Allows the routes from multiple contexts to be aggregated into one DOT file (in addition
to the individual files)
+     *
+     * @parameter expression="false"
+     * @readonly
+     */
+    protected boolean dotAggregationEnabled;
+    /**
      * Project classpath.
      *
      * @parameter expression="${project.testClasspathElements}"
@@ -69,26 +77,27 @@
      */
     private List classpathElements;
 
-
     /**
      * This method will run the mojo
      */
     public void execute() throws MojoExecutionException {
         try {
             executeWithoutWrapping();
-        } catch (Exception e) {
+        }
+        catch (Exception e) {
             throw new MojoExecutionException("Failed: " + e, e);
         }
     }
 
     public void executeWithoutWrapping() throws MalformedURLException, ClassNotFoundException,
-        NoSuchMethodException, IllegalAccessException, MojoExecutionException {
+            NoSuchMethodException, IllegalAccessException, MojoExecutionException {
         ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
         try {
             ClassLoader newLoader = createClassLoader(null);
             Thread.currentThread().setContextClassLoader(newLoader);
             runCamel(newLoader);
-        } finally {
+        }
+        finally {
             Thread.currentThread().setContextClassLoader(oldClassLoader);
         }
     }
@@ -138,11 +147,19 @@
         this.duration = duration;
     }
 
+    public boolean isDotAggregationEnabled() {
+        return dotAggregationEnabled;
+    }
+
+    public void setDotAggregationEnabled(boolean dotAggregationEnabled) {
+        this.dotAggregationEnabled = dotAggregationEnabled;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------
 
     protected void runCamel(ClassLoader newLoader) throws ClassNotFoundException, NoSuchMethodException,
-        IllegalAccessException, MojoExecutionException {
+            IllegalAccessException, MojoExecutionException {
         getLog().debug("Running Camel in: " + newLoader);
         Class<?> type = newLoader.loadClass("org.apache.camel.spring.Main");
         Method method = type.getMethod("main", String[].class);
@@ -150,19 +167,33 @@
         getLog().debug("Starting the Camel Main with arguments: " + Arrays.asList(arguments));
 
         try {
-            method.invoke(null, new Object[] {arguments});
-        } catch (InvocationTargetException e) {
+            method.invoke(null, new Object[]{arguments});
+        }
+        catch (InvocationTargetException e) {
             Throwable t = e.getTargetException();
             throw new MojoExecutionException("Failed: " + t, t);
         }
     }
 
     protected String[] createArguments() {
-        if (dotEnabled) {
-            return new String[] {"-duration", duration, "-outdir", outputDirectory};
-        } else {
-            return new String[] {"-duration", duration};
+
+        ArrayList<String> args = new ArrayList<String>(5);
+        if (isDotEnabled()) {
+
+            args.add("-outdir");
+            args.add(getOutputDirectory());
         }
+
+        if (isDotAggregationEnabled()) {
+
+            args.add("-aggregate-dot");
+            args.add("true");
+        }
+
+        args.add("-duration");
+        args.add(getDuration());
+
+        return (String[]) args.toArray(new String[0]);
     }
 
     protected ClassLoader createClassLoader(ClassLoader parent) throws MalformedURLException
{
@@ -171,7 +202,7 @@
         int size = classpathElements.size();
         URL[] urls = new URL[size];
         for (int i = 0; i < size; i++) {
-            String name = (String)classpathElements.get(i);
+            String name = (String) classpathElements.get(i);
             File file = new File(name);
             urls[i] = file.toURL();
             getLog().debug("URL: " + urls[i]);



Mime
View raw message