View Javadoc

1   /*
2    * $Id: GraphRenderer.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.components;
12  
13  import org.mule.tools.visualizer.config.GraphEnvironment;
14  import org.mule.tools.visualizer.postrenderers.FileCleanerPostRenderer;
15  import org.mule.tools.visualizer.postrenderers.MuleDocPostRenderer;
16  import org.mule.tools.visualizer.util.DOTtoMAP;
17  import org.mule.util.StringUtils;
18  import org.mule.util.SystemUtils;
19  
20  import com.oy.shared.lm.graph.Graph;
21  import com.oy.shared.lm.out.GRAPHtoDOTtoGIF;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.InputStreamReader;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.commons.io.FileUtils;
35  
36  public class GraphRenderer
37  {
38  
39      public static final String MULE_GRAPHVIZ = "MULE_GRAPHVIZ";
40      public static final String MULE_HOME = "MULE_HOME";
41  
42      private GraphEnvironment env;
43      private List postRenderers = new ArrayList();
44  
45      public GraphRenderer(GraphEnvironment env) throws Exception
46      {
47          this.env = env;
48          postRenderers.add(new MuleDocPostRenderer(env));
49          postRenderers.add(new FileCleanerPostRenderer());
50  
51      }
52  
53      public String saveGraph(Graph graph, String filename, File outFolder) throws IOException
54      {
55          // output graph to *.gif
56          final String dotFileName = new File(outFolder, filename + ".dot").getAbsolutePath();
57          final String mapFileName = new File(outFolder, filename + ".cmapx").getAbsolutePath();
58          final String gifFileName = new File(outFolder, filename + ".gif").getAbsolutePath();
59  
60          final String exeFile = getSaveExecutable();
61          env.log("Executing: " + exeFile);
62          GRAPHtoDOTtoGIF.transform(graph, dotFileName, gifFileName, exeFile);
63          env.log("generating MAP");
64          DOTtoMAP.transform(exeFile, dotFileName, mapFileName, env);
65  
66          Map context = new HashMap();
67          String map = FileUtils.readFileToString(new File(mapFileName), "UTF-8");
68          String path = env.getConfig().getOutputDirectory().getAbsolutePath() + File.separator;
69          context.put("dotFileName", path + filename + ".dot");
70          context.put("mapFileName", path + filename + ".cmapx");
71          context.put("mapFile", map);
72          context.put("gifFileName", filename + ".gif");
73          context.put("htmlFileName", path + filename + ".html");
74          context.put("outFolder", outFolder.getAbsolutePath());
75  
76          for (Iterator iter = postRenderers.iterator(); iter.hasNext();)
77          {
78              PostRenderer element = (PostRenderer) iter.next();
79              element.postRender(env, context, graph);
80          }
81          return gifFileName;
82      }
83  
84      private String getSaveExecutable() throws FileNotFoundException
85      {
86          if (env.getConfig().getExecuteCommand() == null)
87          {
88              String dot = SystemUtils.getenv(MULE_GRAPHVIZ);
89              if (StringUtils.isNotBlank(dot))
90              {
91                  env.getConfig().setExecuteCommand(new File(dot).getAbsolutePath());
92              }
93              else
94              {
95                  String osName = System.getProperty("os.name").toLowerCase();
96                  if (osName.startsWith("windows"))
97                  {
98                      setWindowsExecutable();
99                  }
100                 else // try anything else in a unix-like way
101                 {
102                     setUnixExecutable();
103                 }
104             }
105         }
106 
107         File f = new File(env.getConfig().getExecuteCommand());
108         if (!f.exists())
109         {
110             throw new FileNotFoundException(f.getAbsolutePath());
111         }
112 
113         return env.getConfig().getExecuteCommand();
114     }
115 
116     private void setUnixExecutable() throws FileNotFoundException
117     {
118         try 
119         {
120             Process process = Runtime.getRuntime().exec("which dot");
121             File f = new File(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine());
122             env.getConfig().setExecuteCommand(f.getAbsolutePath());
123         }
124         catch (Exception e)
125         {
126             throw (FileNotFoundException) new FileNotFoundException(
127                 "Could not find the executable 'dot': " + e.getMessage()).initCause(e);
128         }
129     }
130 
131     private void setWindowsExecutable()
132     {
133         File f = new File("win32/dot.exe");
134         if (!f.exists())
135         {
136             String home = SystemUtils.getenv(MULE_HOME);
137             if (StringUtils.isNotBlank(home))
138             {
139                 f = new File(home + "/tools/config-graph/win32/dot.exe");
140             }
141         }   
142         if (f.exists())
143         {
144             env.getConfig().setExecuteCommand(f.getAbsolutePath());
145         }
146     }
147 
148 }