1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.ibeans.spi; |
11 | |
|
12 | |
import org.mule.DefaultMuleEvent; |
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.MuleSession; |
18 | |
import org.mule.api.component.InterfaceBinding; |
19 | |
import org.mule.api.context.MuleContextAware; |
20 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
21 | |
import org.mule.config.i18n.CoreMessages; |
22 | |
import org.mule.message.DefaultExceptionPayload; |
23 | |
import org.mule.module.ibeans.config.IBeanFlowConstruct; |
24 | |
import org.mule.session.DefaultMuleSession; |
25 | |
import org.mule.transport.NullPayload; |
26 | |
import org.mule.util.StringMessageUtils; |
27 | |
|
28 | |
import java.lang.reflect.Method; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
import org.ibeans.api.ClientAnnotationHandler; |
35 | |
import org.ibeans.api.InvocationContext; |
36 | |
import org.ibeans.api.Request; |
37 | |
import org.ibeans.api.Response; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class MuleCallAnnotationHandler implements ClientAnnotationHandler |
43 | |
{ |
44 | |
public static final String DEFAULT_METHOD_NAME_TOKEN = "default"; |
45 | |
|
46 | 0 | protected static Log logger = LogFactory.getLog(MuleCallAnnotationHandler.class); |
47 | |
|
48 | |
private MuleContext muleContext; |
49 | |
|
50 | |
private IBeanFlowConstruct flow; |
51 | |
|
52 | 0 | protected Map<String, InterfaceBinding> routers = new HashMap<String, InterfaceBinding>(); |
53 | |
|
54 | |
public MuleCallAnnotationHandler(MuleContext muleContext) |
55 | 0 | { |
56 | 0 | this.muleContext = muleContext; |
57 | 0 | } |
58 | |
|
59 | |
public void setFlow(IBeanFlowConstruct flow) |
60 | |
{ |
61 | 0 | this.flow = flow; |
62 | 0 | } |
63 | |
|
64 | |
public void addRouterForInterface(InterfaceBinding router) |
65 | |
{ |
66 | 0 | if (router instanceof MuleContextAware) |
67 | |
{ |
68 | 0 | ((MuleContextAware) router).setMuleContext(muleContext); |
69 | |
} |
70 | 0 | if (router.getMethod() == null) |
71 | |
{ |
72 | 0 | if (routers.size() == 0) |
73 | |
{ |
74 | 0 | routers.put(DEFAULT_METHOD_NAME_TOKEN, router); |
75 | |
} |
76 | |
else |
77 | |
{ |
78 | 0 | throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage()); |
79 | |
} |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | routers.put(router.getMethod(), router); |
84 | |
} |
85 | |
|
86 | |
|
87 | 0 | } |
88 | |
|
89 | |
public Response invoke(InvocationContext ctx) throws Exception |
90 | |
{ |
91 | 0 | InterfaceBinding router = routers.get(ctx.getMethod().toString()); |
92 | 0 | if (router == null) |
93 | |
{ |
94 | 0 | throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(ctx.getMethod().getName()).toString()); |
95 | |
} |
96 | 0 | router.getEndpoint().getProperties().putAll(ctx.getIBeanDefaultConfig().getPropertyParams()); |
97 | 0 | Request req = ctx.getRequest(); |
98 | 0 | MuleMessage message = ((MuleRequestMessage)req).getMessage(); |
99 | |
|
100 | 0 | if (logger.isTraceEnabled()) |
101 | |
{ |
102 | |
try |
103 | |
{ |
104 | 0 | logger.trace("Message Before invoking " |
105 | |
+ ctx.getMethod() |
106 | |
+ ": \n" |
107 | |
+ StringMessageUtils.truncate( |
108 | |
StringMessageUtils.toString(message.getPayload()), |
109 | |
2000, false)); |
110 | 0 | logger.trace("Message Headers: \n" |
111 | |
+ StringMessageUtils.headersToString(message)); |
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | |
|
116 | 0 | } |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 0 | MuleEvent replyEvent = null; |
133 | |
MuleMessage reply; |
134 | 0 | MuleSession session = new DefaultMuleSession(flow, muleContext); |
135 | |
|
136 | |
try |
137 | |
{ |
138 | 0 | replyEvent = router.process(new DefaultMuleEvent(message, router.getEndpoint(), session)); |
139 | |
} |
140 | 0 | catch (Throwable e) |
141 | |
{ |
142 | |
|
143 | 0 | reply = new DefaultMuleMessage(NullPayload.getInstance(), muleContext); |
144 | 0 | reply.setExceptionPayload(new DefaultExceptionPayload(e)); |
145 | 0 | return new MuleResponseMessage(reply); |
146 | 0 | } |
147 | 0 | return new MuleResponseMessage(replyEvent.getMessage()); |
148 | |
} |
149 | |
|
150 | |
public String getScheme(Method method) |
151 | |
{ |
152 | 0 | InterfaceBinding router = routers.get(method.toString()); |
153 | 0 | if (router == null) |
154 | |
{ |
155 | 0 | throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString()); |
156 | |
} |
157 | 0 | return router.getEndpoint().getEndpointURI().getScheme(); |
158 | |
} |
159 | |
|
160 | |
ImmutableEndpoint getEndpointForMethod(Method method) |
161 | |
{ |
162 | 0 | InterfaceBinding router = routers.get(method.toString()); |
163 | 0 | if (router != null) |
164 | |
{ |
165 | 0 | return router.getEndpoint(); |
166 | |
} |
167 | 0 | return null; |
168 | |
} |
169 | |
} |