1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.scripting.transformer; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.module.scripting.component.Scriptable; |
16 | |
import org.mule.transformer.AbstractMessageAwareTransformer; |
17 | |
|
18 | |
import javax.script.ScriptException; |
19 | |
import javax.script.Bindings; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 64 | public class ScriptTransformer extends AbstractMessageAwareTransformer |
25 | |
{ |
26 | |
private Scriptable script; |
27 | |
|
28 | |
public Object transform(MuleMessage message, String outputEncoding) throws TransformerException |
29 | |
{ |
30 | 20 | Bindings bindings = script.getScriptEngine().createBindings(); |
31 | 20 | script.populateBindings(bindings, message); |
32 | |
try |
33 | |
{ |
34 | 20 | return script.runScript(bindings); |
35 | |
} |
36 | 0 | catch (ScriptException e) |
37 | |
{ |
38 | 0 | throw new TransformerException(this, e); |
39 | |
} |
40 | |
} |
41 | |
|
42 | |
public Scriptable getScript() |
43 | |
{ |
44 | 0 | return script; |
45 | |
} |
46 | |
|
47 | |
public void setScript(Scriptable script) |
48 | |
{ |
49 | 64 | this.script = script; |
50 | 64 | } |
51 | |
} |