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 | 0 | protected static Log logger = LogFactory.getLog(NestedInvocationHandler.class); |
36 | |
|
37 | 0 | protected Map routers = new ConcurrentHashMap(); |
38 | |
|
39 | |
protected NestedInvocationHandler(UMONestedRouter router) |
40 | 0 | { |
41 | 0 | addRouterForInterface(router); |
42 | 0 | } |
43 | |
|
44 | |
public void addRouterForInterface(UMONestedRouter router) |
45 | |
{ |
46 | 0 | if (router.getMethod() == null) |
47 | |
{ |
48 | 0 | if (routers.size() == 0) |
49 | |
{ |
50 | 0 | routers.put(DEFAULT_METHOD_NAME_TOKEN, router); |
51 | |
} |
52 | |
else |
53 | |
{ |
54 | 0 | throw new IllegalArgumentException(CoreMessages.mustSetMethodNamesOnBinding().getMessage()); |
55 | |
} |
56 | |
} |
57 | |
else |
58 | |
{ |
59 | 0 | routers.put(router.getMethod(), router); |
60 | |
} |
61 | 0 | } |
62 | |
|
63 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
64 | |
{ |
65 | |
|
66 | 0 | UMOMessage message = new MuleMessage(args); |
67 | 0 | UMONestedRouter router = (UMONestedRouter) routers.get(method.getName()); |
68 | 0 | if (router == null) |
69 | |
{ |
70 | 0 | router = (UMONestedRouter) routers.get(DEFAULT_METHOD_NAME_TOKEN); |
71 | |
} |
72 | |
|
73 | 0 | if (router == null) |
74 | |
{ |
75 | 0 | throw new IllegalArgumentException( |
76 | |
CoreMessages.cannotFindBindingForMethod(method.getName()).toString()); |
77 | |
} |
78 | 0 | UMOEndpoint endpoint = router.getEndpoint(); |
79 | |
|
80 | |
UMOMessage reply; |
81 | |
|
82 | 0 | UMOEvent currentEvent = RequestContext.getEvent(); |
83 | 0 | final MuleEvent event = new MuleEvent(message, endpoint, currentEvent.getComponent(), currentEvent); |
84 | |
|
85 | 0 | reply = router.route(event); |
86 | |
|
87 | 0 | if (reply != null) |
88 | |
{ |
89 | 0 | return reply.getPayload(); |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | return null; |
94 | |
} |
95 | |
} |
96 | |
} |