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