1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.ibean; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.config.MuleProperties; |
14 | |
import org.mule.api.endpoint.OutboundEndpoint; |
15 | |
import org.mule.api.transport.DispatchException; |
16 | |
import org.mule.module.ibeans.i18n.IBeansMessages; |
17 | |
import org.mule.transport.AbstractMessageDispatcher; |
18 | |
|
19 | |
import java.lang.reflect.Method; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Collections; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.ibeans.impl.IBeansNotationHelper; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class IBeansMessageDispatcher extends AbstractMessageDispatcher |
30 | |
{ |
31 | |
|
32 | |
private Object ibean; |
33 | |
|
34 | |
|
35 | |
private String method; |
36 | |
private String ibeanName; |
37 | |
|
38 | |
public IBeansMessageDispatcher(OutboundEndpoint endpoint) throws MuleException |
39 | |
{ |
40 | 0 | super(endpoint); |
41 | 0 | List state = (List)endpoint.getProperty(IBeansConnector.STATE_PARAMS_PROPERTY); |
42 | 0 | if(state==null) |
43 | |
{ |
44 | 0 | state = Collections.emptyList(); |
45 | |
} |
46 | 0 | IBeansConnector cnn = (IBeansConnector)getConnector(); |
47 | 0 | ibean = cnn.createIbean(endpoint.getEndpointURI(), state); |
48 | 0 | ibeanName = IBeansNotationHelper.getIBeanShortID(ibean.getClass().getInterfaces()[0]); |
49 | |
|
50 | 0 | String address = endpoint.getEndpointURI().getAddress(); |
51 | 0 | method = address.substring(address.indexOf(".")+1); |
52 | |
|
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void doDispatch(MuleEvent event) throws Exception |
57 | |
{ |
58 | 0 | doSend(event); |
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
public MuleMessage doSend(MuleEvent event) throws Exception |
63 | |
{ |
64 | 0 | Object payload = event.getMessage().getPayload(); |
65 | |
Object[] params; |
66 | |
|
67 | 0 | if(payload.getClass().isArray()) |
68 | |
{ |
69 | 0 | params = (Object[])payload; |
70 | |
} |
71 | 0 | else if (payload instanceof ArrayList) |
72 | |
{ |
73 | 0 | params = ((ArrayList) payload).toArray(); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 0 | params = new Object[]{payload}; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | 0 | Class<?>[] types = new Class[params.length]; |
83 | |
|
84 | 0 | for (int i = 0; i < params.length; i++) |
85 | |
{ |
86 | 0 | types[i] = params[i].getClass(); |
87 | |
} |
88 | |
|
89 | |
Method callMethod; |
90 | |
|
91 | 0 | String methodName = event.getMessage().getInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, method); |
92 | |
try |
93 | |
{ |
94 | 0 | callMethod = ibean.getClass().getMethod(methodName, types); |
95 | |
} |
96 | 0 | catch (Throwable e) |
97 | |
{ |
98 | 0 | throw new DispatchException(IBeansMessages.ibeanMethodNotFound(ibeanName, methodName, types), event, this, e); |
99 | 0 | } |
100 | |
|
101 | 0 | Object result = callMethod.invoke(ibean, params); |
102 | 0 | return new DefaultMuleMessage(result, event.getMessage(), event.getMuleContext()); |
103 | |
} |
104 | |
} |
105 | |
|