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