View Javadoc

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