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