1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.scripting.component; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.component.InterfaceBinding; |
16 | |
import org.mule.api.lifecycle.InitialisationException; |
17 | |
import org.mule.component.AbstractComponent; |
18 | |
import org.mule.component.BindingInvocationHandler; |
19 | |
import org.mule.util.ClassUtils; |
20 | |
|
21 | |
import java.lang.reflect.Proxy; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.script.Bindings; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class ScriptComponent extends AbstractComponent |
35 | |
{ |
36 | 0 | protected List<InterfaceBinding> bindings = new ArrayList<InterfaceBinding>(); |
37 | |
|
38 | |
private Scriptable script; |
39 | |
|
40 | |
private Map<String, Object> proxies; |
41 | |
|
42 | |
@Override |
43 | |
protected void doInitialise() throws InitialisationException |
44 | |
{ |
45 | 0 | script.setMuleContext(muleContext); |
46 | 0 | super.doInitialise(); |
47 | |
try |
48 | |
{ |
49 | 0 | configureComponentBindings(); |
50 | |
} |
51 | 0 | catch (MuleException e) |
52 | |
{ |
53 | 0 | throw new InitialisationException(e, this); |
54 | 0 | } |
55 | 0 | } |
56 | |
|
57 | |
@Override |
58 | |
protected Object doInvoke(MuleEvent event) throws Exception |
59 | |
{ |
60 | |
|
61 | 0 | Bindings bindings = script.getScriptEngine().createBindings(); |
62 | 0 | if (proxies.size() > 0) |
63 | |
{ |
64 | 0 | bindings.putAll(proxies); |
65 | |
} |
66 | 0 | script.populateBindings(bindings, event); |
67 | |
try |
68 | |
{ |
69 | 0 | return script.runScript(bindings); |
70 | |
} |
71 | 0 | catch (Exception e) |
72 | |
{ |
73 | |
|
74 | 0 | throw e; |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
public Scriptable getScript() |
79 | |
{ |
80 | 0 | return script; |
81 | |
} |
82 | |
|
83 | |
public void setScript(Scriptable script) |
84 | |
{ |
85 | 0 | this.script = script; |
86 | 0 | } |
87 | |
|
88 | |
public List<InterfaceBinding> getInterfaceBindings() |
89 | |
{ |
90 | 0 | return bindings; |
91 | |
} |
92 | |
|
93 | |
public void setInterfaceBindings(List<InterfaceBinding> bindingCollection) |
94 | |
{ |
95 | 0 | this.bindings = bindingCollection; |
96 | 0 | } |
97 | |
|
98 | |
protected void configureComponentBindings() throws MuleException |
99 | |
{ |
100 | 0 | proxies = new HashMap<String, Object>(); |
101 | |
|
102 | |
|
103 | 0 | if (bindings != null && bindings.size() > 0) |
104 | |
{ |
105 | 0 | for (Iterator<?> it = bindings.iterator(); it.hasNext();) |
106 | |
{ |
107 | 0 | InterfaceBinding interfaceBinding = (InterfaceBinding) it.next(); |
108 | 0 | String bindingName = ClassUtils.getSimpleName(interfaceBinding.getInterface()); |
109 | 0 | if (proxies.containsKey(bindingName)) |
110 | |
{ |
111 | 0 | Object proxy = proxies.get(bindingName); |
112 | 0 | BindingInvocationHandler handler = (BindingInvocationHandler) Proxy.getInvocationHandler(proxy); |
113 | 0 | handler.addRouterForInterface(interfaceBinding); |
114 | 0 | } |
115 | |
else |
116 | |
{ |
117 | 0 | Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), |
118 | |
new Class[]{interfaceBinding.getInterface()}, |
119 | |
new BindingInvocationHandler(interfaceBinding)); |
120 | 0 | proxies.put(bindingName, proxy); |
121 | |
} |
122 | 0 | } |
123 | |
} |
124 | 0 | } |
125 | |
} |