View Javadoc

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