KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > interceptor > LifecycleInterceptorHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb3.interceptor;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26
27 import javax.interceptor.InvocationContext;
28 import javax.ejb.PostActivate JavaDoc;
29 import javax.annotation.PostConstruct;
30 import javax.annotation.PreDestroy;
31 import javax.ejb.PrePassivate JavaDoc;
32 import javax.ejb.Timeout JavaDoc;
33 import javax.ejb.Timer JavaDoc;
34
35 import org.jboss.ejb3.BeanContext;
36 import org.jboss.ejb3.EJBContainer;
37 import org.jboss.ejb3.stateful.StatefulBeanContext;
38 import org.jboss.util.MethodHashing;
39 import org.jboss.logging.Logger;
40
41 /**
42  *
43  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
44  * @version $Revision: 55144 $
45  */

46 public class LifecycleInterceptorHandler
47 {
48    private static final Logger log = Logger.getLogger(LifecycleInterceptorHandler.class);
49
50    private EJBContainer container;
51    private InterceptorInfo[] postConstructs;
52    private InterceptorInfo[] postActivates;
53    private InterceptorInfo[] prePassivates;
54    private InterceptorInfo[] preDestroys;
55    private Method JavaDoc[] beanPostConstructs;
56    private Method JavaDoc[] beanPostActivates;
57    private Method JavaDoc[] beanPrePassivates;
58    private Method JavaDoc[] beanPreDestroys;
59    private Method JavaDoc timeoutCallbackMethod;
60    private long timeoutCalllbackHash;
61
62    public LifecycleInterceptorHandler(EJBContainer container, Class JavaDoc[] handledCallbacks)
63    {
64       this.container = container;
65       InterceptorInfoRepository repostitory = container.getInterceptorRepository();
66       for (Class JavaDoc clazz : handledCallbacks)
67       {
68          if (clazz == PostConstruct.class)
69          {
70             postConstructs = repostitory.getPostConstructInterceptors(container);
71             beanPostConstructs = repostitory.getBeanClassPostConstructs(container);
72          }
73          else if (clazz == PostActivate JavaDoc.class)
74          {
75             postActivates = repostitory.getPostActivateInterceptors(container);
76             beanPostActivates = repostitory.getBeanClassPostActivates(container);
77          }
78          else if (clazz == PrePassivate JavaDoc.class)
79          {
80             prePassivates = repostitory.getPrePassivateInterceptors(container);
81             beanPrePassivates = repostitory.getBeanClassPrePassivates(container);
82          }
83          else if (clazz == PreDestroy.class)
84          {
85             preDestroys = repostitory.getPreDestroyInterceptors(container);
86             beanPreDestroys = repostitory.getBeanClassPreDestroys(container);
87          }
88          else if (clazz == Timeout JavaDoc.class)
89          {
90             resolveTimeoutCallback();
91          }
92       }
93    }
94
95    public long getTimeoutCalllbackHash()
96    {
97       return timeoutCalllbackHash;
98    }
99
100    public void postConstruct(BeanContext ctx)
101    {
102       try
103       {
104          InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext(
105                PostConstruct.class,
106                ctx,
107                postConstructs,
108                beanPostConstructs);
109          ic.proceed();
110       }
111       catch (Exception JavaDoc e)
112       {
113          throw new RuntimeException JavaDoc(e);
114       }
115    }
116
117    public void preDestroy(BeanContext ctx)
118    {
119       Object JavaDoc id = null;
120       if (ctx instanceof StatefulBeanContext)
121       {
122          id = ((StatefulBeanContext)ctx).getId();
123       }
124       try
125       {
126          InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext(
127                PreDestroy.class,
128                ctx,
129                preDestroys,
130                beanPreDestroys);
131          ic.proceed();
132       }
133       catch (Exception JavaDoc e)
134       {
135          throw new RuntimeException JavaDoc(e);
136       }
137    }
138
139    public void postActivate(BeanContext ctx)
140    {
141       try
142       {
143          InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext(
144                PostActivate JavaDoc.class,
145                ctx,
146                postActivates,
147                beanPostActivates);
148          ic.proceed();
149       }
150       catch (Exception JavaDoc e)
151       {
152          throw new RuntimeException JavaDoc(e);
153       }
154    }
155
156    public void prePassivate(BeanContext ctx)
157    {
158       try
159       {
160          InvocationContext ic = LifecycleInvocationContextImpl.getLifecycleInvocationContext(
161                PrePassivate JavaDoc.class,
162                ctx,
163                prePassivates,
164                beanPrePassivates);
165          ic.proceed();
166       }
167       catch (Exception JavaDoc e)
168       {
169          throw new RuntimeException JavaDoc(e);
170       }
171    }
172
173    public Method JavaDoc getTimeoutCallback()
174    {
175       return timeoutCallbackMethod;
176    }
177
178
179    private void resolveTimeoutCallback()
180    {
181       for (Method JavaDoc method : container.getBeanClass().getMethods())
182       {
183          if (container.resolveAnnotation(method, Timeout JavaDoc.class) != null)
184          {
185             if (Modifier.isPublic(method.getModifiers()) &&
186                   method.getReturnType().equals(Void.TYPE) &&
187                   method.getParameterTypes().length == 1 &&
188                   method.getParameterTypes()[0].equals(Timer JavaDoc.class))
189             {
190                timeoutCallbackMethod = method;
191             }
192             else
193             {
194                throw new RuntimeException JavaDoc("@Timeout methods must have the signature: public void <METHOD>(javax.ejb.Timer timer) - " + method);
195             }
196          }
197       }
198
199       try
200       {
201          if (timeoutCallbackMethod == null && javax.ejb.TimedObject JavaDoc.class.isAssignableFrom(container.getBeanClass()))
202          {
203             Class JavaDoc[] params = new Class JavaDoc[]{Timer JavaDoc.class};
204             timeoutCallbackMethod = container.getBeanClass().getMethod("ejbTimeout", params);
205          }
206       }
207       catch (Exception JavaDoc e)
208       {
209          e.printStackTrace();
210       }
211
212       if (timeoutCallbackMethod != null)
213       {
214          timeoutCalllbackHash = MethodHashing.calculateHash(timeoutCallbackMethod);
215       }
216    }
217
218 }
219
Popular Tags