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