1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.components.script.jsr223; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.umo.UMOEventContext; |
15 | |
import org.mule.umo.lifecycle.Callable; |
16 | |
import org.mule.umo.lifecycle.InitialisationException; |
17 | |
import org.mule.util.MuleLogger; |
18 | |
|
19 | |
import javax.script.Bindings; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class ScriptComponent extends Scriptable implements Callable |
26 | |
{ |
27 | |
private Bindings bindings; |
28 | |
|
29 | |
public void initialise() throws InitialisationException |
30 | |
{ |
31 | 0 | super.initialise(); |
32 | 0 | bindings = getScriptEngine().createBindings(); |
33 | 0 | } |
34 | |
|
35 | |
public Object onCall(UMOEventContext eventContext) throws Exception |
36 | |
{ |
37 | 0 | populateBindings(bindings, eventContext); |
38 | 0 | Object result = runScript(bindings); |
39 | 0 | if (result == null) |
40 | |
{ |
41 | 0 | result = bindings.get("result"); |
42 | |
} |
43 | 0 | return result; |
44 | |
} |
45 | |
|
46 | |
protected void populateBindings(Bindings namespace, UMOEventContext context) |
47 | |
{ |
48 | 0 | namespace.put("eventContext", context); |
49 | 0 | namespace.put("managementContext", MuleManager.getInstance()); |
50 | 0 | namespace.put("message", context.getMessage()); |
51 | 0 | namespace.put("descriptor", context.getComponentDescriptor()); |
52 | 0 | namespace.put("componentNamespace", this.bindings); |
53 | 0 | namespace.put("log", new MuleLogger(logger)); |
54 | 0 | namespace.put("result", new Object()); |
55 | 0 | } |
56 | |
|
57 | |
public Bindings getBindings() |
58 | |
{ |
59 | 0 | return bindings; |
60 | |
} |
61 | |
|
62 | |
} |