KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.map.EntityResolver;
28 import org.apache.cayenne.map.ObjEntity;
29
30 /**
31  * A runtime callback processor for a single kind of lifecycle events.
32  *
33  * @since 3.0
34  * @author Andrus Adamchik
35  */

36 class LifecycleCallbackEventHandler {
37
38     private EntityResolver resolver;
39     private Map JavaDoc listeners;
40     private Collection JavaDoc defaultListeners;
41
42     LifecycleCallbackEventHandler(EntityResolver resolver) {
43         this.resolver = resolver;
44         this.listeners = new HashMap JavaDoc();
45         this.defaultListeners = new ArrayList JavaDoc();
46     }
47
48     private boolean excludingDefaultListeners(Class JavaDoc objectClass) {
49         ObjEntity entity = resolver.lookupObjEntity(objectClass);
50         return entity != null && entity.isExcludingDefaultListeners();
51     }
52
53     private boolean excludingSuperclassListeners(Class JavaDoc 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     /**
63      * Removes all listeners.
64      */

65     void clear() {
66         listeners.clear();
67         defaultListeners.clear();
68     }
69
70     /**
71      * Registers a callback method to be invoked on a provided non-entity object when a
72      * lifecycle event occurs on any entity that does not suppress default callbacks.
73      */

74     void addDefaultListener(Object JavaDoc listener, String JavaDoc methodName) {
75         CallbackOnListener callback = new CallbackOnListener(listener, methodName);
76         addDefaultCallback(callback);
77     }
78
79     /**
80      * Registers a callback object to be invoked when a lifecycle event occurs.
81      */

82     private void addDefaultCallback(AbstractCallback callback) {
83         defaultListeners.add(callback);
84     }
85
86     /**
87      * Registers a callback method to be invoked on an entity class instances when a
88      * lifecycle event occurs.
89      */

90     void addListener(Class JavaDoc entityClass, String JavaDoc methodName) {
91         addCallback(entityClass, new CallbackOnEntity(entityClass, methodName));
92     }
93
94     /**
95      * Registers callback method to be invoked on a provided non-entity object when a
96      * lifecycle event occurs.
97      */

98     void addListener(Class JavaDoc entityClass, Object JavaDoc listener, String JavaDoc methodName) {
99         CallbackOnListener callback = new CallbackOnListener(
100                 listener,
101                 methodName,
102                 entityClass);
103         addCallback(entityClass, callback);
104     }
105
106     /**
107      * Registers a callback object to be invoked when a lifecycle event occurs.
108      */

109     private void addCallback(Class JavaDoc entityClass, AbstractCallback callback) {
110         Collection JavaDoc entityListeners = (Collection JavaDoc) listeners.get(entityClass.getName());
111
112         if (entityListeners == null) {
113             entityListeners = new ArrayList JavaDoc(3);
114             listeners.put(entityClass.getName(), entityListeners);
115         }
116
117         entityListeners.add(callback);
118     }
119
120     /**
121      * Invokes callbacks for a given entity object.
122      */

123     void performCallbacks(Object JavaDoc object) {
124
125         // default listeners are invoked first
126
if (!defaultListeners.isEmpty() && !excludingDefaultListeners(object.getClass())) {
127             Iterator JavaDoc it = (Iterator JavaDoc) defaultListeners.iterator();
128             while (it.hasNext()) {
129                 ((AbstractCallback) it.next()).performCallback(object);
130             }
131         }
132
133         // apply per-entity listeners
134
performCallbacks(object, object.getClass());
135     }
136
137     /**
138      * Invokes callbacks for a collection of entity objects.
139      */

140     void performCallbacks(Collection JavaDoc objects) {
141         Iterator JavaDoc it = objects.iterator();
142         while (it.hasNext()) {
143             Object JavaDoc object = it.next();
144             performCallbacks(object);
145         }
146     }
147
148     /**
149      * Invokes callbacks for the class hierarchy, starting from the most generic
150      * superclass.
151      */

152     private void performCallbacks(Object JavaDoc object, Class JavaDoc callbackEntityClass) {
153         if (Object JavaDoc.class.equals(callbackEntityClass) || callbackEntityClass == null) {
154             return;
155         }
156
157         // recursively perform super callbacks first
158
if (!excludingSuperclassListeners(callbackEntityClass)) {
159             performCallbacks(object, callbackEntityClass.getSuperclass());
160         }
161
162         // perform callbacks on provided class
163
String JavaDoc key = callbackEntityClass.getName();
164         Collection JavaDoc entityListeners = (Collection JavaDoc) listeners.get(key);
165
166         if (entityListeners != null) {
167             Iterator JavaDoc it = (Iterator JavaDoc) entityListeners.iterator();
168             while (it.hasNext()) {
169                 ((AbstractCallback) it.next()).performCallback(object);
170             }
171         }
172     }
173
174 }
175
Popular Tags