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