View Javadoc

1   /*
2    * Created on Aug 12, 2004 by mgreer
3    */
4   package org.apache.struts2.sitegraph;
5   
6   import java.io.ByteArrayOutputStream;
7   import java.io.FileWriter;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.io.Writer;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.apache.struts2.StrutsException;
15  import org.apache.struts2.sitegraph.renderers.DOTRenderer;
16  
17  /***
18   * // START SNIPPET: javadocs-intro
19   * SiteGraph is a tool that renders out GraphViz-generated images depicting your
20   * Struts-powered web application's flow. SiteGraph requires GraphViz be installed
21   * and that the "dot" executable be in your command path. You can find GraphViz
22   * at http://www.graphviz.org.
23   * // END SNIPPET: javadocs-intro
24   * <p/>
25   * // START SNIPPET: javadocs-api
26   * If you wish to use SiteGraph through its API rather than through the command line,
27   * you can do that as well. All you need to do is create a new SiteGraph instance,
28   * optionally specify a {@link Writer} to output the dot content to, and then call
29   * {@link #prepare()}.
30   * // END SNIPPET: javadocs-api
31   */
32  public class SiteGraph {
33  
34      private static final Log LOG = LogFactory.getLog(SiteGraph.class);
35  
36      private String configDir;
37      private String views;
38      private String output;
39      private String namespace;
40      private Writer writer;
41  
42      public SiteGraph(String configDir, String views, String output, String namespace) {
43          this.configDir = configDir;
44          this.views = views;
45          this.output = output;
46          this.namespace = namespace;
47      }
48  
49      public static void main(String[] args) throws IOException {
50          LOG.info("SiteGraph starting...");
51  
52          if (args.length != 8 && args.length != 6) {
53              InputStream is = SiteGraph.class.getResourceAsStream("sitegraph-usage.txt");
54              byte[] buffer = new byte[2048];
55              int length = -1;
56              ByteArrayOutputStream baos = new ByteArrayOutputStream();
57              while ((length = is.read(buffer)) != -1) {
58                  baos.write(buffer, 0, length);
59              }
60              is.close();
61              baos.close();
62  
63              String usage = baos.toString();
64              System.out.println(usage.replaceAll("//.*", ""));
65              return;
66          }
67  
68          String configDir = getArg(args, "config");
69          String views = getArg(args, "views");
70          String output = getArg(args, "output");
71          String namespace = getArg(args, "ns");
72  
73          // START SNIPPET: example-api
74          SiteGraph siteGraph = new SiteGraph(configDir, views, output, namespace);
75          siteGraph.prepare();
76          siteGraph.render();
77          // END SNIPPET: example-api
78      }
79  
80      private static String getArg(String[] args, String arg) {
81          for (int i = 0; i < args.length; i++) {
82              if (("-" + arg).equals(args[i]) && ((i + 1) < args.length)) {
83                  return args[i + 1];
84              }
85          }
86  
87          return "";
88      }
89  
90      /***
91       * Prepares the dot generated content and writes out to the provided writer
92       * object. If no writer has been given, that a {@link FileWriter} pointing to "out.dot"
93       * in the specified output directly shall be used.
94       */
95      public void prepare() {
96          if (writer == null) {
97              try {
98                  writer = new FileWriter(output + "/out.dot");
99              } catch (IOException e) {
100                 throw new StrutsException(e);
101             }
102         }
103 
104         StrutsConfigRetriever.setConfiguration(configDir, views.split("[, ]+"));
105         DOTRenderer renderer = new DOTRenderer(writer);
106         renderer.render(namespace);
107     }
108 
109     /***
110      * Invokes the dot command, cause GraphViz to render out.dot in the form of out.gif,
111      * located in the specified output directory. If an error occurs during this process,
112      * the error is logged and the method completes without throwing an exception.
113      */
114     public void render() {
115         try {
116             Runtime.getRuntime().exec("dot -o" + output + "/out.gif -Tgif " + output + "/out.dot");
117         } catch (IOException e) {
118             LOG.error("Could not invoke dot", e);
119         }
120     }
121 
122     public void setWriter(Writer writer) {
123         this.writer = writer;
124     }
125 }