|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
EventLinkerImpl.java | 100% | 92.9% | 100% | 95.5% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.commons.logging.Log;
|
|
26 |
import org.apache.commons.logging.LogFactory;
|
|
27 |
import org.apache.hivemind.ErrorHandler;
|
|
28 |
import org.apache.hivemind.HiveMind;
|
|
29 |
import org.apache.hivemind.Location;
|
|
30 |
import org.apache.hivemind.impl.BaseLocatable;
|
|
31 |
import org.apache.hivemind.service.EventLinker;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Implementation of {@link org.apache.hivemind.service.EventLinker}. Will output warnings
|
|
35 |
* whenever a consumer can't be registered for at least one event set (which can happen
|
|
36 |
* when the consumer does not implement the necessary interfaces).
|
|
37 |
*
|
|
38 |
* @author Howard Lewis Ship
|
|
39 |
*/
|
|
40 |
public class EventLinkerImpl extends BaseLocatable implements EventLinker |
|
41 |
{ |
|
42 |
private Log _log;
|
|
43 |
private ErrorHandler _errorHandler;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Map of {@link java.beans.EventSetDescriptor}[], keyed on producer class.
|
|
47 |
*/
|
|
48 |
private Map _producerEventSets;
|
|
49 |
|
|
50 | 9 |
public EventLinkerImpl(Log log, ErrorHandler errorHandler)
|
51 |
{ |
|
52 | 9 |
_log = log; |
53 | 9 |
_errorHandler = errorHandler; |
54 |
} |
|
55 |
|
|
56 | 10 |
public void addEventListener( |
57 |
Object producer, |
|
58 |
String eventSetName, |
|
59 |
Object consumer, |
|
60 |
Location location) |
|
61 |
{ |
|
62 | 10 |
EventSetDescriptor[] sets = getEventSets(producer); |
63 | 10 |
boolean nameMatch = HiveMind.isNonBlank(eventSetName);
|
64 | 10 |
Class consumerClass = consumer.getClass(); |
65 |
|
|
66 | 10 |
int count = 0;
|
67 | 10 |
for (int i = 0; i < sets.length; i++) |
68 |
{ |
|
69 | 9 |
EventSetDescriptor set = sets[i]; |
70 | 9 |
String name = set.getName(); |
71 |
|
|
72 | 9 |
if (nameMatch)
|
73 |
{ |
|
74 | 3 |
if (!eventSetName.equals(name))
|
75 | 1 |
continue;
|
76 |
|
|
77 | 2 |
if (isAssignable(set, consumerClass))
|
78 | 1 |
addEventListener(producer, set, consumer, location); |
79 |
else
|
|
80 |
{ |
|
81 | 1 |
_errorHandler.error( |
82 |
_log, |
|
83 |
ServiceMessages.notCompatibleWithEvent(consumer, set, producer), |
|
84 |
location, |
|
85 |
null);
|
|
86 |
} |
|
87 |
|
|
88 | 2 |
return;
|
89 |
} |
|
90 |
|
|
91 |
// Not matching on name, add anything that fits!
|
|
92 |
|
|
93 | 6 |
if (isAssignable(set, consumerClass))
|
94 |
{ |
|
95 | 5 |
addEventListener(producer, set, consumer, location); |
96 | 5 |
count++; |
97 |
} |
|
98 |
} |
|
99 |
|
|
100 | 8 |
if (count == 0)
|
101 |
{ |
|
102 | 3 |
if (nameMatch)
|
103 | 1 |
_errorHandler.error( |
104 |
_log, |
|
105 |
ServiceMessages.noSuchEventSet(producer, eventSetName), |
|
106 |
location, |
|
107 |
null);
|
|
108 |
else
|
|
109 | 2 |
_errorHandler.error( |
110 |
_log, |
|
111 |
ServiceMessages.noEventMatches(consumer, producer), |
|
112 |
location, |
|
113 |
null);
|
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 | 8 |
private boolean isAssignable(EventSetDescriptor set, Class consumerClass) |
118 |
{ |
|
119 | 8 |
return set.getListenerType().isAssignableFrom(consumerClass);
|
120 |
} |
|
121 |
|
|
122 | 6 |
private void addEventListener( |
123 |
Object producer, |
|
124 |
EventSetDescriptor set, |
|
125 |
Object consumer, |
|
126 |
Location location) |
|
127 |
{ |
|
128 | 6 |
Method m = set.getAddListenerMethod(); |
129 |
|
|
130 | 6 |
try
|
131 |
{ |
|
132 | 6 |
m.invoke(producer, new Object[] { consumer });
|
133 |
} |
|
134 |
catch (Exception ex)
|
|
135 |
{ |
|
136 | 0 |
_errorHandler.error( |
137 |
_log, |
|
138 |
ServiceMessages.unableToAddListener(producer, set, consumer, location, ex), |
|
139 |
location, |
|
140 |
ex); |
|
141 |
|
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 | 10 |
private EventSetDescriptor[] getEventSets(Object producer)
|
146 |
{ |
|
147 | 10 |
return getEventSets(producer.getClass());
|
148 |
} |
|
149 |
|
|
150 | 10 |
private synchronized EventSetDescriptor[] getEventSets(Class producerClass) |
151 |
{ |
|
152 | 10 |
EventSetDescriptor[] result = null;
|
153 |
|
|
154 | 10 |
if (_producerEventSets == null) |
155 | 9 |
_producerEventSets = new HashMap();
|
156 |
else
|
|
157 | 1 |
result = (EventSetDescriptor[]) _producerEventSets.get(producerClass); |
158 |
|
|
159 | 10 |
if (result == null) |
160 |
{ |
|
161 | 9 |
result = findEventSets(producerClass); |
162 |
|
|
163 | 9 |
_producerEventSets.put(producerClass, result); |
164 |
} |
|
165 |
|
|
166 | 10 |
return result;
|
167 |
} |
|
168 |
|
|
169 | 9 |
private EventSetDescriptor[] findEventSets(Class producerClass)
|
170 |
{ |
|
171 | 9 |
try
|
172 |
{ |
|
173 | 9 |
BeanInfo beanInfo = Introspector.getBeanInfo(producerClass); |
174 |
|
|
175 |
// Will return an empty array (not null) when the class contains
|
|
176 |
// no event sets.
|
|
177 |
|
|
178 | 9 |
return beanInfo.getEventSetDescriptors();
|
179 |
} |
|
180 |
catch (IntrospectionException ex)
|
|
181 |
{ |
|
182 | 0 |
_errorHandler.error( |
183 |
_log, |
|
184 |
ServiceMessages.unableToIntrospectClass(producerClass, ex), |
|
185 |
null,
|
|
186 |
ex); |
|
187 |
|
|
188 | 0 |
return new EventSetDescriptor[0]; |
189 |
} |
|
190 |
|
|
191 |
} |
|
192 |
|
|
193 |
} |
|
194 |
|
|