1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.nested; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.MuleRuntimeException; |
16 | |
import org.mule.api.MuleSession; |
17 | |
import org.mule.api.endpoint.OutboundEndpoint; |
18 | |
import org.mule.api.routing.NestedRouter; |
19 | |
import org.mule.api.routing.OutboundRouter; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
import org.mule.management.stats.RouterStatistics; |
22 | |
import org.mule.routing.AbstractRouter; |
23 | |
import org.mule.routing.outbound.OutboundPassThroughRouter; |
24 | |
|
25 | |
import java.lang.reflect.Proxy; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
|
30 | |
public class DefaultNestedRouter extends AbstractRouter implements NestedRouter |
31 | |
{ |
32 | |
|
33 | 0 | private static final Log logger = LogFactory.getLog(DefaultNestedRouter.class); |
34 | |
|
35 | |
private Class interfaceClass; |
36 | |
|
37 | |
private String methodName; |
38 | |
|
39 | |
|
40 | |
protected OutboundRouter outboundRouter; |
41 | |
|
42 | |
public DefaultNestedRouter() |
43 | 0 | { |
44 | 0 | setRouterStatistics(new RouterStatistics(RouterStatistics.TYPE_NESTED)); |
45 | 0 | } |
46 | |
|
47 | |
public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous) throws MessagingException |
48 | |
{ |
49 | 0 | return outboundRouter.route(message, session, synchronous); |
50 | |
} |
51 | |
|
52 | |
public void setInterface(Class interfaceClass) |
53 | |
{ |
54 | 0 | this.interfaceClass = interfaceClass; |
55 | 0 | } |
56 | |
|
57 | |
public Class getInterface() |
58 | |
{ |
59 | 0 | return interfaceClass; |
60 | |
} |
61 | |
|
62 | |
public String getMethod() |
63 | |
{ |
64 | 0 | return methodName; |
65 | |
} |
66 | |
|
67 | |
public void setMethod(String methodName) |
68 | |
{ |
69 | 0 | this.methodName = methodName; |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public Object createProxy(Object target) |
79 | |
{ |
80 | |
try |
81 | |
{ |
82 | 0 | Object proxy = Proxy.newProxyInstance(getInterface().getClassLoader(), new Class[]{getInterface()}, |
83 | |
new NestedInvocationHandler(this)); |
84 | 0 | logger.debug("Have proxy?: " + (null != proxy)); |
85 | 0 | return proxy; |
86 | |
|
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | throw new MuleRuntimeException(CoreMessages.failedToCreateProxyFor(target), e); |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
public void setEndpoint(OutboundEndpoint e) |
95 | |
{ |
96 | 0 | outboundRouter = new OutboundPassThroughRouter(); |
97 | 0 | outboundRouter.addEndpoint(e); |
98 | 0 | outboundRouter.setTransactionConfig(e.getTransactionConfig()); |
99 | 0 | } |
100 | |
|
101 | |
public Class getInterfaceClass() |
102 | |
{ |
103 | 0 | return interfaceClass; |
104 | |
} |
105 | |
|
106 | |
public String toString() |
107 | |
{ |
108 | 0 | final StringBuffer sb = new StringBuffer(); |
109 | 0 | sb.append("DefaultNestedRouter"); |
110 | 0 | sb.append("{method='").append(methodName).append('\''); |
111 | 0 | sb.append(", interface=").append(interfaceClass); |
112 | 0 | sb.append('}'); |
113 | 0 | return sb.toString(); |
114 | |
} |
115 | |
|
116 | |
public OutboundEndpoint getEndpoint() |
117 | |
{ |
118 | 0 | if (outboundRouter != null) |
119 | |
{ |
120 | 0 | return (OutboundEndpoint) outboundRouter.getEndpoints().get(0); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 0 | return null; |
125 | |
} |
126 | |
} |
127 | |
} |