KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > LifecycleCallbackRegistry


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.reflect;
20
21 import java.util.Collection JavaDoc;
22
23 import org.apache.cayenne.LifecycleListener;
24 import org.apache.cayenne.map.CallbackMap;
25 import org.apache.cayenne.map.EntityResolver;
26
27 /**
28  * A registry of lifecycle callbacks for all callback event types. Valid event types are
29  * {@link LifecycleListener#PRE_PERSIST}, {@link LifecycleListener#POST_PERSIST},
30  * {@link LifecycleListener#PRE_UPDATE}, {@link LifecycleListener#POST_UPDATE},
31  * {@link LifecycleListener#PRE_REMOVE}, {@link LifecycleListener#POST_REMOVE},
32  * {@link LifecycleListener#POST_LOAD}.
33  *
34  * @since 3.0
35  * @author Andrus Adamchik
36  */

37 public class LifecycleCallbackRegistry {
38
39     private LifecycleCallbackEventHandler[] eventCallbacks;
40
41     /**
42      * Creates an empty callback registry.
43      */

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     /**
52      * Removes all listeners for all event types.
53      */

54     public void clear() {
55         for (int i = 0; i < eventCallbacks.length; i++) {
56             eventCallbacks[i].clear();
57         }
58     }
59
60     /**
61      * Removes listeners for a single event type.
62      */

63     public void clear(int type) {
64         eventCallbacks[type].clear();
65     }
66
67     /**
68      * Returns true if there are no listeners for a specific event type.
69      */

70     public boolean isEmpty(int type) {
71         return eventCallbacks[type].isEmpty();
72     }
73
74     /**
75      * Registers a {@link LifecycleListener} for all events on all entities. Note that
76      * listeners are not required to implement {@link LifecycleListener} interface. Other
77      * methods in this class can be used to register arbitrary listeners.
78      */

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     /**
90      * Registers a callback method to be invoked on a provided non-entity object when a
91      * lifecycle event occurs on any entity that does not suppress default callbacks.
92      */

93     public void addDefaultListener(int type, Object JavaDoc listener, String JavaDoc methodName) {
94         eventCallbacks[type].addDefaultListener(listener, methodName);
95     }
96
97     /**
98      * Registers a {@link LifecycleListener} for all events on all entities. Note that
99      * listeners are not required to implement {@link LifecycleListener} interface. Other
100      * methods in this class can be used to register arbitrary listeners.
101      */

102     public void addListener(Class JavaDoc 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     /**
113      * Registers callback method to be invoked on a provided non-entity object when a
114      * lifecycle event occurs for a specific entity.
115      */

116     public void addListener(
117             int type,
118             Class JavaDoc entityClass,
119             Object JavaDoc listener,
120             String JavaDoc methodName) {
121         eventCallbacks[type].addListener(entityClass, listener, methodName);
122     }
123
124     /**
125      * Registers a callback method to be invoked on an entity class instances when a
126      * lifecycle event occurs.
127      */

128     public void addListener(int type, Class JavaDoc entityClass, String JavaDoc methodName) {
129         eventCallbacks[type].addListener(entityClass, methodName);
130     }
131
132     /**
133      * Invokes callbacks of a specific type for a given entity object.
134      */

135     public void performCallbacks(int type, Object JavaDoc object) {
136         eventCallbacks[type].performCallbacks(object);
137     }
138
139     /**
140      * Invokes callbacks of a specific type for a collection of entity objects.
141      */

142     public void performCallbacks(int type, Collection JavaDoc objects) {
143         eventCallbacks[type].performCallbacks(objects);
144     }
145 }
146
Popular Tags