Coverage Report - org.mule.components.script.AbstractScriptComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractScriptComponent
0%
0/53
0%
0/8
1.8
 
 1  
 /*
 2  
  * $Id: AbstractScriptComponent.java 7976 2007-08-21 14:26:13Z 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.components.script;
 12  
 
 13  
 import org.mule.impl.UMODescriptorAware;
 14  
 import org.mule.umo.UMODescriptor;
 15  
 import org.mule.umo.UMOException;
 16  
 import org.mule.umo.lifecycle.Callable;
 17  
 import org.mule.umo.lifecycle.Initialisable;
 18  
 import org.mule.umo.lifecycle.InitialisationException;
 19  
 import org.mule.umo.lifecycle.Lifecycle;
 20  
 import org.mule.util.ClassUtils;
 21  
 import org.mule.util.FileUtils;
 22  
 import org.mule.util.monitor.FileListener;
 23  
 import org.mule.util.monitor.FileMonitor;
 24  
 
 25  
 import java.io.File;
 26  
 import java.net.MalformedURLException;
 27  
 import java.net.URL;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 /**
 33  
  * <code>AbstractScriptComponent</code> is a component that can execute scripts as
 34  
  * components in Mule. This component also supports reloading if the script file
 35  
  * changes (providing the file is on the file system)
 36  
  * 
 37  
  */
 38  0
 public abstract class AbstractScriptComponent
 39  
     implements Initialisable, Lifecycle, UMODescriptorAware, FileListener, Callable
 40  
 {
 41  
     public static final int DEFAULT_RELOAD_INTERVAL_MS = 60000;
 42  
 
 43  
     /**
 44  
      * logger used by this class
 45  
      */
 46  0
     protected transient Log logger = LogFactory.getLog(getClass());
 47  0
     private String script = null;
 48  
 
 49  0
     private String scriptText = null;
 50  0
     private boolean autoReload = true;
 51  
     protected UMODescriptor descriptor;
 52  
     private FileMonitor monitor;
 53  0
     private long reloadInterval = DEFAULT_RELOAD_INTERVAL_MS;
 54  
 
 55  
     public void setDescriptor(UMODescriptor descriptor)
 56  
     {
 57  0
         this.descriptor = descriptor;
 58  0
     }
 59  
 
 60  
     public void initialise() throws InitialisationException
 61  
     {
 62  0
         if (getScript() == null && getScriptText() == null)
 63  
         {
 64  0
             String extension = getDefaultFileExtension();
 65  0
             if (!extension.startsWith("."))
 66  
             {
 67  0
                 extension = "." + extension;
 68  
             }
 69  0
             setScript(descriptor.getName() + extension);
 70  0
             logger.info("script name is not set, using default: " + descriptor.getName() + extension);
 71  
         }
 72  
 
 73  0
         if (getScriptText() != null)
 74  
         {
 75  0
             loadInterpreter(getScriptText());
 76  
         }
 77  
         else
 78  
         {
 79  
             // load script before creating a file monitor so that the script name
 80  
             // can be monified
 81  0
             loadInterpreter(getScriptUrl(getScript()));
 82  
         }
 83  0
         if (autoReload)
 84  
         {
 85  0
             File f = FileUtils.newFile(getScript());
 86  0
             if (f.exists())
 87  
             {
 88  0
                 monitor = new FileMonitor(reloadInterval);
 89  0
                 monitor.addFile(f);
 90  0
                 monitor.addListener(this);
 91  0
                 logger.debug("Component script is reloadable");
 92  
             }
 93  
             else
 94  
             {
 95  0
                 logger.warn("Cannot setup autoreload as the script fie is not on the local file system");
 96  
             }
 97  
         }
 98  0
     }
 99  
 
 100  
     protected URL getScriptUrl(String scriptLocation)
 101  
     {
 102  0
         File f = FileUtils.newFile(scriptLocation);
 103  0
         if (f.exists())
 104  
         {
 105  
             try
 106  
             {
 107  0
                 return f.toURL();
 108  
             }
 109  0
             catch (MalformedURLException e)
 110  
             {
 111  0
                 logger.error("Failed to create URL from file: " + f.getAbsolutePath(), e);
 112  0
                 return null;
 113  
             }
 114  
         }
 115  
         else
 116  
         {
 117  0
             return ClassUtils.getResource(scriptLocation, getClass());
 118  
         }
 119  
     }
 120  
 
 121  
     public String getScript()
 122  
     {
 123  0
         return script;
 124  
     }
 125  
 
 126  
     public void setScript(String script)
 127  
     {
 128  0
         this.script = script;
 129  0
     }
 130  
 
 131  
     public boolean isAutoReload()
 132  
     {
 133  0
         return autoReload;
 134  
     }
 135  
 
 136  
     public void setAutoReload(boolean autoReload)
 137  
     {
 138  0
         this.autoReload = autoReload;
 139  0
     }
 140  
 
 141  
     public void start() throws UMOException
 142  
     {
 143  0
         if (monitor != null)
 144  
         {
 145  0
             monitor.start();
 146  
         }
 147  0
     }
 148  
 
 149  
     public void stop() throws UMOException
 150  
     {
 151  0
         if (monitor != null)
 152  
         {
 153  0
             monitor.stop();
 154  
         }
 155  0
     }
 156  
 
 157  
     public void dispose()
 158  
     {
 159  
         try
 160  
         {
 161  0
             stop();
 162  
         }
 163  0
         catch (UMOException e)
 164  
         {
 165  0
             logger.error(e.getMessage(), e);
 166  0
         }
 167  0
     }
 168  
 
 169  
     public String getScriptText()
 170  
     {
 171  0
         return scriptText;
 172  
     }
 173  
 
 174  
     public void setScriptText(String scriptText)
 175  
     {
 176  0
         this.scriptText = scriptText;
 177  0
     }
 178  
 
 179  
     protected abstract void loadInterpreter(URL script) throws InitialisationException;
 180  
 
 181  
     protected abstract void loadInterpreter(String scriptText) throws InitialisationException;
 182  
 
 183  
     protected abstract String getDefaultFileExtension();
 184  
 }