KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.jboss.ejb3.metamodel.Interceptor;
31
32 /**
33  * We cannot use annotation overrides for the interceptor stuff since they do not have a
34  * container associated with them
35  *
36  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
37  * @version $Revision: 45242 $
38  */

39 public class InterceptorInfo
40 {
41    Class JavaDoc clazz;
42    Interceptor xml;
43    
44    //interceptor methods defined by this class
45
protected Method JavaDoc aroundInvoke;
46    protected Method JavaDoc postConstruct;
47    protected Method JavaDoc postActivate;
48    protected Method JavaDoc preDestroy;
49    protected Method JavaDoc prePassivate;
50    
51    //Interceptor methods defined by this class and all superclasses
52
protected Method JavaDoc[] aroundInvokeHierarchy;
53    protected Method JavaDoc[] postConstructHierarchy;
54    protected Method JavaDoc[] postActivateHierarchy;
55    protected Method JavaDoc[] preDestroyHierarchy;
56    protected Method JavaDoc[] prePassivateHierarchy;
57    
58    boolean haveCalculatedHierarchy;
59  
60    protected InterceptorInfo()
61    {
62    }
63
64    public InterceptorInfo(Class JavaDoc clazz)
65    {
66       this.clazz = clazz;
67    }
68    
69    public InterceptorInfo(InterceptorInfo interceptorInfo)
70    {
71       this.clazz = interceptorInfo.clazz;
72       this.aroundInvoke = interceptorInfo.aroundInvoke;
73       this.postConstruct = interceptorInfo.postConstruct;
74       this.postActivate = interceptorInfo.postActivate;
75       this.preDestroy = interceptorInfo.preDestroy;
76       this.prePassivate = interceptorInfo.prePassivate;
77       this.aroundInvokeHierarchy = interceptorInfo.aroundInvokeHierarchy;
78       this.postConstructHierarchy = interceptorInfo.postConstructHierarchy;
79       this.postActivateHierarchy = interceptorInfo.postActivateHierarchy;
80       this.preDestroyHierarchy = interceptorInfo.preDestroyHierarchy;
81       this.prePassivateHierarchy = interceptorInfo.prePassivateHierarchy;
82    }
83
84    protected void setXml(Interceptor xml)
85    {
86       this.xml = xml;
87    }
88    
89    public Interceptor getXml()
90    {
91       return xml;
92    }
93    
94    public Method JavaDoc getAroundInvoke()
95    {
96       return aroundInvoke;
97    }
98
99    protected void setAroundInvoke(Method JavaDoc aroundInvoke)
100    {
101       if (aroundInvoke == null) return;
102       
103       if (this.aroundInvoke != null && !this.aroundInvoke.equals(aroundInvoke))
104       {
105          throw new RuntimeException JavaDoc("Interceptors can only have one around-invoke/@AroundInvoke method - " + clazz.getName());
106       }
107       this.aroundInvoke = makeAccessible(aroundInvoke);
108    }
109
110    public Class JavaDoc getClazz()
111    {
112       return clazz;
113    }
114
115    public boolean haveCalculatedHierarchy()
116    {
117       return haveCalculatedHierarchy;
118    }
119    
120    public Method JavaDoc getPostActivate()
121    {
122       return postActivate;
123    }
124
125    protected void setPostActivate(Method JavaDoc postActivate)
126    {
127       if (postActivate == null) return;
128       
129       if (this.postActivate != null && !this.postActivate.equals(postActivate))
130       {
131          throw new RuntimeException JavaDoc("Interceptors can only have one post-activate/@PostActivate method - " + clazz.getName());
132       }
133       this.postActivate = makeAccessible(postActivate);
134    }
135
136    public Method JavaDoc getPostConstruct()
137    {
138       return postConstruct;
139    }
140
141    protected void setPostConstruct(Method JavaDoc postConstruct)
142    {
143       if (postConstruct == null) return;
144       
145       if (this.postConstruct != null && !this.postConstruct.equals(postConstruct))
146       {
147          throw new RuntimeException JavaDoc("Interceptors can only have one post-construct/@PostConstruct method - " + clazz.getName());
148       }
149       this.postConstruct = makeAccessible(postConstruct);
150    }
151
152    public Method JavaDoc getPreDestroy()
153    {
154       return preDestroy;
155    }
156
157    protected void setPreDestroy(Method JavaDoc preDestroy)
158    {
159       if (preDestroy == null) return;
160       
161       if (this.preDestroy != null && !this.preDestroy.equals(preDestroy))
162       {
163          throw new RuntimeException JavaDoc("Interceptors can only have one pre-destroy/@PreDestroy method - " + clazz.getName());
164       }
165       this.preDestroy = makeAccessible(preDestroy);
166    }
167
168    public Method JavaDoc getPrePassivate()
169    {
170       return prePassivate;
171    }
172
173    protected void setPrePassivate(Method JavaDoc prePassivate)
174    {
175       if (prePassivate == null) return;
176       
177       if (this.prePassivate != null && !this.prePassivate.equals(prePassivate))
178       {
179          throw new RuntimeException JavaDoc("Interceptors can only have one pre-passivate/@PrePassivate method - " + clazz.getName());
180       }
181       this.prePassivate = makeAccessible(prePassivate);
182    }
183    
184    public Method JavaDoc[] getAroundInvokes()
185    {
186       return aroundInvokeHierarchy;
187    }
188
189    public Method JavaDoc[] getPostActivates()
190    {
191       return postActivateHierarchy;
192    }
193
194    public Method JavaDoc[] getPostConstructs()
195    {
196       return postConstructHierarchy;
197    }
198
199    public Method JavaDoc[] getPreDestroys()
200    {
201       return preDestroyHierarchy;
202    }
203
204    public Method JavaDoc[] getPrePassivates()
205    {
206       return prePassivateHierarchy;
207    }
208
209    private Method JavaDoc makeAccessible(final Method JavaDoc method)
210    {
211       try
212       {
213          AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
214             public Object JavaDoc run()
215             {
216                method.setAccessible(true);
217                return null;
218             }
219          });
220       }
221       catch (PrivilegedActionException JavaDoc e)
222       {
223          throw new RuntimeException JavaDoc(e.getException());
224       }
225       
226       return method;
227    }
228    
229    public String JavaDoc toString()
230    {
231       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("InterceptorInfo{class=" + clazz);
232       appendMethods(sb);
233       sb.append("}");
234       return sb.toString();
235    }
236
237    protected void appendMethods(StringBuffer JavaDoc sb)
238    {
239       appendMethodString(sb, "aroundInvoke", aroundInvoke);
240       appendMethodString(sb, "postConstruct", postConstruct);
241       appendMethodString(sb, "postActivate", postActivate);
242       appendMethodString(sb, "prePassivate", prePassivate);
243       appendMethodString(sb, "preDestroy", preDestroy);
244    }
245    
246    protected void appendMethodString(StringBuffer JavaDoc buf, String JavaDoc methodType, Method JavaDoc m)
247    {
248       if (m != null)
249       {
250          buf.append(", " + methodType + "=" + m.getName());
251       }
252    }
253    
254    public void calculateHierarchy(InterceptorInfo superInfo)
255    {
256       if (haveCalculatedHierarchy)
257       {
258          return;
259       }
260       
261       postConstructHierarchy = initaliseMethods((superInfo != null) ? superInfo.postConstructHierarchy : null, postConstruct);
262       postActivateHierarchy = initaliseMethods((superInfo != null) ? superInfo.postActivateHierarchy : null, postActivate);
263       aroundInvokeHierarchy = initaliseMethods((superInfo != null) ? superInfo.aroundInvokeHierarchy : null, aroundInvoke);
264       prePassivateHierarchy = initaliseMethods((superInfo != null) ? superInfo.prePassivateHierarchy : null, prePassivate);
265       preDestroyHierarchy = initaliseMethods((superInfo != null) ? superInfo.preDestroyHierarchy : null, preDestroy);
266       
267       haveCalculatedHierarchy = true;
268    }
269    
270    private Method JavaDoc[] initaliseMethods(Method JavaDoc[] superMethods, Method JavaDoc myMethod)
271    {
272       if (superMethods == null && myMethod == null)
273       {
274          return null;
275       }
276       ArrayList JavaDoc hierarchy = new ArrayList JavaDoc();
277       if (superMethods != null)
278       {
279          //We only want to add superclass interceptor/lifecycle methods if we do not override them
280
for (int i = 0 ; i < superMethods.length ; ++i)
281          {
282             if (!haveMethod(superMethods[i]))
283             {
284                hierarchy.add(superMethods[i]);
285             }
286          }
287       }
288       
289       if (myMethod != null)
290       {
291          hierarchy.add(myMethod);
292       }
293       
294       return (Method JavaDoc[])hierarchy.toArray(new Method JavaDoc[hierarchy.size()]);
295    }
296    
297    private boolean haveMethod(Method JavaDoc method)
298    {
299       try
300       {
301          clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
302          return true;
303       }
304       catch (NoSuchMethodException JavaDoc e)
305       {
306          return false;
307       }
308    }
309
310    @Override JavaDoc
311    public boolean equals(Object JavaDoc obj)
312    {
313       if (obj instanceof InterceptorInfo)
314       {
315          return clazz.equals(((InterceptorInfo)obj).getClazz());
316       }
317       return false;
318    }
319
320    @Override JavaDoc
321    public int hashCode()
322    {
323       return clazz.getName().hashCode();
324    }
325    
326    
327 }
328
Popular Tags