1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.ibean; |
12 | |
|
13 | |
import org.mule.api.DefaultMuleException; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.module.ibeans.config.IBeanHolder; |
19 | |
import org.mule.module.ibeans.spi.MuleIBeansPlugin; |
20 | |
import org.mule.transport.AbstractConnector; |
21 | |
import org.mule.util.ClassUtils; |
22 | |
|
23 | |
import java.lang.reflect.Method; |
24 | |
import java.util.Collections; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.ibeans.annotation.State; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class IBeansConnector extends AbstractConnector |
33 | |
{ |
34 | |
public static final String STATE_PARAMS_PROPERTY = "ibean.state.params"; |
35 | |
public static final String CALL_PARAMS_PROPERTY = "ibean.call.params"; |
36 | |
|
37 | |
private MuleIBeansPlugin iBeansPlugin; |
38 | |
|
39 | |
|
40 | |
public static final String PROTOCOL = "ibean"; |
41 | |
|
42 | |
public IBeansConnector(MuleContext context) |
43 | |
{ |
44 | 0 | super(context); |
45 | 0 | this.iBeansPlugin = new MuleIBeansPlugin(context); |
46 | |
|
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
public void doInitialise() throws InitialisationException |
51 | |
{ |
52 | |
|
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void doConnect() throws Exception |
57 | |
{ |
58 | |
|
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
public void doDisconnect() throws Exception |
63 | |
{ |
64 | |
|
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
public void doStart() throws MuleException |
69 | |
{ |
70 | |
|
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
public void doStop() throws MuleException |
75 | |
{ |
76 | |
|
77 | 0 | } |
78 | |
|
79 | |
@Override |
80 | |
public void doDispose() |
81 | |
{ |
82 | |
|
83 | 0 | } |
84 | |
|
85 | |
public String getProtocol() |
86 | |
{ |
87 | 0 | return PROTOCOL; |
88 | |
} |
89 | |
|
90 | |
public MuleIBeansPlugin getiBeansPlugin() |
91 | |
{ |
92 | 0 | return iBeansPlugin; |
93 | |
} |
94 | |
|
95 | |
public void setiBeansPlugin(MuleIBeansPlugin iBeansPlugin) |
96 | |
{ |
97 | 0 | this.iBeansPlugin = iBeansPlugin; |
98 | 0 | } |
99 | |
|
100 | |
Object createIbean(EndpointURI uri, List<?> state) throws MuleException |
101 | |
{ |
102 | |
try { |
103 | |
Object ibean; |
104 | 0 | String address = uri.getAddress(); |
105 | 0 | int i = address.indexOf("."); |
106 | 0 | String ibeanName = address.substring(0, i); |
107 | 0 | IBeanHolder holder = getMuleContext().getRegistry().lookupObject(ibeanName); |
108 | 0 | if(holder==null) |
109 | |
{ |
110 | 0 | throw new IllegalArgumentException(); |
111 | |
} |
112 | 0 | ibean = holder.create(getMuleContext(), getiBeansPlugin()); |
113 | |
|
114 | 0 | if(state.size() > 0) |
115 | |
{ |
116 | 0 | Class[] types = new Class[state.size()]; |
117 | 0 | Object[] params = new Object[state.size()]; |
118 | 0 | int x = 0; |
119 | 0 | for (Object o : state) |
120 | |
{ |
121 | 0 | types[x] = o.getClass(); |
122 | 0 | params[x++] = o; |
123 | |
} |
124 | |
|
125 | 0 | List<Method> methods = ClassUtils.getSatisfiableMethods(holder.getIbeanClass(), types, |
126 | |
true, false, Collections.<String>emptyList(), null); |
127 | 0 | if(methods.size()==0) |
128 | |
{ |
129 | 0 | throw new IllegalArgumentException("no matching methods"); |
130 | |
} |
131 | 0 | else if(methods.size()==1) |
132 | |
{ |
133 | 0 | if(methods.get(0).isAnnotationPresent(State.class)) |
134 | |
{ |
135 | 0 | methods.get(0).invoke(ibean, params); |
136 | |
} |
137 | |
} |
138 | |
else |
139 | |
{ |
140 | 0 | boolean match = false; |
141 | 0 | for (Method method1 : methods) |
142 | |
{ |
143 | 0 | if(method1.isAnnotationPresent(State.class)) |
144 | |
{ |
145 | 0 | method1.invoke(ibean, params); |
146 | 0 | match = true; |
147 | 0 | break; |
148 | |
} |
149 | |
} |
150 | 0 | if(!match) |
151 | |
{ |
152 | 0 | throw new IllegalArgumentException("no matching @State method"); |
153 | |
} |
154 | |
} |
155 | |
} |
156 | 0 | return ibean; |
157 | |
} |
158 | 0 | catch (Exception e) |
159 | |
{ |
160 | 0 | throw new DefaultMuleException(e); |
161 | |
} |
162 | |
} |
163 | |
} |