|
1 |
| package com.agical.rmock.core.hub; |
|
2 |
| |
|
3 |
| import java.beans.BeanInfo; |
|
4 |
| import java.beans.IntrospectionException; |
|
5 |
| import java.beans.Introspector; |
|
6 |
| import java.beans.PropertyDescriptor; |
|
7 |
| import java.util.HashMap; |
|
8 |
| import java.util.HashSet; |
|
9 |
| import java.util.Iterator; |
|
10 |
| import java.util.LinkedList; |
|
11 |
| import java.util.Map; |
|
12 |
| import java.util.Set; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| public class Hub { |
|
23 |
| private final Map connectionPoints; |
|
24 |
| private final Map consumerToConnectionPoints = new HashMap(); |
|
25 |
| private final Map consumableToConnectionPoints = new HashMap(); |
|
26 |
| private final LinkedList scopestack; |
|
27 |
| |
|
28 |
25
| public Hub() {
|
|
29 |
25
| super();
|
|
30 |
25
| connectionPoints = new HashMap();
|
|
31 |
25
| scopestack = new LinkedList();
|
|
32 |
25
| pushScope();
|
|
33 |
| } |
|
34 |
| |
|
35 |
584
| private void pushScope() {
|
|
36 |
584
| scopestack.add(new HashSet());
|
|
37 |
| } |
|
38 |
| |
|
39 |
4450
| private Set peekScope() {
|
|
40 |
4450
| return (Set) scopestack.getLast();
|
|
41 |
| } |
|
42 |
| |
|
43 |
559
| private Set popScope() {
|
|
44 |
559
| return (Set) scopestack.removeLast();
|
|
45 |
| } |
|
46 |
| |
|
47 |
4450
| public void connect(Object collaborator) {
|
|
48 |
4450
| connectToConsumers(collaborator, collaborator.getClass());
|
|
49 |
4450
| peekScope().add(collaborator);
|
|
50 |
4450
| registerConsumers(collaborator);
|
|
51 |
| } |
|
52 |
| |
|
53 |
21200
| private void connectToConsumers(Object collaborator, Class currentClass) {
|
|
54 |
21200
| if (currentClass.isInterface()) {
|
|
55 |
8958
| ConnectionPoint connectionPoint = getConnectionPoint(currentClass);
|
|
56 |
8958
| connectionPoint.connectConsumable(collaborator);
|
|
57 |
8958
| registerConsumable(collaborator, connectionPoint);
|
|
58 |
| } |
|
59 |
21200
| if (currentClass.getSuperclass() != null) {
|
|
60 |
7792
| connectToConsumers(collaborator, currentClass.getSuperclass());
|
|
61 |
| |
|
62 |
| } |
|
63 |
21200
| Class[] interfaces = currentClass.getInterfaces();
|
|
64 |
21200
| for (int i = 0; i < interfaces.length; i++) {
|
|
65 |
8958
| connectToConsumers(collaborator, interfaces[i]);
|
|
66 |
| } |
|
67 |
| } |
|
68 |
| |
|
69 |
4450
| private void registerConsumers(Object collaborator) {
|
|
70 |
4450
| try {
|
|
71 |
4450
| BeanInfo beanInfo = Introspector.getBeanInfo(collaborator.getClass(), Introspector.IGNORE_ALL_BEANINFO);
|
|
72 |
4450
| PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
|
73 |
4450
| for (int i = 0; i < propertyDescriptors.length; i++) {
|
|
74 |
17857
| Class type = propertyDescriptors[i].getPropertyType();
|
|
75 |
17857
| if (propertyDescriptors[i].getWriteMethod() != null && type.isInterface()) {
|
|
76 |
10481
| ConnectionPoint connectionPoint = getConnectionPoint(propertyDescriptors[i].getPropertyType());
|
|
77 |
10481
| connectionPoint.connectConsumer(collaborator);
|
|
78 |
10481
| registerConsumer(collaborator, connectionPoint);
|
|
79 |
| } |
|
80 |
| } |
|
81 |
| } catch (IntrospectionException e) { |
|
82 |
0
| throw new IntrospectionSystemException(e.getMessage());
|
|
83 |
| } |
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
19439
| private synchronized ConnectionPoint getConnectionPoint(Class currentClass) {
|
|
88 |
19439
| ConnectionPoint connectionPoint = (ConnectionPoint) connectionPoints.get(currentClass);
|
|
89 |
19439
| if(connectionPoint == null) {
|
|
90 |
268
| connectionPoint = new MultiplexingProxyConnectionPoint(currentClass, new InterfaceToInstanceConnectionStrategy(currentClass));
|
|
91 |
268
| connectionPoints.put(currentClass, connectionPoint);
|
|
92 |
| } |
|
93 |
19439
| return connectionPoint;
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
4933
| public void disconnect(Object object) {
|
|
98 |
4933
| Set consumerPoints = (Set) consumerToConnectionPoints.get(object);
|
|
99 |
4933
| Set consumablePoints = (Set) consumableToConnectionPoints.get(object);
|
|
100 |
4933
| if (consumerPoints != null) {
|
|
101 |
4931
| for (Iterator iter = consumerPoints.iterator(); iter.hasNext();) {
|
|
102 |
12660
| ConnectionPoint connectionPoint = (ConnectionPoint) iter.next();
|
|
103 |
12660
| connectionPoint.disconnectConsumer(object);
|
|
104 |
| } |
|
105 |
| } |
|
106 |
| |
|
107 |
4933
| if (consumablePoints != null) {
|
|
108 |
4932
| for (Iterator iter = consumablePoints.iterator(); iter.hasNext();) {
|
|
109 |
12142
| ConnectionPoint connectionPoint = (ConnectionPoint) iter.next();
|
|
110 |
12142
| connectionPoint.disconnectConsumable(object);
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| } |
|
114 |
| |
|
115 |
559
| public void beginScope() {
|
|
116 |
559
| pushScope();
|
|
117 |
| } |
|
118 |
| |
|
119 |
559
| public void endScope() {
|
|
120 |
559
| Set scope = popScope();
|
|
121 |
559
| for (Iterator members = scope.iterator(); members.hasNext();) {
|
|
122 |
4375
| Object element = (Object) members.next();
|
|
123 |
4375
| disconnect(element);
|
|
124 |
| } |
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
10481
| private void registerConsumer(Object consumer, ConnectionPoint connectionPoint) {
|
|
130 |
10481
| addToConnectionPointRegistry(consumer, connectionPoint, consumerToConnectionPoints);
|
|
131 |
| } |
|
132 |
| |
|
133 |
0
| private void unregisterConsumer(Object consumer, ConnectionPoint connectionPoint) {
|
|
134 |
0
| removeFromConnectionPointRegistry(consumer, connectionPoint, consumerToConnectionPoints);
|
|
135 |
| } |
|
136 |
| |
|
137 |
| |
|
138 |
8958
| private void registerConsumable(Object consumable, ConnectionPoint connectionPoint) {
|
|
139 |
8958
| addToConnectionPointRegistry(consumable, connectionPoint, consumableToConnectionPoints);
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| private void unregisterConsumable(Object consumable, ConnectionPoint connectionPoint) {
|
|
143 |
0
| removeFromConnectionPointRegistry(consumable, connectionPoint, consumableToConnectionPoints);
|
|
144 |
| } |
|
145 |
| |
|
146 |
19439
| private synchronized void addToConnectionPointRegistry(Object key, ConnectionPoint connectionPoint, Map registry) {
|
|
147 |
19439
| Set connectionPoints = (Set) registry.get(key);
|
|
148 |
19439
| if(connectionPoints == null) {
|
|
149 |
8851
| connectionPoints = new HashSet();
|
|
150 |
8851
| registry.put(key, connectionPoints);
|
|
151 |
| } |
|
152 |
19439
| connectionPoints.add(connectionPoint);
|
|
153 |
| } |
|
154 |
| |
|
155 |
0
| private synchronized void removeFromConnectionPointRegistry(Object key, ConnectionPoint connectionPoint, Map registry) {
|
|
156 |
0
| Set connectionPoints = (Set) registry.get(key);
|
|
157 |
0
| if(connectionPoints == null) {
|
|
158 |
0
| connectionPoints.remove(connectionPoint);
|
|
159 |
| } |
|
160 |
| } |
|
161 |
| } |