1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformers.script; |
12 | |
|
13 | |
import org.mule.components.script.jsr223.Scriptable; |
14 | |
import org.mule.transformers.AbstractEventAwareTransformer; |
15 | |
import org.mule.umo.UMOEventContext; |
16 | |
import org.mule.umo.lifecycle.InitialisationException; |
17 | |
import org.mule.umo.transformer.TransformerException; |
18 | |
|
19 | |
import javax.script.Bindings; |
20 | |
import javax.script.CompiledScript; |
21 | |
import javax.script.ScriptEngine; |
22 | |
import javax.script.ScriptException; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class ScriptTransformer extends AbstractEventAwareTransformer |
28 | |
{ |
29 | 0 | protected final Scriptable scriptable = new Scriptable(); |
30 | |
|
31 | |
public ScriptTransformer() |
32 | |
{ |
33 | 0 | super(); |
34 | 0 | } |
35 | |
|
36 | |
public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException |
37 | |
{ |
38 | 0 | Bindings bindings = this.getScriptEngine().createBindings(); |
39 | 0 | this.populateBindings(bindings, context, src); |
40 | |
|
41 | |
try |
42 | |
{ |
43 | 0 | return scriptable.runScript(bindings); |
44 | |
} |
45 | 0 | catch (ScriptException e) |
46 | |
{ |
47 | 0 | throw new TransformerException(this, e); |
48 | |
} |
49 | |
} |
50 | |
|
51 | |
protected void populateBindings(Bindings namespace, UMOEventContext context, Object src) |
52 | |
{ |
53 | 0 | namespace.put("eventContext", context); |
54 | 0 | namespace.put("message", context.getMessage()); |
55 | 0 | namespace.put("src", src); |
56 | 0 | namespace.put("transformerNamespace", namespace); |
57 | 0 | namespace.put("log", logger); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void initialise() throws InitialisationException |
67 | |
{ |
68 | 0 | super.initialise(); |
69 | 0 | scriptable.initialise(); |
70 | 0 | } |
71 | |
|
72 | |
public ScriptEngine getScriptEngine() |
73 | |
{ |
74 | 0 | return scriptable.getScriptEngine(); |
75 | |
} |
76 | |
|
77 | |
public void setScriptEngine(ScriptEngine scriptEngine) |
78 | |
{ |
79 | 0 | scriptable.setScriptEngine(scriptEngine); |
80 | 0 | } |
81 | |
|
82 | |
public CompiledScript getCompiledScript() |
83 | |
{ |
84 | 0 | return scriptable.getCompiledScript(); |
85 | |
} |
86 | |
|
87 | |
public void setCompiledScript(CompiledScript compiledScript) |
88 | |
{ |
89 | 0 | scriptable.setCompiledScript(compiledScript); |
90 | 0 | } |
91 | |
|
92 | |
public String getScriptText() |
93 | |
{ |
94 | 0 | return scriptable.getScriptText(); |
95 | |
} |
96 | |
|
97 | |
public void setScriptText(String scriptText) |
98 | |
{ |
99 | 0 | scriptable.setScriptText(scriptText); |
100 | 0 | } |
101 | |
|
102 | |
public String getScriptFile() |
103 | |
{ |
104 | 0 | return scriptable.getScriptFile(); |
105 | |
} |
106 | |
|
107 | |
public void setScriptFile(String scriptFile) |
108 | |
{ |
109 | 0 | scriptable.setScriptFile(scriptFile); |
110 | 0 | } |
111 | |
|
112 | |
public void setScriptEngineName(String scriptEngineName) |
113 | |
{ |
114 | 0 | scriptable.setScriptEngineName(scriptEngineName); |
115 | 0 | } |
116 | |
|
117 | |
public String getScriptEngineName() |
118 | |
{ |
119 | 0 | return scriptable.getScriptEngineName(); |
120 | |
} |
121 | |
|
122 | |
} |