1 19 package org.apache.cayenne.reflect; 20 21 import java.util.Collection ; 22 23 import org.apache.cayenne.LifecycleListener; 24 import org.apache.cayenne.map.CallbackMap; 25 import org.apache.cayenne.map.EntityResolver; 26 27 37 public class LifecycleCallbackRegistry { 38 39 private LifecycleCallbackEventHandler[] eventCallbacks; 40 41 44 public LifecycleCallbackRegistry(EntityResolver resolver) { 45 eventCallbacks = new LifecycleCallbackEventHandler[CallbackMap.CALLBACKS.length]; 46 for (int i = 0; i < eventCallbacks.length; i++) { 47 eventCallbacks[i] = new LifecycleCallbackEventHandler(resolver); 48 } 49 } 50 51 54 public void clear() { 55 for (int i = 0; i < eventCallbacks.length; i++) { 56 eventCallbacks[i].clear(); 57 } 58 } 59 60 63 public void clear(int type) { 64 eventCallbacks[type].clear(); 65 } 66 67 70 public boolean isEmpty(int type) { 71 return eventCallbacks[type].isEmpty(); 72 } 73 74 79 public void addDefaultListener(LifecycleListener listener) { 80 addDefaultListener(LifecycleListener.PRE_PERSIST, listener, "prePersist"); 81 addDefaultListener(LifecycleListener.POST_PERSIST, listener, "postPersist"); 82 addDefaultListener(LifecycleListener.PRE_REMOVE, listener, "preRemove"); 83 addDefaultListener(LifecycleListener.POST_REMOVE, listener, "postRemove"); 84 addDefaultListener(LifecycleListener.PRE_UPDATE, listener, "preUpdate"); 85 addDefaultListener(LifecycleListener.POST_UPDATE, listener, "postUpdate"); 86 addDefaultListener(LifecycleListener.POST_LOAD, listener, "postLoad"); 87 } 88 89 93 public void addDefaultListener(int type, Object listener, String methodName) { 94 eventCallbacks[type].addDefaultListener(listener, methodName); 95 } 96 97 102 public void addListener(Class entityClass, LifecycleListener listener) { 103 addListener(LifecycleListener.PRE_PERSIST, entityClass, listener, "prePersist"); 104 addListener(LifecycleListener.POST_PERSIST, entityClass, listener, "postPersist"); 105 addListener(LifecycleListener.PRE_REMOVE, entityClass, listener, "preRemove"); 106 addListener(LifecycleListener.POST_REMOVE, entityClass, listener, "postRemove"); 107 addListener(LifecycleListener.PRE_UPDATE, entityClass, listener, "preUpdate"); 108 addListener(LifecycleListener.POST_UPDATE, entityClass, listener, "postUpdate"); 109 addListener(LifecycleListener.POST_LOAD, entityClass, listener, "postLoad"); 110 } 111 112 116 public void addListener( 117 int type, 118 Class entityClass, 119 Object listener, 120 String methodName) { 121 eventCallbacks[type].addListener(entityClass, listener, methodName); 122 } 123 124 128 public void addListener(int type, Class entityClass, String methodName) { 129 eventCallbacks[type].addListener(entityClass, methodName); 130 } 131 132 135 public void performCallbacks(int type, Object object) { 136 eventCallbacks[type].performCallbacks(object); 137 } 138 139 142 public void performCallbacks(int type, Collection objects) { 143 eventCallbacks[type].performCallbacks(objects); 144 } 145 } 146 | Popular Tags |