1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.service.impl; |
16 |
| |
17 |
| import java.beans.BeanInfo; |
18 |
| import java.beans.EventSetDescriptor; |
19 |
| import java.beans.IntrospectionException; |
20 |
| import java.beans.Introspector; |
21 |
| import java.lang.reflect.Method; |
22 |
| import java.util.HashMap; |
23 |
| import java.util.Map; |
24 |
| |
25 |
| import org.apache.hivemind.ErrorLog; |
26 |
| import org.apache.hivemind.HiveMind; |
27 |
| import org.apache.hivemind.Location; |
28 |
| import org.apache.hivemind.impl.BaseLocatable; |
29 |
| import org.apache.hivemind.service.EventLinker; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class EventLinkerImpl extends BaseLocatable implements EventLinker |
39 |
| { |
40 |
| private ErrorLog _errorLog; |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| private Map _producerEventSets; |
46 |
| |
47 |
9
| public EventLinkerImpl(ErrorLog errorLog)
|
48 |
| { |
49 |
9
| _errorLog = errorLog;
|
50 |
| } |
51 |
| |
52 |
10
| public void addEventListener(Object producer, String eventSetName, Object consumer,
|
53 |
| Location location) |
54 |
| { |
55 |
10
| EventSetDescriptor[] sets = getEventSets(producer);
|
56 |
10
| boolean nameMatch = HiveMind.isNonBlank(eventSetName);
|
57 |
10
| Class consumerClass = consumer.getClass();
|
58 |
| |
59 |
10
| int count = 0;
|
60 |
10
| for (int i = 0; i < sets.length; i++)
|
61 |
| { |
62 |
9
| EventSetDescriptor set = sets[i];
|
63 |
9
| String name = set.getName();
|
64 |
| |
65 |
9
| if (nameMatch)
|
66 |
| { |
67 |
3
| if (!eventSetName.equals(name))
|
68 |
1
| continue;
|
69 |
| |
70 |
2
| if (isAssignable(set, consumerClass))
|
71 |
1
| addEventListener(producer, set, consumer, location);
|
72 |
| else |
73 |
| { |
74 |
1
| _errorLog.error(
|
75 |
| ServiceMessages.notCompatibleWithEvent(consumer, set, producer), |
76 |
| location, |
77 |
| null); |
78 |
| } |
79 |
| |
80 |
2
| return;
|
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
6
| if (isAssignable(set, consumerClass))
|
86 |
| { |
87 |
5
| addEventListener(producer, set, consumer, location);
|
88 |
5
| count++;
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
8
| if (count == 0)
|
93 |
| { |
94 |
3
| if (nameMatch)
|
95 |
1
| _errorLog.error(
|
96 |
| ServiceMessages.noSuchEventSet(producer, eventSetName), |
97 |
| location, |
98 |
| null); |
99 |
| else |
100 |
2
| _errorLog.error(ServiceMessages.noEventMatches(consumer, producer), location, null);
|
101 |
| } |
102 |
| } |
103 |
| |
104 |
8
| private boolean isAssignable(EventSetDescriptor set, Class consumerClass)
|
105 |
| { |
106 |
8
| return set.getListenerType().isAssignableFrom(consumerClass);
|
107 |
| } |
108 |
| |
109 |
6
| private void addEventListener(Object producer, EventSetDescriptor set, Object consumer,
|
110 |
| Location location) |
111 |
| { |
112 |
6
| Method m = set.getAddListenerMethod();
|
113 |
| |
114 |
6
| try
|
115 |
| { |
116 |
6
| m.invoke(producer, new Object[]
|
117 |
| { consumer }); |
118 |
| } |
119 |
| catch (Exception ex) |
120 |
| { |
121 |
0
| _errorLog.error(ServiceMessages.unableToAddListener(
|
122 |
| producer, |
123 |
| set, |
124 |
| consumer, |
125 |
| location, |
126 |
| ex), location, ex); |
127 |
| |
128 |
| } |
129 |
| } |
130 |
| |
131 |
10
| private EventSetDescriptor[] getEventSets(Object producer)
|
132 |
| { |
133 |
10
| return getEventSets(producer.getClass());
|
134 |
| } |
135 |
| |
136 |
10
| private synchronized EventSetDescriptor[] getEventSets(Class producerClass)
|
137 |
| { |
138 |
10
| EventSetDescriptor[] result = null;
|
139 |
| |
140 |
10
| if (_producerEventSets == null)
|
141 |
9
| _producerEventSets = new HashMap();
|
142 |
| else |
143 |
1
| result = (EventSetDescriptor[]) _producerEventSets.get(producerClass);
|
144 |
| |
145 |
10
| if (result == null)
|
146 |
| { |
147 |
9
| result = findEventSets(producerClass);
|
148 |
| |
149 |
9
| _producerEventSets.put(producerClass, result);
|
150 |
| } |
151 |
| |
152 |
10
| return result;
|
153 |
| } |
154 |
| |
155 |
9
| private EventSetDescriptor[] findEventSets(Class producerClass)
|
156 |
| { |
157 |
9
| synchronized (HiveMind.INTROSPECTOR_MUTEX)
|
158 |
| { |
159 |
9
| try
|
160 |
| { |
161 |
9
| BeanInfo beanInfo = Introspector.getBeanInfo(producerClass);
|
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
9
| return beanInfo.getEventSetDescriptors();
|
167 |
| } |
168 |
| catch (IntrospectionException ex) |
169 |
| { |
170 |
0
| _errorLog.error(
|
171 |
| ServiceMessages.unableToIntrospectClass(producerClass, ex), |
172 |
| null, |
173 |
| ex); |
174 |
| |
175 |
0
| return new EventSetDescriptor[0];
|
176 |
| } |
177 |
| } |
178 |
| } |
179 |
| |
180 |
| } |