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