Coverage Report - org.mule.components.script.jsr223.Scriptable
 
Classes in this File Line Coverage Branch Coverage Complexity
Scriptable
0%
0/63
0%
0/13
2.111
 
 1  
 /*
 2  
  * $Id: Scriptable.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.jsr223;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.umo.lifecycle.Initialisable;
 15  
 import org.mule.umo.lifecycle.InitialisationException;
 16  
 import org.mule.util.IOUtils;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.InputStream;
 20  
 import java.io.InputStreamReader;
 21  
 import java.io.Reader;
 22  
 import java.io.StringReader;
 23  
 
 24  
 import javax.script.Bindings;
 25  
 import javax.script.Compilable;
 26  
 import javax.script.CompiledScript;
 27  
 import javax.script.ScriptEngine;
 28  
 import javax.script.ScriptEngineManager;
 29  
 import javax.script.ScriptException;
 30  
 
 31  
 import org.apache.commons.logging.Log;
 32  
 import org.apache.commons.logging.LogFactory;
 33  
 
 34  
 /**
 35  
  * A JSR 223 Script component. Allows any JSR 223 compliant script engines such as
 36  
  * javaScript, Groovy or Rhino to be embedded as Mule components.
 37  
  */
 38  0
 public class Scriptable implements Initialisable
 39  
 {
 40  
 
 41  
     /**
 42  
      * logger used by this class
 43  
      */
 44  0
     protected transient Log logger = LogFactory.getLog(getClass());
 45  
 
 46  
     private String scriptText;
 47  
     private String scriptFile;
 48  
     private Reader script;
 49  
 
 50  
     private CompiledScript compiledScript;
 51  
     private ScriptEngine scriptEngine;
 52  
     private String scriptEngineName;
 53  
 
 54  
     public void initialise() throws InitialisationException
 55  
     {
 56  0
         if (scriptEngine == null)
 57  
         {
 58  0
             if (compiledScript == null)
 59  
             {
 60  0
                 if (scriptEngineName != null)
 61  
                 {
 62  0
                     scriptEngine = createScriptEngine();
 63  
                 }
 64  0
                 else if (scriptFile != null)
 65  
                 {
 66  0
                     int i = scriptFile.lastIndexOf(".");
 67  0
                     if (i > -1)
 68  
                     {
 69  0
                         setScriptEngineName(scriptFile.substring(i + 1));
 70  0
                         logger.info("Script Engine name not set.  Defaulting to file extension: "
 71  
                                         + getScriptEngineName());
 72  0
                         scriptEngine = createScriptEngine();
 73  
                     }
 74  
                 }
 75  0
                 if (scriptEngine == null)
 76  
                 {
 77  0
                     throw new InitialisationException(
 78  
                         CoreMessages.propertiesNotSet("scriptEngine, scriptEngineName, compiledScript"), 
 79  
                         this);
 80  
                 }
 81  
             }
 82  
             else
 83  
             {
 84  0
                 scriptEngine = compiledScript.getEngine();
 85  
             }
 86  
         }
 87  
 
 88  0
         if (compiledScript == null)
 89  
         {
 90  0
             if (script == null)
 91  
             {
 92  0
                 if (scriptText == null && scriptFile == null)
 93  
                 {
 94  0
                     throw new InitialisationException(
 95  
                         CoreMessages.propertiesNotSet("scriptText, scriptFile"), this);
 96  
                 }
 97  0
                 else if (scriptText != null)
 98  
                 {
 99  0
                     script = new StringReader(scriptText);
 100  
                 }
 101  
                 else
 102  
                 {
 103  
                     InputStream is;
 104  
                     try
 105  
                     {
 106  0
                         is = IOUtils.getResourceAsStream(scriptFile, getClass());
 107  0
                         script = new InputStreamReader(is);
 108  
                     }
 109  0
                     catch (IOException e)
 110  
                     {
 111  0
                         throw new InitialisationException(
 112  
                             CoreMessages.cannotLoadFromClasspath(scriptFile), e, this);
 113  0
                     }
 114  
                 }
 115  
             }
 116  
             try
 117  
             {
 118  0
                 compiledScript = compileScript(script);
 119  
             }
 120  0
             catch (ScriptException e)
 121  
             {
 122  0
                 throw new InitialisationException(e, this);
 123  0
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     public ScriptEngine getScriptEngine()
 128  
     {
 129  0
         return scriptEngine;
 130  
     }
 131  
 
 132  
     public void setScriptEngine(ScriptEngine scriptEngine)
 133  
     {
 134  0
         this.scriptEngine = scriptEngine;
 135  0
     }
 136  
 
 137  
     public CompiledScript getCompiledScript()
 138  
     {
 139  0
         return compiledScript;
 140  
     }
 141  
 
 142  
     public void setCompiledScript(CompiledScript compiledScript)
 143  
     {
 144  0
         this.compiledScript = compiledScript;
 145  0
     }
 146  
 
 147  
     public String getScriptText()
 148  
     {
 149  0
         return scriptText;
 150  
     }
 151  
 
 152  
     public void setScriptText(String scriptText)
 153  
     {
 154  0
         this.scriptText = scriptText;
 155  0
     }
 156  
 
 157  
     public String getScriptFile()
 158  
     {
 159  0
         return scriptFile;
 160  
     }
 161  
 
 162  
     public void setScriptFile(String scriptFile)
 163  
     {
 164  0
         this.scriptFile = scriptFile;
 165  0
     }
 166  
 
 167  
     public void setScriptEngineName(String scriptEngineName)
 168  
     {
 169  0
         this.scriptEngineName = scriptEngineName;
 170  0
     }
 171  
 
 172  
     public String getScriptEngineName()
 173  
     {
 174  0
         return scriptEngineName;
 175  
     }
 176  
 
 177  
     protected CompiledScript compileScript(Compilable compilable, Reader scriptReader) throws ScriptException
 178  
     {
 179  0
         return compilable.compile(scriptReader);
 180  
     }
 181  
 
 182  
     protected CompiledScript compileScript(Reader scriptReader) throws ScriptException
 183  
     {
 184  0
         if (scriptEngine instanceof Compilable)
 185  
         {
 186  0
             Compilable compilable = (Compilable)scriptEngine;
 187  0
             return compileScript(compilable, scriptReader);
 188  
         }
 189  0
         return null;
 190  
     }
 191  
 
 192  
     protected CompiledScript compileScript(Compilable compilable) throws ScriptException
 193  
     {
 194  0
         return compileScript(compilable, script);
 195  
     }
 196  
 
 197  
     protected Object evaluteScript(Bindings bindings) throws ScriptException
 198  
     {
 199  0
         return scriptEngine.eval(scriptText, bindings);
 200  
     }
 201  
 
 202  
     public Object runScript(Bindings bindings) throws ScriptException
 203  
     {
 204  
         Object result;
 205  0
         if (compiledScript != null)
 206  
         {
 207  0
             result = compiledScript.eval(bindings);
 208  
         }
 209  
         else
 210  
         {
 211  0
             result = evaluteScript(bindings);
 212  
         }
 213  0
         return result;
 214  
     }
 215  
 
 216  
     public Object runScript(CompiledScript compiledScript, Bindings bindings) throws ScriptException
 217  
     {
 218  0
         Object result = null;
 219  0
         if (compiledScript != null)
 220  
         {
 221  0
             result = compiledScript.eval(bindings);
 222  
         }
 223  0
         return result;
 224  
     }
 225  
 
 226  
     protected ScriptEngine createScriptEngine()
 227  
     {
 228  0
         ScriptEngineManager manager = new ScriptEngineManager();
 229  0
         return manager.getEngineByName(scriptEngineName);
 230  
     }
 231  
 
 232  
 }