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