View Javadoc

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