1
2
3
4
5
6
7
8
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
36
37
38 public class Scriptable implements Initialisable
39 {
40
41
42
43
44 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 if (scriptEngine == null)
57 {
58 if (compiledScript == null)
59 {
60 if (scriptEngineName != null)
61 {
62 scriptEngine = createScriptEngine();
63 }
64 else if (scriptFile != null)
65 {
66 int i = scriptFile.lastIndexOf(".");
67 if (i > -1)
68 {
69 setScriptEngineName(scriptFile.substring(i + 1));
70 logger.info("Script Engine name not set. Defaulting to file extension: "
71 + getScriptEngineName());
72 scriptEngine = createScriptEngine();
73 }
74 }
75 if (scriptEngine == null)
76 {
77 throw new InitialisationException(
78 CoreMessages.propertiesNotSet("scriptEngine, scriptEngineName, compiledScript"),
79 this);
80 }
81 }
82 else
83 {
84 scriptEngine = compiledScript.getEngine();
85 }
86 }
87
88 if (compiledScript == null)
89 {
90 if (script == null)
91 {
92 if (scriptText == null && scriptFile == null)
93 {
94 throw new InitialisationException(
95 CoreMessages.propertiesNotSet("scriptText, scriptFile"), this);
96 }
97 else if (scriptText != null)
98 {
99 script = new StringReader(scriptText);
100 }
101 else
102 {
103 InputStream is;
104 try
105 {
106 is = IOUtils.getResourceAsStream(scriptFile, getClass());
107 script = new InputStreamReader(is);
108 }
109 catch (IOException e)
110 {
111 throw new InitialisationException(
112 CoreMessages.cannotLoadFromClasspath(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(Bindings bindings) throws ScriptException
198 {
199 return scriptEngine.eval(scriptText, bindings);
200 }
201
202 public Object runScript(Bindings bindings) throws ScriptException
203 {
204 Object result;
205 if (compiledScript != null)
206 {
207 result = compiledScript.eval(bindings);
208 }
209 else
210 {
211 result = evaluteScript(bindings);
212 }
213 return result;
214 }
215
216 public Object runScript(CompiledScript compiledScript, Bindings bindings) throws ScriptException
217 {
218 Object result = null;
219 if (compiledScript != null)
220 {
221 result = compiledScript.eval(bindings);
222 }
223 return result;
224 }
225
226 protected ScriptEngine createScriptEngine()
227 {
228 ScriptEngineManager manager = new ScriptEngineManager();
229 return manager.getEngineByName(scriptEngineName);
230 }
231
232 }