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