Coverage Report - org.mule.tools.visualizer.config.VelocitySupport
 
Classes in this File Line Coverage Branch Coverage Complexity
VelocitySupport
87%
33/38
39%
7/18
2
 
 1  
 /*
 2  
  * $Id: VelocitySupport.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 org.mule.tools.visualizer.postrenderers.MuleDocPostRenderer;
 14  
 import org.mule.tools.visualizer.util.VelocityLogger;
 15  
 import org.mule.util.FileUtils;
 16  
 import org.mule.util.IOUtils;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 import java.net.URL;
 22  
 
 23  
 import org.apache.velocity.app.VelocityEngine;
 24  
 import org.apache.velocity.runtime.log.LogSystem;
 25  
 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
 26  
 
 27  
 public abstract class VelocitySupport
 28  
 {
 29  
 
 30  
     public static final String FILE = "file";
 31  
     public static final String JAR = "jar";
 32  
     // this loads from the claspath
 33  
     // http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html
 34  
     public static final String MAGIC_VELOCITY_RESOURCE_LOADER = "class.resource.loader.class";
 35  
 
 36  
     private VelocityEngine ve;
 37  
 
 38  12
     private GraphEnvironment env = null;
 39  
 
 40  
     private static LogSystem logSystem;
 41  
 
 42  
     protected VelocitySupport(GraphEnvironment env) throws Exception
 43  12
     {
 44  12
         this.setEnv(env);
 45  12
         logSystem = new VelocityLogger(env);
 46  12
         setVe(new VelocityEngine());
 47  12
         getVe().setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, logSystem);
 48  12
         inferVelocityLoaderPath(getVe());
 49  12
         getVe().init();
 50  12
     }
 51  
 
 52  
     /**
 53  
      * This is something of a hack.  Velocity loads files relative to the property 
 54  
      * VelocityEngine.FILE_RESOURCE_LOADER_PATH.  So if we can find the template ourselves then 
 55  
      * we can infer what the property value should be so that velocity works ok.
 56  
      */
 57  
     private void inferVelocityLoaderPath(VelocityEngine ve) throws IOException
 58  
     {
 59  12
         String defaultPath = (String) ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH);
 60  12
         if (null == defaultPath || StringUtils.isEmpty(defaultPath))
 61  
         {
 62  12
             URL url = IOUtils.getResourceAsUrl(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE, getClass());
 63  12
             if (FILE.equals(url.getProtocol()))
 64  
             {
 65  12
                 String path = FileUtils.getResourcePath(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE, getClass());
 66  12
                 if (!StringUtils.isEmpty(path))
 67  
                 {
 68  12
                     File fullPath = new File(path);
 69  12
                     File target = new File(MuleDocPostRenderer.DEFAULT_MULE_TEMPLATE);
 70  
 
 71  
                     // drop trailing files until we are at the relative parent
 72  36
                     while (null != target && !StringUtils.isEmpty(target.getPath()))
 73  
                     {
 74  24
                         env.log(fullPath.getPath() + " - " + target.getPath());
 75  24
                         target = target.getParentFile();
 76  24
                         fullPath = fullPath.getParentFile();
 77  
                     }
 78  
 
 79  12
                     path = fullPath.getPath();
 80  12
                     if (path.endsWith("!"))
 81  
                     {
 82  0
                         path = path + File.separator;
 83  
                     }
 84  12
                     getEnv().log(VelocityEngine.FILE_RESOURCE_LOADER_PATH + " = " + path);
 85  12
                     ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path);
 86  
                 }
 87  12
             }
 88  0
             else if (JAR.equals(url.getProtocol()))
 89  
             {
 90  0
                 env.log(url.toString());
 91  0
                 ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
 92  0
                 ve.setProperty(MAGIC_VELOCITY_RESOURCE_LOADER, ClasspathResourceLoader.class.getName());
 93  
             }
 94  
         }
 95  12
     }
 96  
 
 97  
     protected void setEnv(GraphEnvironment env)
 98  
     {
 99  20
         this.env = env;
 100  20
     }
 101  
 
 102  
     protected GraphEnvironment getEnv()
 103  
     {
 104  12
         return env;
 105  
     }
 106  
 
 107  
     protected void setVe(VelocityEngine ve)
 108  
     {
 109  12
         this.ve = ve;
 110  12
     }
 111  
 
 112  
     protected VelocityEngine getVe()
 113  
     {
 114  48
         return ve;
 115  
     }
 116  
 
 117  
 }