1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.nested;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.impl.MuleEvent;
15 import org.mule.impl.MuleMessage;
16 import org.mule.impl.RequestContext;
17 import org.mule.umo.UMOEvent;
18 import org.mule.umo.UMOMessage;
19 import org.mule.umo.endpoint.UMOEndpoint;
20 import org.mule.umo.routing.UMONestedRouter;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.util.Map;
25
26 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 public class NestedInvocationHandler implements InvocationHandler
31 {
32
33 public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
34
35 protected static Log logger = LogFactory.getLog(NestedInvocationHandler.class);
36
37 protected Map routers = new ConcurrentHashMap();
38
39 protected NestedInvocationHandler(UMONestedRouter router)
40 {
41 addRouterForInterface(router);
42 }
43
44 public void addRouterForInterface(UMONestedRouter router)
45 {
46 if (router.getMethod() == null)
47 {
48 if (routers.size() == 0)
49 {
50 routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
51 }
52 else
53 {
54 throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage());
55 }
56 }
57 else
58 {
59 routers.put(router.getMethod(), router);
60 }
61 }
62
63 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
64 {
65
66 UMOMessage message = new MuleMessage(args);
67 UMONestedRouter router = (UMONestedRouter) routers.get(method.getName());
68 if (router == null)
69 {
70 router = (UMONestedRouter) routers.get(DEFAULT_METHOD_NAME_TOKEN);
71 }
72
73 if (router == null)
74 {
75 throw new IllegalArgumentException(
76 CoreMessages.cannotFindBindingForMethod(method.getName()).toString());
77 }
78 UMOEndpoint endpoint = router.getEndpoint();
79
80 UMOMessage reply;
81
82 UMOEvent currentEvent = RequestContext.getEvent();
83 final MuleEvent event = new MuleEvent(message, endpoint, currentEvent.getComponent(), currentEvent);
84
85 reply = router.route(event);
86
87 if (reply != null)
88 {
89 return reply.getPayload();
90 }
91 else
92 {
93 return null;
94 }
95 }
96 }