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 protected static Log logger = LogFactory.getLog(MuleCallAnnotationHandler.class);
47
48 private MuleContext muleContext;
49
50 private IBeanFlowConstruct flow;
51
52 protected Map<String, InterfaceBinding> routers = new HashMap<String, InterfaceBinding>();
53
54 public MuleCallAnnotationHandler(MuleContext muleContext)
55 {
56 this.muleContext = muleContext;
57 }
58
59 public void setFlow(IBeanFlowConstruct flow)
60 {
61 this.flow = flow;
62 }
63
64 public void addRouterForInterface(InterfaceBinding router)
65 {
66 if (router instanceof MuleContextAware)
67 {
68 ((MuleContextAware) router).setMuleContext(muleContext);
69 }
70 if (router.getMethod() == null)
71 {
72 if (routers.size() == 0)
73 {
74 routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
75 }
76 else
77 {
78 throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
79 }
80 }
81 else
82 {
83 routers.put(router.getMethod(), router);
84 }
85
86
87 }
88
89 public Response invoke(InvocationContext ctx) throws Exception
90 {
91 InterfaceBinding router = routers.get(ctx.getMethod().toString());
92 if (router == null)
93 {
94 throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(ctx.getMethod().getName()).toString());
95 }
96 router.getEndpoint().getProperties().putAll(ctx.getIBeanDefaultConfig().getPropertyParams());
97 Request req = ctx.getRequest();
98 MuleMessage message = ((MuleRequestMessage)req).getMessage();
99
100 if (logger.isTraceEnabled())
101 {
102 try
103 {
104 logger.trace("Message Before invoking "
105 + ctx.getMethod()
106 + ": \n"
107 + StringMessageUtils.truncate(
108 StringMessageUtils.toString(message.getPayload()),
109 2000, false));
110 logger.trace("Message Headers: \n"
111 + StringMessageUtils.headersToString(message));
112 }
113 catch (Exception e)
114 {
115
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 MuleEvent replyEvent = null;
133 MuleMessage reply;
134 MuleSession session = new DefaultMuleSession(flow, muleContext);
135
136 try
137 {
138 replyEvent = router.process(new DefaultMuleEvent(message, router.getEndpoint(), session));
139 }
140 catch (Throwable e)
141 {
142
143 reply = new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
144 reply.setExceptionPayload(new DefaultExceptionPayload(e));
145 return new MuleResponseMessage(reply);
146 }
147 return new MuleResponseMessage(replyEvent.getMessage());
148 }
149
150 public String getScheme(Method method)
151 {
152 InterfaceBinding router = routers.get(method.toString());
153 if (router == null)
154 {
155 throw new IllegalArgumentException(CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
156 }
157 return router.getEndpoint().getEndpointURI().getScheme();
158 }
159
160 ImmutableEndpoint getEndpointForMethod(Method method)
161 {
162 InterfaceBinding router = routers.get(method.toString());
163 if (router != null)
164 {
165 return router.getEndpoint();
166 }
167 return null;
168 }
169 }