1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tools.visualizer.config; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.io.FileInputStream; |
15 | |
import java.io.FileNotFoundException; |
16 | |
import java.io.IOException; |
17 | |
import java.util.ArrayList; |
18 | |
import java.util.Iterator; |
19 | |
import java.util.List; |
20 | |
import java.util.Map; |
21 | |
import java.util.Properties; |
22 | |
import java.util.StringTokenizer; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class GraphConfig |
29 | |
{ |
30 | |
|
31 | |
public static final String ARG_FILES = "-files"; |
32 | |
public static final String ARG_EXEC = "-exec"; |
33 | |
public static final String ARG_OUTPUT_DIR = "-outputdir"; |
34 | |
public static final String ARG_OUTPUT_FILE = "-outputfile"; |
35 | |
public static final String ARG_CAPTION = "-caption"; |
36 | |
public static final String ARG_MAPPINGS = "-mappings"; |
37 | |
public static final String ARG_HELP = "-?"; |
38 | |
public static final String ARG_KEEP_DOT_FILES = "-keepdotfiles"; |
39 | |
public static final String ARG_COMBINE_FILES = "-combinefiles"; |
40 | |
public static final String ARG_URLS = "-urls"; |
41 | |
public static final String ARG_CONFIG = "-config"; |
42 | |
public static final String ARG_WORKING_DIRECTORY = "-workingdir"; |
43 | |
public static final String ARG_DEBUG = "-debug"; |
44 | |
|
45 | |
public static final String ARG_SHOW_CONNECTORS = "-showconnectors"; |
46 | |
public static final String ARG_SHOW_MODELS = "-showmodels"; |
47 | |
public static final String ARG_SHOW_CONFIG = "-showconfig"; |
48 | |
public static final String ARG_SHOW_AGENTS = "-showagents"; |
49 | |
public static final String ARG_SHOW_TRANSFORMERS = "-showtransformers"; |
50 | |
public static final String ARG_SHOW_ALL = "-showall"; |
51 | |
public static final String ARG_TEMPLATE_PROPS = "-templateprops"; |
52 | |
|
53 | |
private List files; |
54 | |
private String executeCommand; |
55 | |
private File outputDirectory; |
56 | |
private String outputFilename; |
57 | |
private String workingDirectory; |
58 | |
private String caption; |
59 | |
private File mappingsFile; |
60 | |
private File urlsFile; |
61 | 4 | private boolean combineFiles = false; |
62 | 4 | private boolean keepDotFiles = false; |
63 | 4 | private List ignoredAttributes = null; |
64 | |
|
65 | 4 | private Properties mappings = new Properties(); |
66 | 4 | private Properties urls = new Properties(); |
67 | 4 | private Properties propsFromFile = null; |
68 | 4 | private Properties templateProps = null; |
69 | |
|
70 | 4 | private boolean showConnectors = true; |
71 | 4 | private boolean showTransformers = false; |
72 | 4 | private boolean showModels = false; |
73 | 4 | private boolean showConfig = false; |
74 | 4 | private boolean showAgents = false; |
75 | 4 | private boolean showAll = false; |
76 | 4 | private boolean debug = false; |
77 | |
|
78 | |
public GraphConfig() |
79 | 4 | { |
80 | 4 | files = new ArrayList(); |
81 | 4 | addIgnorredArributes(); |
82 | 4 | } |
83 | |
|
84 | |
public GraphEnvironment init() |
85 | |
{ |
86 | 0 | return new GraphEnvironment(this); |
87 | |
} |
88 | |
|
89 | |
public GraphEnvironment init(String[] args) throws IOException |
90 | |
{ |
91 | 0 | loadProperties(getOpt(args, ARG_CONFIG, null)); |
92 | 0 | setWorkingDirectory(getOpt(args, ARG_WORKING_DIRECTORY, null)); |
93 | 0 | setFilesString(getOpt(args, ARG_FILES, null)); |
94 | 0 | loadTemplateProps(getOpt(args, ARG_TEMPLATE_PROPS, null)); |
95 | 0 | setOutputDirectory(getOpt(args, ARG_OUTPUT_DIR, null)); |
96 | 0 | setOutputFilename(getOpt(args, ARG_OUTPUT_FILE, null)); |
97 | 0 | setCaption(getOpt(args, ARG_CAPTION, null)); |
98 | 0 | setExecuteCommand(getOpt(args, ARG_EXEC, null)); |
99 | 0 | setKeepDotFiles(Boolean.valueOf(getOpt(args, ARG_KEEP_DOT_FILES, "false")).booleanValue()); |
100 | 0 | setCombineFiles(Boolean.valueOf(getOpt(args, ARG_COMBINE_FILES, "false")).booleanValue()); |
101 | 0 | setShowAll(Boolean.valueOf(getOpt(args, ARG_SHOW_ALL, String.valueOf(showAll))).booleanValue()); |
102 | 0 | if (!showAll) |
103 | |
{ |
104 | 0 | setShowConnectors(Boolean.valueOf( |
105 | |
getOpt(args, ARG_SHOW_CONNECTORS, String.valueOf(showConnectors))).booleanValue()); |
106 | 0 | setShowConfig(Boolean.valueOf(getOpt(args, ARG_SHOW_CONFIG, String.valueOf(showConfig))) |
107 | |
.booleanValue()); |
108 | 0 | setShowAgents(Boolean.valueOf(getOpt(args, ARG_SHOW_AGENTS, String.valueOf(showAgents))) |
109 | |
.booleanValue()); |
110 | 0 | setShowModels(Boolean.valueOf(getOpt(args, ARG_SHOW_MODELS, String.valueOf(showModels))) |
111 | |
.booleanValue()); |
112 | 0 | setShowTransformers(Boolean.valueOf(getOpt(args, ARG_SHOW_TRANSFORMERS, |
113 | |
String.valueOf(showTransformers))).booleanValue()); |
114 | |
} |
115 | 0 | setMappingsFile(getOpt(args, ARG_MAPPINGS, null)); |
116 | 0 | setUrlsFile(getOpt(args, ARG_URLS, null)); |
117 | 0 | setDebug(Boolean.valueOf(getOpt(args, ARG_DEBUG, String.valueOf(debug))).booleanValue()); |
118 | |
|
119 | 0 | return init(); |
120 | |
} |
121 | |
|
122 | |
protected void addIgnorredArributes() |
123 | |
{ |
124 | 4 | ignoredAttributes = new ArrayList(); |
125 | 4 | ignoredAttributes.add("className"); |
126 | 4 | ignoredAttributes.add("inboundEndpoint"); |
127 | 4 | ignoredAttributes.add("outboundEndpoint"); |
128 | 4 | ignoredAttributes.add("responseEndpoint"); |
129 | 4 | ignoredAttributes.add("inboundTransformer"); |
130 | 4 | ignoredAttributes.add("outboundTransformer"); |
131 | 4 | ignoredAttributes.add("type"); |
132 | 4 | ignoredAttributes.add("singleton"); |
133 | 4 | ignoredAttributes.add("containerManaged"); |
134 | |
|
135 | |
|
136 | 4 | ignoredAttributes.add("name"); |
137 | 4 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public String applyWorkingDirectory(String path) |
146 | |
{ |
147 | 4 | return this.applyDirectory(workingDirectory, path); |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public String applyOutputDirectory(String path) |
157 | |
{ |
158 | 8 | return this.applyDirectory(this.getOutputDirectory().getPath(), path); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
protected String applyDirectory(String dirPath, String path) |
169 | |
{ |
170 | 12 | if (path == null) |
171 | |
{ |
172 | 0 | return null; |
173 | |
} |
174 | 12 | if (dirPath == null) |
175 | |
{ |
176 | 0 | return path; |
177 | |
} |
178 | 12 | if (path.startsWith("/") || path.startsWith("\\")) |
179 | |
{ |
180 | 4 | return path; |
181 | |
} |
182 | 8 | return dirPath + File.separator + path; |
183 | |
} |
184 | |
|
185 | |
public void loadProperties(String props) throws IOException |
186 | |
{ |
187 | 4 | if (null != props) |
188 | |
{ |
189 | 0 | propsFromFile = new Properties(); |
190 | 0 | propsFromFile.load(new FileInputStream(props)); |
191 | |
} |
192 | 4 | } |
193 | |
|
194 | |
public void loadTemplateProps(String props) throws IOException |
195 | |
{ |
196 | 4 | templateProps = new Properties(); |
197 | 4 | if (props != null) |
198 | |
{ |
199 | 0 | for (StringTokenizer stringTokenizer = new StringTokenizer(props, ","); |
200 | 0 | stringTokenizer.hasMoreTokens();) |
201 | |
{ |
202 | 0 | Properties p = new Properties(); |
203 | 0 | p.load(new FileInputStream(applyWorkingDirectory(stringTokenizer.nextToken()))); |
204 | |
|
205 | 0 | for (Iterator iterator = p.entrySet().iterator(); iterator.hasNext();) |
206 | |
{ |
207 | 0 | Map.Entry e = (Map.Entry) iterator.next(); |
208 | 0 | if (templateProps.getProperty(e.getKey().toString()) != null) |
209 | |
{ |
210 | 0 | System.err.println("There is a properties conflict with property: " + e.getKey()); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 0 | templateProps.put(e.getKey(), e.getValue()); |
215 | |
} |
216 | 0 | } |
217 | 0 | } |
218 | |
} |
219 | 4 | } |
220 | |
|
221 | |
public void validate() |
222 | |
{ |
223 | 4 | if (files == null || files.size() == 0) |
224 | |
{ |
225 | 0 | throw new IllegalStateException("At least one mule config file must be set"); |
226 | |
} |
227 | 4 | } |
228 | |
|
229 | |
private String getOpt(String[] args, String name, String defaultValue) |
230 | |
{ |
231 | |
|
232 | 0 | if (propsFromFile != null) |
233 | |
{ |
234 | 0 | return propsFromFile.getProperty(name.substring(1), defaultValue); |
235 | |
} |
236 | |
|
237 | 0 | String rval = defaultValue; |
238 | 0 | for (int i = 0; i < args.length; i++) |
239 | |
{ |
240 | 0 | if (args[i].equals(name)) |
241 | |
{ |
242 | 0 | if (i + 1 >= args.length) |
243 | |
{ |
244 | 0 | break; |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 0 | String arg = args[i + 1]; |
249 | 0 | if (arg.startsWith("-")) |
250 | |
{ |
251 | 0 | break; |
252 | |
} |
253 | |
else |
254 | |
{ |
255 | 0 | rval = arg; |
256 | 0 | break; |
257 | |
} |
258 | |
} |
259 | |
} |
260 | |
} |
261 | 0 | if (rval == null || rval.length() == 0) |
262 | |
{ |
263 | 0 | rval = null; |
264 | |
} |
265 | 0 | return rval; |
266 | |
} |
267 | |
|
268 | |
public List getFiles() |
269 | |
{ |
270 | 8 | return files; |
271 | |
} |
272 | |
|
273 | |
public void setFiles(List files) |
274 | |
{ |
275 | 8 | this.files = files; |
276 | 8 | } |
277 | |
|
278 | |
public void setFilesString(String filesString) |
279 | |
{ |
280 | 0 | if (filesString != null) |
281 | |
{ |
282 | 0 | files = new ArrayList(); |
283 | 0 | for (StringTokenizer stringTokenizer = new StringTokenizer(filesString, ","); stringTokenizer |
284 | 0 | .hasMoreTokens();) |
285 | |
{ |
286 | 0 | files.add(applyWorkingDirectory(stringTokenizer.nextToken())); |
287 | |
} |
288 | |
} |
289 | |
|
290 | 0 | } |
291 | |
|
292 | |
public String getExecuteCommand() |
293 | |
{ |
294 | 12 | return executeCommand; |
295 | |
} |
296 | |
|
297 | |
public void setExecuteCommand(String executeCommand) |
298 | |
{ |
299 | 8 | this.executeCommand = executeCommand; |
300 | 8 | } |
301 | |
|
302 | |
public File getOutputDirectory() |
303 | |
{ |
304 | 32 | return outputDirectory; |
305 | |
} |
306 | |
|
307 | |
public void setOutputDirectory(String outputDir) |
308 | |
{ |
309 | 4 | if (outputDir == null) |
310 | |
{ |
311 | 0 | outputDir = (workingDirectory == null ? "." : workingDirectory); |
312 | |
} |
313 | 4 | outputDirectory = new File(applyWorkingDirectory(outputDir)); |
314 | 4 | if (!outputDirectory.exists()) |
315 | |
{ |
316 | 0 | outputDirectory.mkdirs(); |
317 | |
} |
318 | 4 | } |
319 | |
|
320 | |
public String getCaption() |
321 | |
{ |
322 | 4 | return caption; |
323 | |
} |
324 | |
|
325 | |
public void setCaption(String caption) |
326 | |
{ |
327 | 4 | this.caption = caption; |
328 | 4 | } |
329 | |
|
330 | |
public File getMappingsFile() |
331 | |
{ |
332 | 0 | return mappingsFile; |
333 | |
} |
334 | |
|
335 | |
public void setMappingsFile(String temp) throws IOException |
336 | |
{ |
337 | 4 | if (temp != null) |
338 | |
{ |
339 | 0 | File file = new File(applyWorkingDirectory(temp)); |
340 | 0 | if (file.exists()) |
341 | |
{ |
342 | 0 | mappings = new Properties(); |
343 | 0 | mappings.load(new FileInputStream(file)); |
344 | 0 | mappings.list(System.out); |
345 | 0 | mappingsFile = file; |
346 | |
} |
347 | |
else |
348 | |
{ |
349 | 0 | throw new FileNotFoundException("Could not find file: " + file.getAbsolutePath()); |
350 | |
} |
351 | |
} |
352 | 4 | } |
353 | |
|
354 | |
public boolean isCombineFiles() |
355 | |
{ |
356 | 12 | return combineFiles; |
357 | |
} |
358 | |
|
359 | |
public void setCombineFiles(boolean combineFiles) |
360 | |
{ |
361 | 4 | this.combineFiles = combineFiles; |
362 | 4 | } |
363 | |
|
364 | |
public boolean isKeepDotFiles() |
365 | |
{ |
366 | 4 | return keepDotFiles; |
367 | |
} |
368 | |
|
369 | |
public void setKeepDotFiles(boolean keepDotFiles) |
370 | |
{ |
371 | 4 | this.keepDotFiles = keepDotFiles; |
372 | 4 | } |
373 | |
|
374 | |
public List getIgnoredAttributes() |
375 | |
{ |
376 | 50 | return ignoredAttributes; |
377 | |
} |
378 | |
|
379 | |
public void setIgnoredAttributes(List ignoredAttributes) |
380 | |
{ |
381 | 0 | this.ignoredAttributes = ignoredAttributes; |
382 | 0 | } |
383 | |
|
384 | |
public Properties getMappings() |
385 | |
{ |
386 | 48 | return mappings; |
387 | |
} |
388 | |
|
389 | |
public void setMappings(Properties mappings) |
390 | |
{ |
391 | 0 | this.mappings = mappings; |
392 | 0 | } |
393 | |
|
394 | |
public String getOutputFilename() |
395 | |
{ |
396 | 4 | return outputFilename; |
397 | |
} |
398 | |
|
399 | |
public void setOutputFilename(String outputFilename) |
400 | |
{ |
401 | 4 | this.outputFilename = outputFilename; |
402 | 4 | } |
403 | |
|
404 | |
public Properties getUrls() |
405 | |
{ |
406 | 4 | return urls; |
407 | |
} |
408 | |
|
409 | |
public void setUrls(Properties urls) |
410 | |
{ |
411 | 0 | this.urls = urls; |
412 | 0 | } |
413 | |
|
414 | |
public File getUrlsFile() |
415 | |
{ |
416 | 0 | return urlsFile; |
417 | |
} |
418 | |
|
419 | |
public void setUrlsFile(String temp) throws IOException |
420 | |
{ |
421 | 4 | if (temp != null) |
422 | |
{ |
423 | 0 | urlsFile = new File(applyWorkingDirectory(temp)); |
424 | 0 | if (urlsFile.exists()) |
425 | |
{ |
426 | 0 | urls = new Properties(); |
427 | 0 | urls.load(new FileInputStream(urlsFile)); |
428 | 0 | urls.list(System.out); |
429 | |
} |
430 | |
} |
431 | 4 | } |
432 | |
|
433 | |
public boolean isShowConnectors() |
434 | |
{ |
435 | 4 | return showConnectors; |
436 | |
} |
437 | |
|
438 | |
public void setShowConnectors(boolean showConnectors) |
439 | |
{ |
440 | 4 | this.showConnectors = showConnectors; |
441 | 4 | } |
442 | |
|
443 | |
public boolean isShowModels() |
444 | |
{ |
445 | 4 | return showModels; |
446 | |
} |
447 | |
|
448 | |
public void setShowModels(boolean showModels) |
449 | |
{ |
450 | 4 | this.showModels = showModels; |
451 | 4 | } |
452 | |
|
453 | |
public boolean isShowConfig() |
454 | |
{ |
455 | 0 | return showConfig; |
456 | |
} |
457 | |
|
458 | |
public void setShowConfig(boolean showConfig) |
459 | |
{ |
460 | 4 | this.showConfig = showConfig; |
461 | 4 | } |
462 | |
|
463 | |
public boolean isShowAgents() |
464 | |
{ |
465 | 4 | return showAgents; |
466 | |
} |
467 | |
|
468 | |
public void setShowAgents(boolean showAgents) |
469 | |
{ |
470 | 4 | this.showAgents = showAgents; |
471 | 4 | } |
472 | |
|
473 | |
public boolean isShowTransformers() |
474 | |
{ |
475 | 4 | return showTransformers; |
476 | |
} |
477 | |
|
478 | |
public void setShowTransformers(boolean showTransformers) |
479 | |
{ |
480 | 4 | this.showTransformers = showTransformers; |
481 | 4 | } |
482 | |
|
483 | |
public boolean isShowAll() |
484 | |
{ |
485 | 4 | return showAll; |
486 | |
} |
487 | |
|
488 | |
public void setShowAll(boolean showAll) |
489 | |
{ |
490 | 4 | this.showAll = showAll; |
491 | 4 | showConfig = true; |
492 | 4 | showConnectors = true; |
493 | 4 | showAgents = true; |
494 | 4 | showModels = true; |
495 | 4 | showTransformers = true; |
496 | 4 | } |
497 | |
|
498 | |
public void setWorkingDirectory(String temp) |
499 | |
{ |
500 | 4 | if (temp != null) |
501 | |
{ |
502 | 4 | File f = new File(temp); |
503 | 4 | if (!f.exists()) |
504 | |
{ |
505 | 0 | f.mkdirs(); |
506 | |
} |
507 | 4 | workingDirectory = f.getAbsolutePath(); |
508 | |
} |
509 | 4 | } |
510 | |
|
511 | |
public String getWorkingDirectory() |
512 | |
{ |
513 | 0 | return workingDirectory; |
514 | |
} |
515 | |
|
516 | |
public boolean isDebug() |
517 | |
{ |
518 | 12 | return this.debug; |
519 | |
} |
520 | |
|
521 | |
public void setDebug(boolean debug) |
522 | |
{ |
523 | 0 | this.debug = debug; |
524 | 0 | } |
525 | |
} |