KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > callback > EntityCallbackHandler


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.hibernate.ejb.callback;
8
9 import java.util.HashMap JavaDoc;
10 import javax.persistence.EntityListener;
11 import javax.persistence.PostLoad;
12 import javax.persistence.PostPersist;
13 import javax.persistence.PostRemove;
14 import javax.persistence.PostUpdate;
15 import javax.persistence.PrePersist;
16 import javax.persistence.PreRemove;
17 import javax.persistence.PreUpdate;
18 import javax.persistence.PersistenceException;
19
20 /**
21  * Keep track of all lifecycle callbacks and listeners for a given persistence unit
22  *
23  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
24  */

25 public class EntityCallbackHandler {
26     private HashMap JavaDoc<Class JavaDoc, Callback> preCreates = new HashMap JavaDoc<Class JavaDoc, Callback>();
27     private HashMap JavaDoc<Class JavaDoc, Callback> postCreates = new HashMap JavaDoc<Class JavaDoc, Callback>();
28     private HashMap JavaDoc<Class JavaDoc, Callback> preRemoves = new HashMap JavaDoc<Class JavaDoc, Callback>();
29     private HashMap JavaDoc<Class JavaDoc, Callback> postRemoves = new HashMap JavaDoc<Class JavaDoc, Callback>();
30     private HashMap JavaDoc<Class JavaDoc, Callback> preUpdates = new HashMap JavaDoc<Class JavaDoc, Callback>();
31     private HashMap JavaDoc<Class JavaDoc, Callback> postUpdates = new HashMap JavaDoc<Class JavaDoc, Callback>();
32     private HashMap JavaDoc<Class JavaDoc, Callback> postLoads = new HashMap JavaDoc<Class JavaDoc, Callback>();
33
34     public void add(Class JavaDoc entity) {
35         addCallback( entity, preCreates, PrePersist.class );
36         addCallback( entity, postCreates, PostPersist.class );
37         addCallback( entity, preRemoves, PreRemove.class );
38         addCallback( entity, postRemoves, PostRemove.class );
39         addCallback( entity, preUpdates, PreUpdate.class );
40         addCallback( entity, postUpdates, PostUpdate.class );
41         addCallback( entity, postLoads, PostLoad.class );
42     }
43
44     public boolean preCreate(Object JavaDoc bean) {
45         return callback( preCreates.get( bean.getClass() ), bean );
46     }
47
48     public boolean postCreate(Object JavaDoc bean) {
49         return callback( postCreates.get( bean.getClass() ), bean );
50     }
51
52     public boolean preRemove(Object JavaDoc bean) {
53         return callback( preRemoves.get( bean.getClass() ), bean );
54     }
55
56     public boolean postRemove(Object JavaDoc bean) {
57         return callback( postRemoves.get( bean.getClass() ), bean );
58     }
59
60     public boolean preUpdate(Object JavaDoc bean) {
61         return callback( preUpdates.get( bean.getClass() ), bean );
62     }
63
64     public boolean postUpdate(Object JavaDoc bean) {
65         return callback( postUpdates.get( bean.getClass() ), bean );
66     }
67
68     public boolean postLoad(Object JavaDoc bean) {
69         return callback( postLoads.get( bean.getClass() ), bean );
70     }
71
72
73     private boolean callback(Callback callback, Object JavaDoc bean) {
74         if ( callback != null ) {
75             callback.invoke( bean );
76             return true;
77         }
78         else {
79             return false;
80         }
81     }
82
83
84     private void addCallback(Class JavaDoc entity, HashMap JavaDoc<Class JavaDoc, Callback> map, Class JavaDoc annotation) {
85         Callback callback = null;
86         Class JavaDoc clazz = entity;
87         while ( callback == null && clazz != null ) {
88             callback = resolveCallback( clazz, annotation );
89             if ( callback != null ) {
90                 map.put( entity, callback );
91                 break;
92             }
93
94             clazz = clazz.getSuperclass();
95         }
96     }
97
98     private Callback resolveCallback(Class JavaDoc entity, Class JavaDoc annotation) {
99         Class JavaDoc listener = null;
100         EntityListener cl = (EntityListener) entity.getAnnotation( EntityListener.class );
101         try {
102             if ( cl != null ) {
103                 listener = cl.value();
104             }
105             return CallbackResolver.resolveCallback( entity, annotation, listener );
106         }
107         catch (ClassNotFoundException JavaDoc e) {
108             throw new PersistenceException(
109                     "Could not load CallbackListener class " + cl.value() + " for bean " + entity.getName()
110             );
111         }
112         catch (Exception JavaDoc e) {
113             throw new PersistenceException( e.getCause() );
114         }
115     }
116 }
117
Popular Tags