|
1 |
| package com.agical.rmock.core.hub; |
|
2 |
| |
|
3 |
| import java.lang.reflect.InvocationHandler; |
|
4 |
| import java.lang.reflect.InvocationTargetException; |
|
5 |
| import java.lang.reflect.Method; |
|
6 |
| import java.lang.reflect.Proxy; |
|
7 |
| import java.util.HashSet; |
|
8 |
| import java.util.Iterator; |
|
9 |
| import java.util.Set; |
|
10 |
| |
|
11 |
| import com.agical.rmock.core.exception.UnexpectedFatalSystemException; |
|
12 |
| |
|
13 |
| public class ProxySetMethodConnectionStrategy implements ConnectionStrategy { |
|
14 |
| |
|
15 |
| private final Method setter; |
|
16 |
| private final Set consumables; |
|
17 |
| private final Set consumers; |
|
18 |
| |
|
19 |
| private class ConsumableInvocationHandler implements InvocationHandler { |
|
20 |
13718
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
|
21 |
13718
| if(method.getName().equals("toString")) {
|
|
22 |
0
| return "Proxy for <"+setter.getParameterTypes()[0]+">";
|
|
23 |
| } |
|
24 |
13718
| if( consumables.isEmpty() ) {
|
|
25 |
2
| throw new ReferenceOutOfScopeException( "No reference currently in scope for " + method );
|
|
26 |
| } |
|
27 |
13716
| Object lastResult = null;
|
|
28 |
| |
|
29 |
13716
| for (Iterator iter = consumables.iterator(); iter.hasNext();) {
|
|
30 |
19117
| Object consumable = (Object) iter.next();
|
|
31 |
19117
| try {
|
|
32 |
19117
| if( consumable !=null ) {
|
|
33 |
19117
| try {
|
|
34 |
19117
| lastResult = method.invoke(consumable, args);
|
|
35 |
| } catch (AbstractMethodError e) { |
|
36 |
0
| System.err.println( consumable + "::::" + method);
|
|
37 |
0
| e.printStackTrace();
|
|
38 |
| } |
|
39 |
| } else { |
|
40 |
0
| throw new ReferenceOutOfScopeException( "Consumable is null when trying to call " + method );
|
|
41 |
| } |
|
42 |
| } |
|
43 |
| catch (IllegalAccessException e) { |
|
44 |
0
| throw new UnexpectedFatalSystemException(e);
|
|
45 |
| } catch (InvocationTargetException e) { |
|
46 |
57
| throw e.getTargetException();
|
|
47 |
| } |
|
48 |
| } |
|
49 |
13659
| return lastResult;
|
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
9335
| public ProxySetMethodConnectionStrategy(Method setter) {
|
|
54 |
9335
| this.setter = setter;
|
|
55 |
9335
| this.consumables = new HashSet();
|
|
56 |
9335
| this.consumers = new HashSet();
|
|
57 |
| } |
|
58 |
| |
|
59 |
14789
| public void connectToConsumer(Object consumer, Object consumable) {
|
|
60 |
14789
| try {
|
|
61 |
14789
| Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), setter.getParameterTypes(), new ConsumableInvocationHandler());
|
|
62 |
14789
| setter.invoke(consumer, new Object[] {proxy});
|
|
63 |
| |
|
64 |
14789
| consumables.add(consumable);
|
|
65 |
14789
| consumers.add(consumer);
|
|
66 |
| } |
|
67 |
| catch (IllegalAccessException e) { |
|
68 |
0
| throw new FatalAssignSystemException(e);
|
|
69 |
| } catch (InvocationTargetException e) { |
|
70 |
0
| throw new FatalAssignSystemException(e.getTargetException());
|
|
71 |
| } |
|
72 |
| } |
|
73 |
| |
|
74 |
19354
| public void disconnectFromConsumer(Object consumer, Object consumable) {
|
|
75 |
19354
| consumables.remove(consumable);
|
|
76 |
| |
|
77 |
19354
| if(consumables.isEmpty()) {
|
|
78 |
14217
| try {
|
|
79 |
14217
| setter.invoke(consumer, new Object[] {null});
|
|
80 |
| } |
|
81 |
| catch (IllegalAccessException e) { |
|
82 |
0
| throw new FatalAssignSystemException(e);
|
|
83 |
| } catch (InvocationTargetException e) { |
|
84 |
0
| throw new FatalAssignSystemException(e.getTargetException());
|
|
85 |
| } |
|
86 |
| } |
|
87 |
| } |
|
88 |
| } |