1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
93 SiteGraph siteGraph = new SiteGraph(configDir, views, output, namespace);
94 siteGraph.prepare();
95 siteGraph.render();
96
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 }