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