1
2
3
4
5
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
30
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
41
42
43
44 proxy = interfaceBinding.createProxy(componentObject);
45 bindings.put(interfaceBinding.getInterface(), proxy);
46
47
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 }