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.component;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.component.InterfaceBinding;
11  import org.mule.api.component.JavaComponent;
12  import org.mule.api.lifecycle.InitialisationException;
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.model.resolvers.NoSatisfiableMethodsException;
15  import org.mule.model.resolvers.TooManySatisfiableMethodsException;
16  import org.mule.util.ClassUtils;
17  
18  import java.lang.reflect.Method;
19  import java.lang.reflect.Proxy;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  public class BindingUtils
25  {
26  
27      public static void configureBinding(JavaComponent component, Object componentObject) throws MuleException
28      {
29          // Initialise the nested router and bind the endpoints to the methods using a
30          // Proxy
31          if (component.getInterfaceBindings() != null)
32          {
33              Map<Class<?>, Object> bindings = new HashMap<Class<?>, Object>();
34              for (InterfaceBinding interfaceBinding : component.getInterfaceBindings())
35              {
36                  Object proxy = bindings.get(interfaceBinding.getInterface());
37  
38                  if (proxy == null)
39                  {
40                      // Create a proxy that implements this interface
41                      // and just routes away using a mule client
42                      // ( using the high level Mule client is probably
43                      // a bit agricultural but this is just POC stuff )
44                      proxy = interfaceBinding.createProxy(componentObject);
45                      bindings.put(interfaceBinding.getInterface(), proxy);
46  
47                      // Now lets set the proxy on the Service object
48                      Method setterMethod;
49  
50                      List methods = ClassUtils.getSatisfiableMethods(componentObject.getClass(),
51                                                                      new Class[] {interfaceBinding.getInterface()}, true, false, null);
52                      if (methods.size() == 1)
53                      {
54                          setterMethod = (Method) methods.get(0);
55                      }
56                      else if (methods.size() > 1)
57                      {
58                          throw new TooManySatisfiableMethodsException(componentObject.getClass(),
59                                                                       new Class[] {interfaceBinding.getInterface()});
60                      }
61                      else
62                      {
63                          throw new NoSatisfiableMethodsException(componentObject.getClass(),
64                                                                  new Class[] {interfaceBinding.getInterface()});
65                      }
66  
67                      try
68                      {
69                          setterMethod.invoke(componentObject, proxy);
70                      }
71                      catch (Exception e)
72                      {
73                          throw new InitialisationException(CoreMessages.failedToSetProxyOnService(interfaceBinding,
74                                                                                                   componentObject.getClass()), e, null);
75                      }
76                  }
77                  else
78                  {
79                      BindingInvocationHandler handler = (BindingInvocationHandler) Proxy.getInvocationHandler(proxy);
80                      handler.addRouterForInterface(interfaceBinding);
81                  }
82              }
83          }
84      }
85  
86  }