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