KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > callback > SessionCallbackHandler


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.jboss.ejb3.callback;
8
9 import java.util.HashMap JavaDoc;
10 import javax.ejb.CallbackListener;
11 import javax.ejb.PostActivate JavaDoc;
12 import javax.ejb.PostConstruct;
13 import javax.ejb.PreDestroy;
14 import javax.ejb.PrePassivate JavaDoc;
15 import javax.ejb.Timeout JavaDoc;
16 import org.jboss.aop.Advisor;
17 import org.jboss.logging.Logger;
18
19 /**
20  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
21  * @version $Revision: 1.6.2.1 $
22  */

23 public class SessionCallbackHandler
24 {
25    private static final Logger log = Logger.getLogger(SessionCallbackHandler.class);
26    
27    private Advisor beanAdvisor;
28    private Class JavaDoc beanClass;
29    private Class JavaDoc listener;
30
31    HashMap JavaDoc<Class JavaDoc, Callback> callbacks = new HashMap JavaDoc<Class JavaDoc, Callback>();
32
33    public SessionCallbackHandler(Advisor beanAdvisor, Class JavaDoc[] validCallbacks)throws Exception JavaDoc
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 JavaDoc o = beanAdvisor.resolveAnnotation(validCallbacks[i]);
53          }
54       }
55    }
56
57    public void postConstruct(Object JavaDoc bean)
58    {
59       callback(PostConstruct.class, bean);
60    }
61
62    public void preDestroy(Object JavaDoc bean)
63    {
64       callback(PreDestroy.class, bean);
65    }
66
67    public void postActivate(Object JavaDoc bean)
68    {
69       callback(PostActivate JavaDoc.class, bean);
70    }
71
72    public void prePassivate(Object JavaDoc bean)
73    {
74       callback(PrePassivate JavaDoc.class, bean);
75    }
76
77    public Callback getTimeoutCallback()
78    {
79       return getCallback(Timeout JavaDoc.class);
80    }
81
82    public Callback getCallback(Class JavaDoc callback)
83    {
84       return callbacks.get(callback);
85    }
86
87    private void callback(Class JavaDoc callback, Object JavaDoc 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 JavaDoc e)
98       {
99          throw new RuntimeException JavaDoc(e);
100       }
101    }
102
103    private Callback resolveCallback(Advisor beanAdvisor, Class JavaDoc annotation) throws Exception JavaDoc
104    {
105       Class JavaDoc beanClass = beanAdvisor.getClazz();
106       return CallbackResolver.resolveCallback(beanAdvisor, beanClass, annotation, listener);
107    }
108 }
109
Popular Tags