1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.scripting.component; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.routing.NestedRouter; |
19 | |
import org.mule.api.routing.NestedRouterCollection; |
20 | |
import org.mule.component.AbstractComponent; |
21 | |
import org.mule.routing.nested.DefaultNestedRouterCollection; |
22 | |
import org.mule.routing.nested.NestedInvocationHandler; |
23 | |
import org.mule.transformer.TransformerTemplate; |
24 | |
import org.mule.transport.NullPayload; |
25 | |
import org.mule.util.ClassUtils; |
26 | |
|
27 | |
import java.lang.reflect.Proxy; |
28 | |
import java.util.Collections; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
import javax.script.Bindings; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 40 | public class ScriptComponent extends AbstractComponent |
40 | |
{ |
41 | |
|
42 | 40 | protected NestedRouterCollection nestedRouter = new DefaultNestedRouterCollection(); |
43 | |
|
44 | |
private Scriptable script; |
45 | |
|
46 | |
private Map proxies; |
47 | |
|
48 | |
|
49 | |
protected void doInitialise() throws InitialisationException |
50 | |
{ |
51 | 40 | super.doInitialise(); |
52 | |
try |
53 | |
{ |
54 | 40 | configureComponentBindings(); |
55 | |
} |
56 | 0 | catch (MuleException e) |
57 | |
{ |
58 | 0 | throw new InitialisationException(e, this); |
59 | 40 | } |
60 | |
|
61 | 40 | } |
62 | |
|
63 | |
protected MuleMessage doOnCall(MuleEvent event) throws Exception |
64 | |
{ |
65 | |
|
66 | 16 | Bindings bindings = script.getScriptEngine().createBindings(); |
67 | 16 | if (proxies.size() > 0) |
68 | |
{ |
69 | 2 | bindings.putAll(proxies); |
70 | |
} |
71 | 16 | script.populateBindings(bindings, event); |
72 | 16 | Object result = script.runScript(bindings); |
73 | |
|
74 | 16 | if (result != null) |
75 | |
{ |
76 | 16 | if (result instanceof MuleMessage) |
77 | |
{ |
78 | 0 | return (MuleMessage) result; |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 16 | event.getMessage().applyTransformers(Collections.singletonList(new TransformerTemplate( |
83 | |
new TransformerTemplate.OverwitePayloadCallback(result)))); |
84 | 16 | return event.getMessage(); |
85 | |
} |
86 | |
} |
87 | |
else |
88 | |
{ |
89 | 0 | return new DefaultMuleMessage(NullPayload.getInstance()); |
90 | |
} |
91 | |
} |
92 | |
|
93 | |
|
94 | |
public Scriptable getScript() |
95 | |
{ |
96 | 0 | return script; |
97 | |
} |
98 | |
|
99 | |
public void setScript(Scriptable script) |
100 | |
{ |
101 | 40 | this.script = script; |
102 | 40 | } |
103 | |
|
104 | |
public NestedRouterCollection getNestedRouter() |
105 | |
{ |
106 | 2 | return nestedRouter; |
107 | |
} |
108 | |
|
109 | |
public void setNestedRouter(NestedRouterCollection nestedRouter) |
110 | |
{ |
111 | 0 | this.nestedRouter = nestedRouter; |
112 | 0 | } |
113 | |
|
114 | |
protected void configureComponentBindings() throws MuleException |
115 | |
{ |
116 | 40 | proxies = new HashMap(); |
117 | |
|
118 | |
|
119 | 40 | if (nestedRouter != null && nestedRouter.getRouters().size() > 0) |
120 | |
{ |
121 | 2 | for (Iterator it = nestedRouter.getRouters().iterator(); it.hasNext();) |
122 | |
{ |
123 | 4 | NestedRouter nestedRouter = (NestedRouter) it.next(); |
124 | 4 | String bindingName = ClassUtils.getSimpleName(nestedRouter.getInterface()); |
125 | 4 | if (proxies.containsKey(bindingName)) |
126 | |
{ |
127 | 2 | Object proxy = proxies.get(bindingName); |
128 | 2 | NestedInvocationHandler handler = (NestedInvocationHandler) Proxy.getInvocationHandler(proxy); |
129 | 2 | handler.addRouterForInterface(nestedRouter); |
130 | 2 | } |
131 | |
else |
132 | |
{ |
133 | 2 | Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{nestedRouter.getInterface()}, |
134 | |
new NestedInvocationHandler(nestedRouter)); |
135 | 2 | proxies.put(bindingName, proxy); |
136 | |
} |
137 | |
|
138 | 4 | } |
139 | |
} |
140 | 40 | } |
141 | |
} |