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 | 0 | public class BindingUtils |
25 | |
{ |
26 | |
|
27 | |
public static void configureBinding(JavaComponent component, Object componentObject) throws MuleException |
28 | |
{ |
29 | |
|
30 | |
|
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 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | proxy = interfaceBinding.createProxy(componentObject); |
45 | 0 | bindings.put(interfaceBinding.getInterface(), proxy); |
46 | |
|
47 | |
|
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 | |
} |