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.MuleException; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.expression.ExpressionManager; |
18 | |
import org.mule.api.service.Service; |
19 | |
import org.mule.api.transport.Connector; |
20 | |
import org.mule.transport.AbstractPollingMessageReceiver; |
21 | |
import org.mule.transport.NullPayload; |
22 | |
|
23 | |
import java.lang.reflect.Method; |
24 | |
import java.util.Collections; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class IBeansMessageReceiver extends AbstractPollingMessageReceiver |
33 | |
{ |
34 | |
private Object ibean; |
35 | |
private Method ibeanMethod; |
36 | |
private Object[] callParams; |
37 | |
private String methodName; |
38 | |
|
39 | |
public IBeansMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint) |
40 | |
throws MuleException |
41 | |
{ |
42 | 0 | super(connector, service, endpoint); |
43 | 0 | setFrequency(60); |
44 | 0 | setTimeUnit(TimeUnit.SECONDS); |
45 | |
|
46 | 0 | List<?> state = (List)endpoint.getProperty(IBeansConnector.STATE_PARAMS_PROPERTY); |
47 | 0 | if(state==null) |
48 | |
{ |
49 | 0 | state = Collections.emptyList(); |
50 | |
} |
51 | |
|
52 | 0 | ibean = ((IBeansConnector)connector).createIbean(endpoint.getEndpointURI(), state); |
53 | |
|
54 | 0 | String address = endpoint.getEndpointURI().getAddress(); |
55 | 0 | methodName = address.substring(address.indexOf(".")+1); |
56 | |
|
57 | 0 | List params = (List)endpoint.getProperty(IBeansConnector.CALL_PARAMS_PROPERTY); |
58 | 0 | if(params==null) params = Collections.emptyList(); |
59 | |
|
60 | 0 | ExpressionManager em = connector.getMuleContext().getExpressionManager(); |
61 | 0 | MuleMessage defaultMessage = new DefaultMuleMessage(NullPayload.getInstance(), endpoint.getProperties(), connector.getMuleContext()); |
62 | |
|
63 | 0 | callParams = new Object[params.size()]; |
64 | 0 | int i = 0; |
65 | 0 | for (Object param : params) |
66 | |
{ |
67 | 0 | if(param instanceof String && em.isExpression(param.toString())) |
68 | |
{ |
69 | 0 | param = em.parse(param.toString(), defaultMessage); |
70 | |
} |
71 | 0 | callParams[i++] = param; |
72 | |
} |
73 | |
|
74 | 0 | } |
75 | |
|
76 | |
public void poll() throws Exception |
77 | |
{ |
78 | 0 | if(ibeanMethod==null) |
79 | |
{ |
80 | 0 | ibeanMethod = getMethod(); |
81 | |
} |
82 | 0 | ibeanMethod.invoke(ibean, callParams); |
83 | 0 | } |
84 | |
|
85 | |
protected Method getMethod() throws NoSuchMethodException |
86 | |
{ |
87 | 0 | Class<?>[] paramTypes = new Class<?>[callParams.length]; |
88 | 0 | int i = 0; |
89 | 0 | for (Object param : callParams) |
90 | |
{ |
91 | 0 | paramTypes[i++] = param.getClass(); |
92 | |
} |
93 | 0 | return ibean.getClass().getMethod(methodName, paramTypes); |
94 | |
} |
95 | |
} |