KickJava   Java API By Example, From Geeks To Geeks.

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


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.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import javax.interceptor.InvocationContext;
29 import javax.ejb.PostActivate JavaDoc;
30 import javax.annotation.PostConstruct;
31 import javax.annotation.PreDestroy;
32 import javax.ejb.PrePassivate JavaDoc;
33
34 import org.jboss.ejb3.BeanContext;
35
36 /**
37  *
38  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
39  * @version $Revision: 45473 $
40  */

41 public abstract class LifecycleInvocationContextImpl implements InvocationContext
42 {
43    
44    private int currentInterceptor;
45    private int currentMethod;
46    BeanContext beanContext;
47    InterceptorInfo[] interceptorInfos;
48    Object JavaDoc[] instances;
49    private Method JavaDoc[] beanMethods;
50    private HashMap JavaDoc metadata;
51
52    protected LifecycleInvocationContextImpl()
53    {
54    }
55    
56    public static InvocationContext getLifecycleInvocationContext(Class JavaDoc type, BeanContext beanContext, InterceptorInfo[] interceptorInfos, Method JavaDoc[] beanMethods)
57    {
58       LifecycleInvocationContextImpl ic = null;
59       if (type == PostConstruct.class) ic = new PostConstructICtxImpl();
60       else if (type == PostActivate JavaDoc.class) ic = new PostActivateICtxImpl();
61       else if (type == PrePassivate JavaDoc.class) ic = new PrePassivateICtxImpl();
62       else if (type == PreDestroy.class) ic = new PreDestroyICtxImpl();
63       else throw new RuntimeException JavaDoc("Unsupported lifecycle event: " + type);
64       
65       ic.instances = beanContext.getInterceptorInstances(interceptorInfos);
66       if (interceptorInfos.length != ic.instances.length)
67       {
68          throw new RuntimeException JavaDoc("interceptorInfos and instances have different length");
69       }
70       
71       ic.beanContext = beanContext;
72       ic.beanMethods = beanMethods;
73       ic.interceptorInfos = interceptorInfos;
74       
75       return ic;
76    }
77
78    public Object JavaDoc getTarget()
79    {
80       return beanContext.getInstance();
81    }
82
83    public Method JavaDoc getMethod()
84    {
85       return null;
86    }
87
88    public Object JavaDoc[] getParameters()
89    {
90       return new Object JavaDoc[0];
91    }
92
93    public void setParameters(Object JavaDoc[] params)
94    {
95       //Do nothing
96
}
97
98
99    public java.util.Map JavaDoc getContextData()
100    {
101       if (metadata == null) {
102          metadata = new HashMap JavaDoc();
103       }
104       return metadata;
105    }
106
107    public Object JavaDoc proceed() throws Exception JavaDoc
108    {
109       if (currentInterceptor < interceptorInfos.length)
110       {
111          int oldInterceptor = currentInterceptor;
112          int oldMethod = currentMethod;
113          try
114          {
115             int curr = currentInterceptor;
116             int currMethod = currentMethod++;
117             InterceptorInfo info = interceptorInfos[curr];
118             if (currMethod == getLifecycleMethods(info).length)
119             {
120                curr = ++currentInterceptor;
121                currentMethod = 0;
122                currMethod = currentMethod++;
123                info = (curr < interceptorInfos.length) ? interceptorInfos[curr] : null;
124             }
125             
126             if (info != null)
127             {
128                try
129                {
130                   Method JavaDoc[] methods = getLifecycleMethods(info);
131                   return methods[currMethod].invoke(instances[curr], this);
132                }
133                catch (InvocationTargetException JavaDoc e)
134                {
135                   if (e.getTargetException() instanceof Exception JavaDoc)
136                   {
137                      throw ((Exception JavaDoc) e.getCause());
138                   }
139                   else
140                   {
141                      throw new RuntimeException JavaDoc(e.getCause());
142                   }
143                }
144             }
145          }
146          finally
147          {
148             // so that interceptors like clustering can reinvoke down the chain
149
currentInterceptor = oldInterceptor;
150             currentMethod = oldMethod;
151          }
152       }
153       if (beanMethods != null)
154       {
155          try
156          {
157             for (Method JavaDoc beanMethod : beanMethods)
158             {
159                beanMethod.invoke(getTarget(), new Object JavaDoc[0]);
160             }
161          }
162          catch (InvocationTargetException JavaDoc e)
163          {
164             if (e.getTargetException() instanceof Exception JavaDoc)
165             {
166                throw ((Exception JavaDoc) e.getCause());
167             }
168             else
169             {
170                throw new RuntimeException JavaDoc(e.getCause());
171             }
172          }
173          finally
174          {
175          }
176       }
177       
178       return null;
179    }
180    
181    abstract Method JavaDoc[] getLifecycleMethods(InterceptorInfo info);
182    
183    public static class PostConstructICtxImpl extends LifecycleInvocationContextImpl
184    {
185       Method JavaDoc[] getLifecycleMethods(InterceptorInfo info)
186       {
187          return info.getPostConstructs();
188       }
189    }
190
191    public static class PostActivateICtxImpl extends LifecycleInvocationContextImpl
192    {
193       Method JavaDoc[] getLifecycleMethods(InterceptorInfo info)
194       {
195          return info.getPostActivates();
196       }
197    }
198
199    public static class PrePassivateICtxImpl extends LifecycleInvocationContextImpl
200    {
201       Method JavaDoc[] getLifecycleMethods(InterceptorInfo info)
202       {
203          return info.getPrePassivates();
204       }
205    }
206
207    public static class PreDestroyICtxImpl extends LifecycleInvocationContextImpl
208    {
209       Method JavaDoc[] getLifecycleMethods(InterceptorInfo info)
210       {
211          return info.getPreDestroys();
212       }
213    }
214 }
215
Popular Tags