View Javadoc

1   /*
2    * $Id: MuleVisualizer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tools.visualizer;
12  
13  import org.mule.config.MuleDtdResolver;
14  import org.mule.tools.visualizer.components.EndpointRegistry;
15  import org.mule.tools.visualizer.components.GraphRenderer;
16  import org.mule.tools.visualizer.components.MuleParser;
17  import org.mule.tools.visualizer.components.PostGrapher;
18  import org.mule.tools.visualizer.config.GraphConfig;
19  import org.mule.tools.visualizer.config.GraphEnvironment;
20  import org.mule.tools.visualizer.postgraphers.DocIndexerPostGrapher;
21  import org.mule.tools.visualizer.postgraphers.GalleryPostGrapher;
22  import org.mule.tools.visualizer.postgraphers.MediaCopierPostGrapher;
23  
24  import com.oy.shared.lm.graph.Graph;
25  import com.oy.shared.lm.graph.GraphFactory;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.jdom.Document;
35  import org.jdom.JDOMException;
36  import org.jdom.input.SAXBuilder;
37  
38  public class MuleVisualizer
39  {
40  
41      private GraphEnvironment env;
42  
43      private final GraphRenderer graphRenderer;
44  
45      private final List postGraphers = new ArrayList();
46  
47      public static void main(String[] args)
48      {
49          if (args.length == 0 || args[0].equals(GraphConfig.ARG_HELP))
50          {
51              printUsage();
52              System.exit(0);
53          }
54  
55          MuleVisualizer visualizer = null;
56          GraphEnvironment env = null;
57  
58          try
59          {
60              env = new GraphConfig().init(args);
61  
62              visualizer = new MuleVisualizer(env);
63          }
64          catch (Exception e)
65          {
66              if (null != env)
67              {
68                  env.logError("MuleGrapher failed to process: " + e.getMessage(), e);
69              }
70              System.exit(0);
71          }
72  
73          try
74          {
75              visualizer.visualize(env.getConfig().getFiles());
76          }
77          catch (Exception e)
78          {
79              e.printStackTrace();
80          }
81      }
82  
83      public MuleVisualizer(GraphEnvironment environment) throws Exception
84      {
85          env = environment;
86          this.graphRenderer = new GraphRenderer(env);
87          this.postGraphers.add(new DocIndexerPostGrapher(env));
88          this.postGraphers.add(new GalleryPostGrapher(env));
89          this.postGraphers.add(new MediaCopierPostGrapher());
90      }
91  
92      public List visualize(List files) throws Exception
93      {
94          List results;
95          env.getConfig().setFiles(files);
96          env.getConfig().validate();
97          if (env.getConfig().isCombineFiles())
98          {
99              generateIndividual();
100             results = generateCombined();
101         }
102         else
103         {
104             results = generateIndividual();
105         }
106 
107         for (Iterator iter = postGraphers.iterator(); iter.hasNext();)
108         {
109             PostGrapher postGrapher = (PostGrapher) iter.next();
110             env.log("************ " + postGrapher.getStatusTitle());
111             postGrapher.postGrapher(env);
112         }
113 
114         return results;
115     }
116 
117     protected List generateCombined() throws IOException, JDOMException
118     {
119         env.setDoingCombinedGeneration(true);
120         env.setEndpointRegistry(new EndpointRegistry(env));
121 //        env.log("Doing Combined Generation with file name: " + filename);
122 //        if (filename == null)
123 //        {
124 //            filename = env.getConfig().getFiles().get(0).toString() + ".combined";
125 //        }
126         return generateGraph(1, env.getConfig().getFiles(), env.getConfig().getOutputDirectory(), env.getConfig()
127                 .getCaption());
128     }
129 
130     protected List generateIndividual() throws IOException, JDOMException
131     {
132         env.setDoingCombinedGeneration(false);
133 
134         int ind = 0;
135         List resultFiles = new ArrayList(env.getConfig().getFiles().size());
136         for (Iterator iterator = env.getConfig().getFiles().iterator(); iterator.hasNext();)
137         {
138             env.setEndpointRegistry(new EndpointRegistry(env));
139             ind++;
140             Object o = iterator.next();
141             List list = new ArrayList(1);
142             list.add(o);
143             env.log("Doing inividual generation for file: " + o);
144             resultFiles.add(generateGraph(ind, list, env.getConfig().getOutputDirectory(), env.getConfig().getCaption()));
145         }
146         return resultFiles;
147     }
148 
149     protected List generateGraph(int i, List files, File outputDir, String caption)
150             throws JDOMException, IOException
151     {
152         List results = new ArrayList();
153         String fileName = env.getConfig().getOutputFilename();
154         SAXBuilder builder = new SAXBuilder();
155         builder.setValidation(true);
156         builder.setEntityResolver(new MuleDtdResolver());
157         Graph graph = GraphFactory.newGraph();
158 
159         builder.setIgnoringElementContentWhitespace(true);
160         MuleParser muleParser = new MuleParser(env, builder);
161         for (Iterator iterator = files.iterator(); iterator.hasNext();)
162         {
163 
164             Object o = iterator.next();
165             File myFile = null;
166             if (o instanceof String)
167             {
168                 myFile = new File(o.toString());
169 
170             }
171             else if (o instanceof File)
172             {
173                 myFile = (File) o;
174 
175             }
176 
177             if (myFile != null)
178             {
179                 env.log("**************** processing " + i + " of " + files.size() + 1 + " : "
180                         + myFile.getCanonicalPath());
181 
182                 if (fileName == null)
183                 {
184                     fileName = myFile.getName();
185                 }
186                 muleParser.parseMuleConfig(myFile, graph);
187             }
188             else if (o instanceof InputStream)
189             {
190                 muleParser.parseMuleConfig((InputStream) o, graph);
191             }
192             else if (o instanceof Document)
193             {
194                 muleParser.parseMuleConfig((Document) o, graph);
195             }
196             else
197             {
198                 throw new IllegalArgumentException("Object cannot be processed, unrecognised format: " + o.getClass());
199             }
200 
201             if (fileName == null)
202             {
203                 fileName = "mule-visualised";
204             }
205 
206             if (files.size() > 1)
207             {
208                 if (caption == null)
209                 {
210                     caption = "(no caption set)";
211                 }
212                 graph.getInfo().setCaption(caption);
213             }
214             if (!env.getConfig().isCombineFiles())
215             {
216                 muleParser.finalise(graph);
217                 results.add(graphRenderer.saveGraph(graph, fileName, outputDir));
218             }
219         }
220         if (env.getConfig().isCombineFiles())
221         {
222             muleParser.finalise(graph);
223             results = new ArrayList(1);
224             results.add(graphRenderer.saveGraph(graph, fileName, outputDir));
225         }
226         return results;
227     }
228 
229     public static void printUsage()
230     {
231         System.out.println("Mule Configuration Grapher");
232         System.out.println("Generates  graphs for Mule configuration files");
233         System.out.println("-----------------------------------------------");
234         System.out.println("-files      A comma-seperated list of Mule configuration files (required)");
235         System.out
236                 .println("-outputdir  The directory to write the generated graphs to. Defaults to the current directory (optional)");
237         System.out
238                 .println("-exec       The executable file used for Graph generation. Defaults to ./win32/dot.exe (optional)");
239         System.out
240                 .println("-caption    Default caption for the generated graphs. Defaults to the 'id' attribute in the config file (optional)");
241         System.out.println("-?          Displays this help");
242     }
243 
244 }