Coverage Report - org.mule.component.BindingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BindingUtils
0%
0/23
0%
0/10
0
 
 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  0
 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  0
         if (component.getInterfaceBindings() != null)
 32  
         {
 33  0
             Map<Class<?>, Object> bindings = new HashMap<Class<?>, Object>();
 34  0
             for (InterfaceBinding interfaceBinding : component.getInterfaceBindings())
 35  
             {
 36  0
                 Object proxy = bindings.get(interfaceBinding.getInterface());
 37  
 
 38  0
                 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  0
                     proxy = interfaceBinding.createProxy(componentObject);
 45  0
                     bindings.put(interfaceBinding.getInterface(), proxy);
 46  
 
 47  
                     // Now lets set the proxy on the Service object
 48  
                     Method setterMethod;
 49  
 
 50  0
                     List methods = ClassUtils.getSatisfiableMethods(componentObject.getClass(),
 51  
                                                                     new Class[] {interfaceBinding.getInterface()}, true, false, null);
 52  0
                     if (methods.size() == 1)
 53  
                     {
 54  0
                         setterMethod = (Method) methods.get(0);
 55  
                     }
 56  0
                     else if (methods.size() > 1)
 57  
                     {
 58  0
                         throw new TooManySatisfiableMethodsException(componentObject.getClass(),
 59  
                                                                      new Class[] {interfaceBinding.getInterface()});
 60  
                     }
 61  
                     else
 62  
                     {
 63  0
                         throw new NoSatisfiableMethodsException(componentObject.getClass(),
 64  
                                                                 new Class[] {interfaceBinding.getInterface()});
 65  
                     }
 66  
 
 67  
                     try
 68  
                     {
 69  0
                         setterMethod.invoke(componentObject, proxy);
 70  
                     }
 71  0
                     catch (Exception e)
 72  
                     {
 73  0
                         throw new InitialisationException(CoreMessages.failedToSetProxyOnService(interfaceBinding,
 74  
                                                                                                  componentObject.getClass()), e, null);
 75  0
                     }
 76  0
                 }
 77  
                 else
 78  
                 {
 79  0
                     BindingInvocationHandler handler = (BindingInvocationHandler) Proxy.getInvocationHandler(proxy);
 80  0
                     handler.addRouterForInterface(interfaceBinding);
 81  
                 }
 82  0
             }
 83  
         }
 84  0
     }
 85  
 
 86  
 }