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 public class DefaultInterfaceBinding implements InterfaceBinding
32 {
33 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 OptimizedRequestContext.unsafeRewriteEvent(event.getMessage());
45 return outboundRouter.process(RequestContext.getEvent());
46 }
47
48 public void setInterface(Class<?> interfaceClass)
49 {
50 this.interfaceClass = interfaceClass;
51 }
52
53 public Class<?> getInterface()
54 {
55 return interfaceClass;
56 }
57
58 public String getMethod()
59 {
60 return methodName;
61 }
62
63 public void setMethod(String methodName)
64 {
65 this.methodName = methodName;
66 }
67
68 public Object createProxy(Object target)
69 {
70 try
71 {
72 Object proxy = Proxy.newProxyInstance(getInterface().getClassLoader(), new Class[]{getInterface()},
73 new BindingInvocationHandler(this));
74 if (logger.isDebugEnabled())
75 {
76 logger.debug("Have proxy?: " + (null != proxy));
77 }
78 return proxy;
79
80 }
81 catch (Exception e)
82 {
83 throw new MuleRuntimeException(CoreMessages.failedToCreateProxyFor(target), e);
84 }
85 }
86
87 public void setEndpoint(ImmutableEndpoint e) throws MuleException
88 {
89 if (e instanceof OutboundEndpoint)
90 {
91 outboundRouter = new OutboundPassThroughRouter();
92 outboundRouter.addRoute((OutboundEndpoint) e);
93 outboundRouter.setTransactionConfig(e.getTransactionConfig());
94 }
95 else
96 {
97 throw new IllegalArgumentException("An outbound endpoint is required for Interface binding");
98 }
99 }
100
101 public Class<?> getInterfaceClass()
102 {
103 return interfaceClass;
104 }
105
106 @Override
107 public String toString()
108 {
109 final StringBuffer sb = new StringBuffer();
110 sb.append("DefaultInterfaceBinding");
111 sb.append("{method='").append(methodName).append('\'');
112 sb.append(", interface=").append(interfaceClass);
113 sb.append('}');
114 return sb.toString();
115 }
116
117 public ImmutableEndpoint getEndpoint()
118 {
119 if (outboundRouter != null)
120 {
121 MessageProcessor target = outboundRouter.getRoutes().get(0);
122 return target instanceof ImmutableEndpoint ? (ImmutableEndpoint) target : null;
123 }
124 else
125 {
126 return null;
127 }
128 }
129 }