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 2008/03/28 10:37:33 UTC

svn commit: r642162 - in /activemq/camel/trunk: components/camel-spring/src/main/java/org/apache/camel/spring/ components/camel-spring/src/test/java/org/apache/camel/spring/ examples/camel-example-docs/src/main/resources/META-INF/spring/ tooling/maven/...

Author: jstrachan
Date: Fri Mar 28 02:37:31 2008
New Revision: 642162

URL: http://svn.apache.org/viewvc?rev=642162&view=rev
Log:
added a patch to fix https://issues.apache.org/activemq/browse/CAMEL-406 to deal with multiple CamelContext instances when generating the DOT files

Modified:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java
    activemq/camel/trunk/examples/camel-example-docs/src/main/resources/META-INF/spring/camel-context.xml
    activemq/camel/trunk/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DotMojo.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=642162&r1=642161&r2=642162&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 Fri Mar 28 02:37:31 2008
@@ -20,6 +20,8 @@
 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;
@@ -51,7 +53,7 @@
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
     private String dotOutputDir;
     private List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>();
-    private SpringCamelContext camelContext;
+    private List<SpringCamelContext> camelContexts = new ArrayList<SpringCamelContext>();
 
     public Main() {
         addOption(new Option("h", "help", "Displays the help screen") {
@@ -251,8 +253,8 @@
         this.applicationContextUri = applicationContextUri;
     }
 
-    public SpringCamelContext getCamelContext() {
-        return camelContext;
+    public List<SpringCamelContext> getCamelContexts() {
+        return camelContexts;
     }
 
     public long getDuration() {
@@ -344,13 +346,25 @@
     }
 
     protected void postProcessContext() throws Exception {
-        camelContext = SpringCamelContext.springCamelContext(applicationContext);
-        if (ObjectHelper.isNotNullAndNonEmpty(dotOutputDir)) {
-            RouteDotGenerator generator = new RouteDotGenerator(dotOutputDir);
-            LOG.info("Generating DOT file for routes: " + dotOutputDir + " for: " + camelContext);
-            generator.drawRoutes(camelContext);
+        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);
+
+            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);
+            }
+            postProcesCamelContext(camelContext);
         }
-        postProcesCamelContext(camelContext);
     }
 
     protected void postProcesCamelContext(CamelContext camelContext) throws Exception {

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=642162&r1=642161&r2=642162&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 Fri Mar 28 02:37:31 2008
@@ -42,8 +42,10 @@
         main.addRouteBuilder(builder);
         main.start();
 
-        SpringCamelContext camelContext = main.getCamelContext();
-        assertNotNull(camelContext);
+        List<SpringCamelContext> contextList = main.getCamelContexts();
+        assertNotNull(contextList);
+        assertEquals("size", 1, contextList.size());
+        SpringCamelContext camelContext = contextList.get(0);
 
         MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
         endpoint.expectedMessageCount(2);

Modified: activemq/camel/trunk/examples/camel-example-docs/src/main/resources/META-INF/spring/camel-context.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/examples/camel-example-docs/src/main/resources/META-INF/spring/camel-context.xml?rev=642162&r1=642161&r2=642162&view=diff
==============================================================================
--- activemq/camel/trunk/examples/camel-example-docs/src/main/resources/META-INF/spring/camel-context.xml (original)
+++ activemq/camel/trunk/examples/camel-example-docs/src/main/resources/META-INF/spring/camel-context.xml Fri Mar 28 02:37:31 2008
@@ -22,8 +22,16 @@
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">
 
-  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+  <camelContext id="main" xmlns="http://activemq.apache.org/camel/schema/spring">
     <package>org.apache.camel.example.docs</package>
+  </camelContext>
+
+  <camelContext id="otherRoutes" xmlns="http://activemq.apache.org/camel/schema/spring">
+    <route>
+      <from uri="seda:other.a"/>
+      <to uri="seda:other.b"/>
+      <to uri="seda:other.c"/>
+    </route>
   </camelContext>
 
 </beans>

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=642162&r1=642161&r2=642162&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 Fri Mar 28 02:37:31 2008
@@ -25,9 +25,11 @@
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
+import java.util.Set;
 
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.doxia.sink.Sink;
@@ -76,7 +78,6 @@
      * @readonly
      */
     protected boolean runCamel;
-
     private String indexHtmlContent;
     /**
      * Reference to Maven 2 Project.
@@ -158,8 +159,9 @@
     public void execute() throws MojoExecutionException {
         this.execute(this.buildDirectory, Locale.getDefault());
         try {
-            writeIndexHtmlFile("index.html", indexHtmlContent);
-        } catch (IOException e) {
+            writeIndexHtmlFile(outputDirectory, "index.html", indexHtmlContent);
+        }
+        catch (IOException e) {
             throw new MojoExecutionException("Failed: " + e, e);
         }
     }
@@ -174,10 +176,12 @@
             Sink kitchenSink = getSink();
             if (kitchenSink != null) {
                 kitchenSink.rawText(indexHtmlContent);
-            } else {
-                writeIndexHtmlFile("index.html", indexHtmlContent);
             }
-        } catch (Exception e) {
+            else {
+                writeIndexHtmlFile(outputDirectory, "index.html", indexHtmlContent);
+            }
+        }
+        catch (Exception e) {
             final MavenReportException ex = new MavenReportException(e.getMessage());
             ex.initCause(e.getCause());
             throw ex;
@@ -188,13 +192,14 @@
      * Executes DOT generator.
      *
      * @param outputDir report output directory.
-     * @param locale report locale.
+     * @param locale    report locale.
      * @throws MojoExecutionException if there were any execution errors.
      */
     protected void execute(final File outputDir, final Locale locale) throws MojoExecutionException {
         try {
             runCamelEmbedded(outputDir);
-        } catch (DependencyResolutionRequiredException e) {
+        }
+        catch (DependencyResolutionRequiredException e) {
             throw new MojoExecutionException("Failed: " + e, e);
         }
         outputDir.mkdirs();
@@ -205,17 +210,29 @@
         if (graphvizOutputTypes == null) {
             if (graphvizOutputType == null) {
                 graphvizOutputTypes = DEFAULT_GRAPHVIZ_OUTPUT_TYPES;
-            } else {
-                graphvizOutputTypes = new String[] {graphvizOutputType};
+            }
+            else {
+                graphvizOutputTypes = new String[]{graphvizOutputType};
             }
         }
         try {
-            for (int i = 0; i < files.size(); i++) {
-                File file = (File)((List)files).get(i);
+            Set<String> contextNames = new HashSet<String>();
+            for (File file : files) {
+                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);
+                printHtmlHeader(out, contextName);
                 printHtmlFileHeader(out, file);
                 for (int j = 0; j < graphvizOutputTypes.length; j++) {
                     String format = graphvizOutputTypes[j];
@@ -239,11 +256,34 @@
                     name = name.substring(0, idx);
                     name += ".html";
                 }
-                writeIndexHtmlFile(name, content);
+                writeIndexHtmlFile(file.getParentFile(), name, content);
+            }
+
+            if (multipleCamelContexts) {
+                // lets generate an index page which lists each indiviual
+                // CamelContext file
+                StringWriter buffer = new StringWriter();
+                PrintWriter out = new PrintWriter(buffer);
+
+                out.println("<h1>Camel Contexts</h1>");
+                out.println();
+
+                out.println("<ul>");
+                for (String contextName : contextNames) {
+                    out.print("  <li><a href='");
+                    out.print(contextName);
+                    out.print("/routes.html'>");
+                    out.print(contextName);
+                    out.println("</a></li>");
+                }
+                out.println("</ul>");
+                indexHtmlContent = buffer.toString();
             }
-        } catch (CommandLineException e) {
+        }
+        catch (CommandLineException e) {
             throw new MojoExecutionException("Failed: " + e, e);
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             throw new MojoExecutionException("Failed: " + e, e);
         }
     }
@@ -264,15 +304,15 @@
             mojo.setPluginContext(getPluginContext());
             try {
                 mojo.executeWithoutWrapping();
-            } catch (Exception e) {
+            }
+            catch (Exception e) {
                 getLog().error("Failed to run Camel embedded: " + e, e);
             }
         }
     }
 
