1 7 package org.jboss.ejb3.callback; 8 9 import java.util.HashMap ; 10 import javax.ejb.CallbackListener; 11 import javax.ejb.PostActivate ; 12 import javax.ejb.PostConstruct; 13 import javax.ejb.PreDestroy; 14 import javax.ejb.PrePassivate ; 15 import javax.ejb.Timeout ; 16 import org.jboss.aop.Advisor; 17 import org.jboss.logging.Logger; 18 19 23 public class SessionCallbackHandler 24 { 25 private static final Logger log = Logger.getLogger(SessionCallbackHandler.class); 26 27 private Advisor beanAdvisor; 28 private Class beanClass; 29 private Class listener; 30 31 HashMap <Class , Callback> callbacks = new HashMap <Class , Callback>(); 32 33 public SessionCallbackHandler(Advisor beanAdvisor, Class [] validCallbacks)throws Exception 34 { 35 this.beanAdvisor = beanAdvisor; 36 beanClass = beanAdvisor.getClazz(); 37 38 CallbackListener cl = (CallbackListener)beanAdvisor.resolveAnnotation(CallbackListener.class); 39 if (cl != null) 40 { 41 listener = cl.value(); 42 } 43 44 for (int i = 0 ; i < validCallbacks.length ; i++) 45 { 46 Callback callback = resolveCallback(beanAdvisor, validCallbacks[i]); 47 if (callback != null) 48 { 49 callbacks.put(validCallbacks[i], callback); 50 } else 51 { 52 Object o = beanAdvisor.resolveAnnotation(validCallbacks[i]); 53 } 54 } 55 } 56 57 public void postConstruct(Object bean) 58 { 59 callback(PostConstruct.class, bean); 60 } 61 62 public void preDestroy(Object bean) 63 { 64 callback(PreDestroy.class, bean); 65 } 66 67 public void postActivate(Object bean) 68 { 69 callback(PostActivate .class, bean); 70 } 71 72 public void prePassivate(Object bean) 73 { 74 callback(PrePassivate .class, bean); 75 } 76 77 public Callback getTimeoutCallback() 78 { 79 return getCallback(Timeout .class); 80 } 81 82 public Callback getCallback(Class callback) 83 { 84 return callbacks.get(callback); 85 } 86 87 private void callback(Class callback, Object bean) 88 { 89 try 90 { 91 Callback cb = callbacks.get(callback); 92 if (cb != null) 93 { 94 cb.invoke(bean); 95 } 96 } 97 catch(Exception e) 98 { 99 throw new RuntimeException (e); 100 } 101 } 102 103 private Callback resolveCallback(Advisor beanAdvisor, Class annotation) throws Exception 104 { 105 Class beanClass = beanAdvisor.getClazz(); 106 return CallbackResolver.resolveCallback(beanAdvisor, beanClass, annotation, listener); 107 } 108 } 109 | Popular Tags |