1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.components.script.jsr223; |
12 | |
|
13 | |
import org.mule.components.builder.AbstractMessageBuilder; |
14 | |
import org.mule.components.builder.MessageBuilderException; |
15 | |
import org.mule.umo.UMOEventContext; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.lifecycle.Initialisable; |
18 | |
import org.mule.umo.lifecycle.InitialisationException; |
19 | |
|
20 | |
import javax.script.Bindings; |
21 | |
import javax.script.Compilable; |
22 | |
import javax.script.CompiledScript; |
23 | |
import javax.script.ScriptEngine; |
24 | |
import javax.script.ScriptException; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class ScriptMessageBuilder extends AbstractMessageBuilder implements Initialisable |
30 | |
{ |
31 | |
|
32 | |
|
33 | |
protected Scriptable scriptable; |
34 | |
|
35 | |
public ScriptMessageBuilder() |
36 | 0 | { |
37 | 0 | this.scriptable = new Scriptable(); |
38 | 0 | } |
39 | |
|
40 | |
public Object buildMessage(UMOMessage request, UMOMessage response) throws MessageBuilderException |
41 | |
{ |
42 | 0 | Bindings bindings = scriptable.getScriptEngine().createBindings(); |
43 | 0 | populateBindings(bindings, request, response); |
44 | 0 | Object result = null; |
45 | |
try |
46 | |
{ |
47 | 0 | result = runScript(bindings); |
48 | |
} |
49 | 0 | catch (ScriptException e) |
50 | |
{ |
51 | 0 | throw new MessageBuilderException(response, e); |
52 | 0 | } |
53 | 0 | if (result == null) |
54 | |
{ |
55 | 0 | throw new IllegalArgumentException("A result payload must be returned from the groovy script"); |
56 | |
} |
57 | 0 | return result; |
58 | |
} |
59 | |
|
60 | |
public void initialise() throws InitialisationException { |
61 | 0 | scriptable.initialise(); |
62 | 0 | } |
63 | |
|
64 | |
protected void populateBindings(Bindings namespace, UMOMessage request, UMOMessage response) |
65 | |
{ |
66 | 0 | namespace.put("request", request); |
67 | 0 | namespace.put("response", response); |
68 | 0 | namespace.put("descriptor", descriptor); |
69 | 0 | namespace.put("componentNamespace", namespace); |
70 | 0 | namespace.put("log", logger); |
71 | 0 | } |
72 | |
|
73 | |
public ScriptEngine getScriptEngine() |
74 | |
{ |
75 | 0 | return scriptable.getScriptEngine(); |
76 | |
} |
77 | |
|
78 | |
public void setScriptEngine(ScriptEngine scriptEngine) |
79 | |
{ |
80 | 0 | scriptable.setScriptEngine(scriptEngine); |
81 | 0 | } |
82 | |
|
83 | |
public CompiledScript getCompiledScript() |
84 | |
{ |
85 | 0 | return scriptable.getCompiledScript(); |
86 | |
} |
87 | |
|
88 | |
public void setCompiledScript(CompiledScript compiledScript) |
89 | |
{ |
90 | 0 | scriptable.setCompiledScript(compiledScript); |
91 | 0 | } |
92 | |
|
93 | |
public String getScriptText() |
94 | |
{ |
95 | 0 | return scriptable.getScriptText(); |
96 | |
} |
97 | |
|
98 | |
public void setScriptText(String scriptText) |
99 | |
{ |
100 | 0 | scriptable.setScriptText(scriptText); |
101 | 0 | } |
102 | |
|
103 | |
public String getScriptFile() |
104 | |
{ |
105 | 0 | return scriptable.getScriptFile(); |
106 | |
} |
107 | |
|
108 | |
public void setScriptFile(String scriptFile) |
109 | |
{ |
110 | 0 | scriptable.setScriptFile(scriptFile); |
111 | 0 | } |
112 | |
|
113 | |
public void setScriptEngineName(String scriptEngineName) |
114 | |
{ |
115 | 0 | scriptable.setScriptEngineName(scriptEngineName); |
116 | 0 | } |
117 | |
|
118 | |
protected void populateBindings(Bindings namespace, UMOEventContext context) |
119 | |
{ |
120 | 0 | namespace.put("context", context); |
121 | 0 | namespace.put("message", context.getMessage()); |
122 | 0 | namespace.put("descriptor", context.getComponentDescriptor()); |
123 | 0 | namespace.put("componentNamespace", namespace); |
124 | 0 | namespace.put("log", logger); |
125 | 0 | namespace.put("result", new Object()); |
126 | 0 | } |
127 | |
|
128 | |
protected void compileScript(Compilable compilable) throws ScriptException |
129 | |
{ |
130 | 0 | scriptable.compileScript(compilable); |
131 | 0 | } |
132 | |
|
133 | |
protected Object evaluteScript(Bindings namespace) throws ScriptException |
134 | |
{ |
135 | 0 | return scriptable.evaluteScript(namespace); |
136 | |
} |
137 | |
|
138 | |
protected Object runScript(Bindings namespace) throws ScriptException |
139 | |
{ |
140 | 0 | return scriptable.runScript(namespace); |
141 | |
} |
142 | |
|
143 | |
protected ScriptEngine createScriptEngine() |
144 | |
{ |
145 | 0 | return scriptable.createScriptEngine(); |
146 | |
} |
147 | |
|
148 | |
} |