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