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