KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Map JavaDoc;
28
29 import javax.interceptor.InvocationContext;
30
31 import org.jboss.ejb3.EJBContainerInvocation;
32
33 /**
34  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
35  * @version $Revision: 45473 $
36  */

37 public class InvocationContextImpl implements InvocationContext
38 {
39    private int currentInterceptor;
40    private int currentMethod;
41    private int currentBeanMethod;
42    InterceptorInfo[] interceptorInfos;
43    Object JavaDoc[] instances;
44    private Method JavaDoc[] beanAroundInvokes;
45    private Map JavaDoc metadata;
46
47    EJBContainerInvocation wrapped;
48    
49    public InvocationContextImpl(EJBContainerInvocation inv, InterceptorInfo[] interceptorInfos, Object JavaDoc[] instances, Method JavaDoc[] beanAroundInvokes)
50    {
51       wrapped = inv;
52       this.beanAroundInvokes = beanAroundInvokes;
53       
54       if (interceptorInfos.length != instances.length)
55       {
56          throw new RuntimeException JavaDoc("interceptorInfos and instances have different length");
57       }
58       
59       this.interceptorInfos = interceptorInfos;
60       this.instances = instances;
61    }
62
63    public Object JavaDoc getTarget()
64    {
65       return wrapped.getTargetObject();
66    }
67
68    public Method JavaDoc getMethod()
69    {
70       return wrapped.getMethod();
71    }
72
73    public Object JavaDoc[] getParameters()
74    {
75       return wrapped.getArguments();
76    }
77
78    public void setParameters(Object JavaDoc[] params)
79    {
80       wrapped.setArguments(params);
81    }
82
83    public java.util.Map JavaDoc getContextData()
84    {
85       if (metadata == null) {
86          metadata = ClientInterceptorUtil.getClientMetadataMap(wrapped);
87          if (metadata == null)
88          {
89             metadata = new HashMap JavaDoc();
90          }
91       }
92       return metadata;
93    }
94
95    public Object JavaDoc proceed() throws Exception JavaDoc
96    {
97       if (currentInterceptor < interceptorInfos.length)
98       {
99          int oldInterceptor = currentInterceptor;
100          int oldMethod = currentMethod;
101          try
102          {
103             int curr = currentInterceptor;
104             int currMethod = currentMethod++;
105             
106             InterceptorInfo info = interceptorInfos[curr];
107             if (currMethod == info.getAroundInvokes().length)
108             {
109                curr = ++currentInterceptor;
110                currentMethod = 0;
111                currMethod = currentMethod++;
112                info = (curr < interceptorInfos.length) ? interceptorInfos[curr] : null;
113             }
114             if (info != null)
115             {
116                try
117                {
118                   return info.getAroundInvokes()[currMethod].invoke(instances[curr], this);
119                }
120                catch (InvocationTargetException JavaDoc e)
121                {
122                   if (e.getTargetException() instanceof Exception JavaDoc)
123                   {
124                      throw ((Exception JavaDoc) e.getCause());
125                   }
126                   else
127                   {
128                      throw new RuntimeException JavaDoc(e.getCause());
129                   }
130                }
131             }
132          }
133          finally
134          {
135             // so that interceptors like clustering can reinvoke down the chain
136
currentInterceptor = oldInterceptor;
137             currentMethod = oldMethod;
138          }
139       }
140       
141       if (beanAroundInvokes != null && currentBeanMethod < beanAroundInvokes.length)
142       {
143          try
144          {
145             int curr = currentBeanMethod++;
146             return beanAroundInvokes[curr].invoke(getTarget(), this);
147          }
148          catch (InvocationTargetException JavaDoc e)
149          {
150             if (e.getTargetException() instanceof Exception JavaDoc)
151             {
152                throw ((Exception JavaDoc) e.getCause());
153             }
154             else
155             {
156                throw new RuntimeException JavaDoc(e.getCause());
157             }
158          }
159          finally
160          {
161             currentBeanMethod--;;
162          }
163       }
164       try
165       {
166          return wrapped.invokeNext();
167       }
168       catch (Exception JavaDoc e)
169       {
170          throw e;
171       }
172       catch (Throwable JavaDoc t)
173       {
174          throw new RuntimeException JavaDoc(t);
175       }
176    }
177 }
178
Popular Tags