1 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 38 public class EventLinkerImpl extends BaseLocatable implements EventLinker 39 { 40 private ErrorLog _errorLog; 41 42 45 private Map _producerEventSets; 46 47 public EventLinkerImpl(ErrorLog errorLog) 48 { 49 _errorLog = errorLog; 50 } 51 52 public void addEventListener(Object producer, String eventSetName, Object consumer, 53 Location location) 54 { 55 EventSetDescriptor [] sets = getEventSets(producer); 56 boolean nameMatch = HiveMind.isNonBlank(eventSetName); 57 Class consumerClass = consumer.getClass(); 58 59 int count = 0; 60 for (int i = 0; i < sets.length; i++) 61 { 62 EventSetDescriptor set = sets[i]; 63 String 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 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 set, Class consumerClass) 105 { 106 return set.getListenerType().isAssignableFrom(consumerClass); 107 } 108 109 private void addEventListener(Object producer, EventSetDescriptor set, Object consumer, 110 Location location) 111 { 112 Method m = set.getAddListenerMethod(); 113 114 try 115 { 116 m.invoke(producer, new Object [] 117 { consumer }); 118 } 119 catch (Exception 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 [] getEventSets(Object producer) 132 { 133 return getEventSets(producer.getClass()); 134 } 135 136 private synchronized EventSetDescriptor [] getEventSets(Class producerClass) 137 { 138 EventSetDescriptor [] result = null; 139 140 if (_producerEventSets == null) 141 _producerEventSets = new HashMap (); 142 else 143 result = (EventSetDescriptor []) _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 [] findEventSets(Class producerClass) 156 { 157 synchronized (HiveMind.INTROSPECTOR_MUTEX) 158 { 159 try 160 { 161 BeanInfo beanInfo = Introspector.getBeanInfo(producerClass); 162 163 166 return beanInfo.getEventSetDescriptors(); 167 } 168 catch (IntrospectionException ex) 169 { 170 _errorLog.error( 171 ServiceMessages.unableToIntrospectClass(producerClass, ex), 172 null, 173 ex); 174 175 return new EventSetDescriptor [0]; 176 } 177 } 178 } 179 180 } | Popular Tags |