KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > EventLinkerImpl


1 // Copyright 2004, 2005 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 JavaDoc;
18 import java.beans.EventSetDescriptor JavaDoc;
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
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  * Implementation of {@link org.apache.hivemind.service.EventLinker}. Will output warnings whenever
33  * a consumer can't be registered for at least one event set (which can happen when the consumer
34  * does not implement the necessary interfaces).
35  *
36  * @author Howard Lewis Ship
37  */

38 public class EventLinkerImpl extends BaseLocatable implements EventLinker
39 {
40     private ErrorLog _errorLog;
41
42     /**
43      * Map of {@link java.beans.EventSetDescriptor}[], keyed on producer class.
44      */

45     private Map JavaDoc _producerEventSets;
46
47     public EventLinkerImpl(ErrorLog errorLog)
48     {
49         _errorLog = errorLog;
50     }
51
52     public void addEventListener(Object JavaDoc producer, String JavaDoc eventSetName, Object JavaDoc consumer,
53             Location location)
54     {
55         EventSetDescriptor JavaDoc[] sets = getEventSets(producer);
56         boolean nameMatch = HiveMind.isNonBlank(eventSetName);
57         Class JavaDoc consumerClass = consumer.getClass();
58
59         int count = 0;
60         for (int i = 0; i < sets.length; i++)
61         {
62             EventSetDescriptor JavaDoc set = sets[i];
63             String JavaDoc name = set.getName();
64
65             if (nameMatch)
66             {
67                 if (!eventSetName.equals(name))
68                     continue;
69
70                 if (isAssignable(set, consumerClass))
71                     addEventListener(producer, set, consumer, location);
72                 else
73                 {
74                     _errorLog.error(
75                             ServiceMessages.notCompatibleWithEvent(consumer, set, producer),
76                             location,
77                             null);
78                 }
79
80                 return;
81             }
82
83             // Not matching on name, add anything that fits!
84

85             if (isAssignable(set, consumerClass))
86             {
87                 addEventListener(producer, set, consumer, location);
88                 count++;
89             }
90         }
91
92         if (count == 0)
93         {
94             if (nameMatch)
95                 _errorLog.error(
96                         ServiceMessages.noSuchEventSet(producer, eventSetName),
97                         location,
98                         null);
99             else
100                 _errorLog.error(ServiceMessages.noEventMatches(consumer, producer), location, null);
101         }
102     }
103
104     private boolean isAssignable(EventSetDescriptor JavaDoc set, Class JavaDoc consumerClass)
105     {
106         return set.getListenerType().isAssignableFrom(consumerClass);
107     }
108
109     private void addEventListener(Object JavaDoc producer, EventSetDescriptor JavaDoc set, Object JavaDoc consumer,
110             Location location)
111     {
112         Method JavaDoc m = set.getAddListenerMethod();
113
114         try
115         {
116             m.invoke(producer, new Object JavaDoc[]
117             { consumer });
118         }
119         catch (Exception JavaDoc ex)
120         {
121             _errorLog.error(ServiceMessages.unableToAddListener(
122                     producer,
123                     set,
124                     consumer,
125                     location,
126                     ex), location, ex);
127
128         }
129     }
130
131     private EventSetDescriptor JavaDoc[] getEventSets(Object JavaDoc producer)
132     {
133         return getEventSets(producer.getClass());
134     }
135
136     private synchronized EventSetDescriptor JavaDoc[] getEventSets(Class JavaDoc producerClass)
137     {
138         EventSetDescriptor JavaDoc[] result = null;
139
140         if (_producerEventSets == null)
141             _producerEventSets = new HashMap JavaDoc();
142         else
143             result = (EventSetDescriptor JavaDoc[]) _producerEventSets.get(producerClass);
144
145         if (result == null)
146         {
147             result = findEventSets(producerClass);
148
149             _producerEventSets.put(producerClass, result);
150         }
151
152         return result;
153     }
154
155     private EventSetDescriptor JavaDoc[] findEventSets(Class JavaDoc producerClass)
156     {
157         synchronized (HiveMind.INTROSPECTOR_MUTEX)
158         {
159             try
160             {
161                 BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(producerClass);
162
163                 // Will return an empty array (not null) when the class contains
164
// no event sets.
165

166                 return beanInfo.getEventSetDescriptors();
167             }
168             catch (IntrospectionException JavaDoc ex)
169             {
170                 _errorLog.error(
171                         ServiceMessages.unableToIntrospectClass(producerClass, ex),
172                         null,
173                         ex);
174
175                 return new EventSetDescriptor JavaDoc[0];
176             }
177         }
178     }
179
180 }
Popular Tags