1 7 package org.hibernate.ejb.callback; 8 9 import java.util.HashMap ; 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 25 public class EntityCallbackHandler { 26 private HashMap <Class , Callback> preCreates = new HashMap <Class , Callback>(); 27 private HashMap <Class , Callback> postCreates = new HashMap <Class , Callback>(); 28 private HashMap <Class , Callback> preRemoves = new HashMap <Class , Callback>(); 29 private HashMap <Class , Callback> postRemoves = new HashMap <Class , Callback>(); 30 private HashMap <Class , Callback> preUpdates = new HashMap <Class , Callback>(); 31 private HashMap <Class , Callback> postUpdates = new HashMap <Class , Callback>(); 32 private HashMap <Class , Callback> postLoads = new HashMap <Class , Callback>(); 33 34 public void add(Class 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 bean) { 45 return callback( preCreates.get( bean.getClass() ), bean ); 46 } 47 48 public boolean postCreate(Object bean) { 49 return callback( postCreates.get( bean.getClass() ), bean ); 50 } 51 52 public boolean preRemove(Object bean) { 53 return callback( preRemoves.get( bean.getClass() ), bean ); 54 } 55 56 public boolean postRemove(Object bean) { 57 return callback( postRemoves.get( bean.getClass() ), bean ); 58 } 59 60 public boolean preUpdate(Object bean) { 61 return callback( preUpdates.get( bean.getClass() ), bean ); 62 } 63 64 public boolean postUpdate(Object bean) { 65 return callback( postUpdates.get( bean.getClass() ), bean ); 66 } 67 68 public boolean postLoad(Object bean) { 69 return callback( postLoads.get( bean.getClass() ), bean ); 70 } 71 72 73 private boolean callback(Callback callback, Object 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 entity, HashMap <Class , Callback> map, Class annotation) { 85 Callback callback = null; 86 Class 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 entity, Class annotation) { 99 Class 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 e) { 108 throw new PersistenceException( 109 "Could not load CallbackListener class " + cl.value() + " for bean " + entity.getName() 110 ); 111 } 112 catch (Exception e) { 113 throw new PersistenceException( e.getCause() ); 114 } 115 } 116 } 117 | Popular Tags |