1 19 package org.apache.cayenne.reflect; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.map.EntityResolver; 28 import org.apache.cayenne.map.ObjEntity; 29 30 36 class LifecycleCallbackEventHandler { 37 38 private EntityResolver resolver; 39 private Map listeners; 40 private Collection defaultListeners; 41 42 LifecycleCallbackEventHandler(EntityResolver resolver) { 43 this.resolver = resolver; 44 this.listeners = new HashMap (); 45 this.defaultListeners = new ArrayList (); 46 } 47 48 private boolean excludingDefaultListeners(Class objectClass) { 49 ObjEntity entity = resolver.lookupObjEntity(objectClass); 50 return entity != null && entity.isExcludingDefaultListeners(); 51 } 52 53 private boolean excludingSuperclassListeners(Class objectClass) { 54 ObjEntity entity = resolver.lookupObjEntity(objectClass); 55 return entity != null && entity.isExcludingSuperclassListeners(); 56 } 57 58 boolean isEmpty() { 59 return listeners.isEmpty() && defaultListeners.isEmpty(); 60 } 61 62 65 void clear() { 66 listeners.clear(); 67 defaultListeners.clear(); 68 } 69 70 74 void addDefaultListener(Object listener, String methodName) { 75 CallbackOnListener callback = new CallbackOnListener(listener, methodName); 76 addDefaultCallback(callback); 77 } 78 79 82 private void addDefaultCallback(AbstractCallback callback) { 83 defaultListeners.add(callback); 84 } 85 86 90 void addListener(Class entityClass, String methodName) { 91 addCallback(entityClass, new CallbackOnEntity(entityClass, methodName)); 92 } 93 94 98 void addListener(Class entityClass, Object listener, String methodName) { 99 CallbackOnListener callback = new CallbackOnListener( 100 listener, 101 methodName, 102 entityClass); 103 addCallback(entityClass, callback); 104 } 105 106 109 private void addCallback(Class entityClass, AbstractCallback callback) { 110 Collection entityListeners = (Collection ) listeners.get(entityClass.getName()); 111 112 if (entityListeners == null) { 113 entityListeners = new ArrayList (3); 114 listeners.put(entityClass.getName(), entityListeners); 115 } 116 117 entityListeners.add(callback); 118 } 119 120 123 void performCallbacks(Object object) { 124 125 if (!defaultListeners.isEmpty() && !excludingDefaultListeners(object.getClass())) { 127 Iterator it = (Iterator ) defaultListeners.iterator(); 128 while (it.hasNext()) { 129 ((AbstractCallback) it.next()).performCallback(object); 130 } 131 } 132 133 performCallbacks(object, object.getClass()); 135 } 136 137 140 void performCallbacks(Collection objects) { 141 Iterator it = objects.iterator(); 142 while (it.hasNext()) { 143 Object object = it.next(); 144 performCallbacks(object); 145 } 146 } 147 148 152 private void performCallbacks(Object object, Class callbackEntityClass) { 153 if (Object .class.equals(callbackEntityClass) || callbackEntityClass == null) { 154 return; 155 } 156 157 if (!excludingSuperclassListeners(callbackEntityClass)) { 159 performCallbacks(object, callbackEntityClass.getSuperclass()); 160 } 161 162 String key = callbackEntityClass.getName(); 164 Collection entityListeners = (Collection ) listeners.get(key); 165 166 if (entityListeners != null) { 167 Iterator it = (Iterator ) entityListeners.iterator(); 168 while (it.hasNext()) { 169 ((AbstractCallback) it.next()).performCallback(object); 170 } 171 } 172 } 173 174 } 175 | Popular Tags |