View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * An instance of a binding that matches iBean method name with an endpoint to invoke.
32   * 
33   * Each method annotated with {@link org.ibeans.annotation.Call} or {@link org.ibeans.annotation.Template} has an associated
34   * component binding associated with it.
35   */
36  public class CallInterfaceBinding implements InterfaceBinding, MessageProcessor
37  {
38      protected static final Log logger = LogFactory.getLog(CallInterfaceBinding.class);
39  
40      private Class<?> interfaceClass;
41  
42      private String methodName;
43  
44      // The router used to actually dispatch the message
45      protected OutboundEndpoint endpoint;
46      private FlowConstruct flow;
47      private RouterStatistics routerStatistics;
48  
49  
50      public CallInterfaceBinding(FlowConstruct flow)
51      {
52          routerStatistics = new RouterStatistics(RouterStatistics.TYPE_BINDING);
53          this.flow = flow;
54      }
55  
56      public MuleEvent process(MuleEvent event) throws MuleException
57      {
58          return endpoint.process(event);
59      }
60  
61      public MuleMessage route(MuleMessage message, MuleSession session) throws MuleException
62      {
63          //Work around for allowing the MuleCallAnnotationHandler to invoke the binding directly without having
64          //to know about the flow and create a session
65          if(session==null)
66          {
67              session = new DefaultMuleSession(flow, message.getMuleContext());
68          }
69  
70          MuleEvent result = process(new DefaultMuleEvent(message, endpoint, session));
71          if (result != null)
72          {
73              return result.getMessage();
74          }
75          else
76          {
77              return null;
78          }
79      }
80  
81      public void setInterface(Class<?> interfaceClass)
82      {
83          this.interfaceClass = interfaceClass;
84      }
85  
86      public Class<?> getInterface()
87      {
88          return interfaceClass;
89      }
90  
91      public String getMethod()
92      {
93          return methodName;
94      }
95  
96      public void setMethod(String methodName)
97      {
98          this.methodName = methodName;
99      }
100 
101     public Object createProxy(Object target)
102     {
103         try
104         {
105             Object proxy = Proxy.newProxyInstance(getInterface().getClassLoader(), new Class[]{getInterface()},
106                     new BindingInvocationHandler(this));
107             if (logger.isDebugEnabled())
108             {
109                 logger.debug("Have proxy?: " + (null != proxy));
110             }
111             return proxy;
112 
113         }
114         catch (Exception e)
115         {
116             throw new MuleRuntimeException(CoreMessages.failedToCreateProxyFor(target), e);
117         }
118     }
119 
120     public void setEndpoint(ImmutableEndpoint e)
121     {
122         if (e instanceof OutboundEndpoint)
123         {
124             endpoint = (OutboundEndpoint)e;
125         }
126         else
127         {
128             throw new IllegalArgumentException("An outbound endpoint is required for Interface binding");
129         }
130     }
131 
132     public Class<?> getInterfaceClass()
133     {
134         return interfaceClass;
135     }
136 
137     @Override
138     public String toString()
139     {
140         final StringBuffer sb = new StringBuffer();
141         sb.append("DefaultInterfaceBinding");
142         sb.append("{method='").append(methodName).append('\'');
143         sb.append(", interface=").append(interfaceClass);
144         sb.append('}');
145         return sb.toString();
146     }
147 
148     public ImmutableEndpoint getEndpoint()
149     {
150         return endpoint;
151     }
152 }