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 | 32 | public class Scriptable implements Initialisable |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 32 | 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 | 30 | if (scriptEngine == null) |
57 | |
{ |
58 | 30 | if (compiledScript == null) |
59 | |
{ |
60 | 30 | if (scriptEngineName != null) |
61 | |
{ |
62 | 18 | scriptEngine = createScriptEngine(); |
63 | |
} |
64 | 12 | else if (scriptFile != null) |
65 | |
{ |
66 | 12 | int i = scriptFile.lastIndexOf("."); |
67 | 12 | if (i > -1) |
68 | |
{ |
69 | 12 | setScriptEngineName(scriptFile.substring(i + 1)); |
70 | 12 | logger.info("Script Engine name not set. Defaulting to file extension: " |
71 | |
+ getScriptEngineName()); |
72 | 12 | scriptEngine = createScriptEngine(); |
73 | |
} |
74 | |
} |
75 | 30 | 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 | 30 | if (compiledScript == null) |
89 | |
{ |
90 | 30 | if (script == null) |
91 | |
{ |
92 | 30 | if (scriptText == null && scriptFile == null) |
93 | |
{ |
94 | 0 | throw new InitialisationException( |
95 | |
CoreMessages.propertiesNotSet("scriptText, scriptFile"), this); |
96 | |
} |
97 | 30 | else if (scriptText != null) |
98 | |
{ |
99 | 8 | script = new StringReader(scriptText); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | |
InputStream is; |
104 | |
try |
105 | |
{ |
106 | 22 | is = IOUtils.getResourceAsStream(scriptFile, getClass()); |
107 | 22 | script = new InputStreamReader(is); |
108 | |
} |
109 | 0 | catch (IOException e) |
110 | |
{ |
111 | 0 | throw new InitialisationException( |
112 | |
CoreMessages.cannotLoadFromClasspath(scriptFile), e, this); |
113 | 22 | } |
114 | |
} |
115 | |
} |
116 | |
try |
117 | |
{ |
118 | 30 | compiledScript = compileScript(script); |
119 | |
} |
120 | 0 | catch (ScriptException e) |
121 | |
{ |
122 | 0 | throw new InitialisationException(e, this); |
123 | 30 | } |
124 | |
} |
125 | 30 | } |
126 | |
|
127 | |
public ScriptEngine getScriptEngine() |
128 | |
{ |
129 | 26 | return scriptEngine; |
130 | |
} |
131 | |
|
132 | |
public void setScriptEngine(ScriptEngine scriptEngine) |
133 | |
{ |
134 | 2 | this.scriptEngine = scriptEngine; |
135 | 2 | } |
136 | |
|
137 | |
public CompiledScript getCompiledScript() |
138 | |
{ |
139 | 2 | return compiledScript; |
140 | |
} |
141 | |
|
142 | |
public void setCompiledScript(CompiledScript compiledScript) |
143 | |
{ |
144 | 2 | this.compiledScript = compiledScript; |
145 | 2 | } |
146 | |
|
147 | |
public String getScriptText() |
148 | |
{ |
149 | 2 | return scriptText; |
150 | |
} |
151 | |
|
152 | |
public void setScriptText(String scriptText) |
153 | |
{ |
154 | 16 | this.scriptText = scriptText; |
155 | 16 | } |
156 | |
|
157 | |
public String getScriptFile() |
158 | |
{ |
159 | 2 | return scriptFile; |
160 | |
} |
161 | |
|
162 | |
public void setScriptFile(String scriptFile) |
163 | |
{ |
164 | 24 | this.scriptFile = scriptFile; |
165 | 24 | } |
166 | |
|
167 | |
public void setScriptEngineName(String scriptEngineName) |
168 | |
{ |
169 | 38 | this.scriptEngineName = scriptEngineName; |
170 | 38 | } |
171 | |
|
172 | |
public String getScriptEngineName() |
173 | |
{ |
174 | 14 | return scriptEngineName; |
175 | |
} |
176 | |
|
177 | |
protected CompiledScript compileScript(Compilable compilable, Reader scriptReader) throws ScriptException |
178 | |
{ |
179 | 32 | return compilable.compile(scriptReader); |
180 | |
} |
181 | |
|
182 | |
protected CompiledScript compileScript(Reader scriptReader) throws ScriptException |
183 | |
{ |
184 | 32 | if (scriptEngine instanceof Compilable) |
185 | |
{ |
186 | 32 | Compilable compilable = (Compilable)scriptEngine; |
187 | 32 | 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 | 22 | if (compiledScript != null) |
206 | |
{ |
207 | 22 | result = compiledScript.eval(bindings); |
208 | |
} |
209 | |
else |
210 | |
{ |
211 | 0 | result = evaluteScript(bindings); |
212 | |
} |
213 | 22 | 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 | 30 | ScriptEngineManager manager = new ScriptEngineManager(); |
229 | 30 | return manager.getEngineByName(scriptEngineName); |
230 | |
} |
231 | |
|
232 | |
} |