View Javadoc

1   /*
2    * $Id: SiteGraph.java 560753 2007-07-29 16:18:28Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.sitegraph;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.Writer;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.struts2.StrutsException;
32  import org.apache.struts2.sitegraph.renderers.DOTRenderer;
33  
34  /***
35   * // START SNIPPET: javadocs-intro
36   * SiteGraph is a tool that renders out GraphViz-generated images depicting your
37   * Struts-powered web application's flow. SiteGraph requires GraphViz be installed
38   * and that the "dot" executable be in your command path. You can find GraphViz
39   * at http://www.graphviz.org.
40   * // END SNIPPET: javadocs-intro
41   * <p/>
42   * // START SNIPPET: javadocs-api
43   * If you wish to use SiteGraph through its API rather than through the command line,
44   * you can do that as well. All you need to do is create a new SiteGraph instance,
45   * optionally specify a {@link Writer} to output the dot content to, and then call
46   * {@link #prepare()}.
47   * // END SNIPPET: javadocs-api
48   */
49  public class SiteGraph {
50  
51      private static final Log LOG = LogFactory.getLog(SiteGraph.class);
52  
53      private String configDir;
54      private String views;
55      private String output;
56      private String namespace;
57      private Writer writer;
58  
59      public SiteGraph(String configDir, String views, String output, String namespace) {
60          this.configDir = configDir;
61          this.views = views;
62          this.output = output;
63          this.namespace = namespace;
64      }
65  
66      public static void main(String[] args) throws IOException {
67          LOG.info("SiteGraph starting...");
68  
69          if (args.length != 8 && args.length != 6) {
70              InputStream is = SiteGraph.class.getResourceAsStream("sitegraph-usage.txt");
71              byte[] buffer = new byte[2048];
72              int length = -1;
73              ByteArrayOutputStream baos = new ByteArrayOutputStream();
74              while ((length = is.read(buffer)) != -1) {
75                  baos.write(buffer, 0, length);
76              }
77              is.close();
78              baos.close();
79  
80              String usage = baos.toString();
81              System.out.println(usage.replaceAll("//.*", ""));
82              return;
83          }
84  
85          String configDir = getArg(args, "config");
86          String views = getArg(args, "views");
87          String output = getArg(args, "output");
88          String namespace = getArg(args, "ns");
89  
90          // START SNIPPET: example-api
91          SiteGraph siteGraph = new SiteGraph(configDir, views, output, namespace);
92          siteGraph.prepare();
93          siteGraph.render();
94          // END SNIPPET: example-api
95      }
96  
97      private static String getArg(String[] args, String arg) {
98          for (int i = 0; i < args.length; i++) {
99              if (("-" + arg).equals(args[i]) && ((i + 1) < args.length)) {
100                 return args[i + 1];
101             }
102         }
103 
104         return "";
105     }
106 
107     /***
108      * Prepares the dot generated content and writes out to the provided writer
109      * object. If no writer has been given, that a {@link FileWriter} pointing to "out.dot"
110      * in the specified output directly shall be used.
111      */
112     public void prepare() {
113         if (writer == null) {
114             try {
115                 writer = new FileWriter(output + "/out.dot");
116             } catch (IOException e) {
117                 throw new StrutsException(e);
118             }
119         }
120 
121         StrutsConfigRetriever.setConfiguration(configDir, views.split("[, ]+"));
122         DOTRenderer renderer = new DOTRenderer(writer);
123         renderer.render(namespace);
124     }
125 
126     /***
127      * Invokes the dot command, cause GraphViz to render out.dot in the form of out.gif,
128      * located in the specified output directory. If an error occurs during this process,
129      * the error is logged and the method completes without throwing an exception.
130      */
131     public void render() {
132         try {
133             Runtime.getRuntime().exec("dot -o" + output + "/out.gif -Tgif " + output + "/out.dot");
134         } catch (IOException e) {
135             LOG.error("Could not invoke dot", e);
136         }
137     }
138 
139     public void setWriter(Writer writer) {
140         this.writer = writer;
141     }
142 }