-    protected void writeIndexHtmlFile(String fileName, String content) throws IOException {
-        //File dir = new File(outputDirectory, SUBDIRECTORY);
-        File dir = outputDirectory;
+    protected void writeIndexHtmlFile(File dir, String fileName, String content) throws IOException {
+        //File dir = outputDirectory;
         dir.mkdirs();
         File html = new File(dir, fileName);
         PrintWriter out = null;
@@ -285,19 +325,26 @@
             out.println();
             if (content == null) {
                 out.write("<p>No EIP diagrams available</p>");
-            } else {
+            }
+            else {
                 out.write(content);
             }
             out.println("</body>");
             out.println("</html>");
-        } finally {
+        }
+        finally {
             String description = "Failed to close html output file";
             close(out, description);
         }
     }
 
-    protected void printHtmlHeader(PrintWriter out) {
-        out.println("<h1>Camel EIP Patterns</h1>");
+    protected void printHtmlHeader(PrintWriter out, String contextName) {
+        if (contextName != null) {
+            out.println("<h1>EIP Patterns for CamelContext: " + contextName + "</h1>");
+        }
+        else {
+            out.println("<h1>Camel EIP Patterns</h1>");
+        }
         out.println();
     }
 
@@ -320,7 +367,8 @@
         if (closeable != null) {
             try {
                 closeable.close();
-            } catch (IOException e) {
+            }
+            catch (IOException e) {
                 getLog().warn(description + ": " + e);
             }
         }
@@ -359,7 +407,8 @@
         int idx = name.lastIndexOf(".");
         if (idx > 0) {
             return name.substring(0, idx);
-        } else {
+        }
+        else {
             return name;
         }
     }
@@ -367,7 +416,8 @@
     private void appendFiles(List<File> output, File file) {
         if (file.isDirectory()) {
             appendDirectory(output, file);
-        } else {
+        }
+        else {
             if (isValid(file)) {
                 output.add(file);
             }
@@ -394,13 +444,16 @@
                 String line = reader.readLine();
                 if (line == null) {
                     break;
-                } else {
+                }
+                else {
                     out.println(line);
                 }
             }
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             throw new MojoExecutionException("Failed: " + e, e);
-        } finally {
+        }
+        finally {
             close(reader, "cmapx file");
         }
     }