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