KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > joinpoint > InvocationBase


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.aop.joinpoint;
23
24 import org.jboss.aop.Advisor;
25 import org.jboss.aop.InstanceAdvised;
26 import org.jboss.aop.InstanceAdvisor;
27 import org.jboss.aop.advice.Interceptor;
28 import org.jboss.aop.metadata.MetaDataResolver;
29 import org.jboss.aop.metadata.SimpleMetaData;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * This is a generic object that is used in intercepted invocations
36  * on field access, constructor, and methods
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 56100 $
40  */

41 abstract public class InvocationBase implements java.io.Serializable JavaDoc, Invocation
42 {
43    static final long serialVersionUID = -4880246394729260729L;
44
45    protected org.jboss.aop.metadata.SimpleMetaData metadata = null;
46
47
48    protected transient int currentInterceptor = 0;
49    protected transient org.jboss.aop.advice.Interceptor[] interceptors = null;
50    protected transient Advisor advisor = null;
51    protected transient Map JavaDoc responseContextInfo = null;
52
53    protected transient Object JavaDoc targetObject = null;
54
55    // todo need to refactor this as ClassProxyTemplate still needs this for remoting
56
protected transient MetaDataResolver instanceResolver;
57    
58    public Map JavaDoc getResponseContextInfo()
59    {
60       return responseContextInfo;
61    }
62
63    public void setResponseContextInfo(Map JavaDoc responseContextInfo)
64    {
65       this.responseContextInfo = responseContextInfo;
66    }
67
68    public void addResponseAttachment(Object JavaDoc key, Object JavaDoc val)
69    {
70       if (responseContextInfo == null) responseContextInfo = new HashMap JavaDoc(1);
71       responseContextInfo.put(key, val);
72    }
73
74    public Object JavaDoc getResponseAttachment(Object JavaDoc key)
75    {
76       if (responseContextInfo == null) return null;
77       return responseContextInfo.get(key);
78    }
79
80    /**
81     * Return all the contextual data attached to this invocation
82     */

83    public SimpleMetaData getMetaData()
84    {
85       if (metadata == null) metadata = new SimpleMetaData();
86       return metadata;
87    }
88
89    /**
90     * Set all the contextual data attached to this invocation
91     */

92    public void setMetaData(SimpleMetaData data)
93    {
94       this.metadata = data;
95    }
96
97    public Object JavaDoc resolveClassMetaData(Object JavaDoc key, Object JavaDoc attr)
98    {
99       return getAdvisor().getClassMetaData().getMetaData(key, attr);
100    }
101    
102    public int getCurrentInterceptor()
103    {
104       return currentInterceptor;
105    }
106
107    /**
108     * Invoke on the next interceptor in the chain. If this is already
109     * the end of the chain, reflection will call the constructor, field, or
110     * method you are invoking on.
111     */

112    public Object JavaDoc invokeNext() throws Throwable JavaDoc
113    {
114       try
115       {
116          return interceptors[currentInterceptor++].invoke(this);
117       }
118       finally
119       {
120          // so that interceptors like clustering can reinvoke down the chain
121
currentInterceptor--;
122       }
123    }
124
125    /**
126     * Invokes the target joinpoint for this invocation skipping any subsequent
127     * interceptors in the chain.
128     */

129    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
130    {
131       return null;
132    }
133
134    /**
135     * Invoke on the next interceptor in the chain. If this is already
136     * the end of the chain, reflection will call the constructor, field, or
137     * method you are invoking on.
138     * <p/>
139     * The Invocation will use a new set of interceptors to do the invocation
140     */

141    public Object JavaDoc invokeNext(Interceptor[] newInterceptors) throws Throwable JavaDoc
142    {
143       // Save the old stack position
144
org.jboss.aop.advice.Interceptor[] oldInterceptors = interceptors;
145       int oldCurrentInterceptor = currentInterceptor;
146
147       // Start the new stack
148
interceptors = newInterceptors;
149       currentInterceptor = 0;
150
151       // Invoke the new stack
152
try
153       {
154          return invokeNext();
155       }
156       finally
157       {
158          // Restore the old stack
159
interceptors = oldInterceptors;
160          currentInterceptor = oldCurrentInterceptor;
161       }
162    }
163
164    public InvocationBase(org.jboss.aop.advice.Interceptor[] interceptors)
165    {
166       this(interceptors, null);
167    }
168
169    public InvocationBase(org.jboss.aop.advice.Interceptor[] interceptors, org.jboss.aop.metadata.SimpleMetaData meta)
170    {
171       // We expect a copy of the interceptor chain so that it can't change
172
// in the middle of an invocation. This is so that
173
// we can redeploy interceptor chains, yet not effect
174
// currently running invocations.
175
this.interceptors = interceptors;
176       this.metadata = meta;
177    }
178
179    /**
180     * Copy constructor.
181     */

182    public InvocationBase(Invocation invocation)
183    {
184       this.interceptors = invocation.getInterceptors();
185       setTargetObject(invocation.getTargetObject());
186    }
187
188    public InvocationBase()
189    {
190       // for externalization
191
}
192
193    /**
194     * This used to be final, but I had to get rid of that since I need to
195     * lazily initialise the interceptors from the generated joinpoint/invocation classes
196     */

197    public org.jboss.aop.advice.Interceptor[] getInterceptors()
198    {
199       return interceptors;
200    }
201
202
203    public Object JavaDoc resolveClassAnnotation(Class JavaDoc annotation)
204    {
205       if (advisor != null) return advisor.resolveAnnotation(annotation);
206       return null;
207    }
208
209    public Object JavaDoc resolveAnnotation(Class JavaDoc annotation)
210    {
211       // todo need to add hooks for invocation and thread metadata.
212
return null;
213    }
214    
215    public Object JavaDoc resolveAnnotation(Class JavaDoc[] annotations)
216    {
217       // todo need to add hooks for invocation and thread metadata.
218
return null;
219    }
220
221    /**
222     * This method resolves metadata based on the context of the invocation.
223     * It iterates through its list of MetaDataResolvers to find out the
224     * value of the metadata desired.
225     * <p/>
226     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
227     * ClassAdvisor.getMethodMetaData (or field, or constructor)
228     * ClassAdvisor.getDefaultMetaData
229     */

230    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
231    {
232       Object JavaDoc val = null;
233       if (this.metadata != null)
234       {
235          val = this.metadata.resolve(this, group, attr);
236          if (val != null) return val;
237       }
238
239       val = org.jboss.aop.metadata.ThreadMetaData.instance().resolve(this, group, attr);
240       if (val != null) return val;
241
242       MetaDataResolver resolver = getInstanceResolver();
243       if (resolver != null)
244       {
245          val = getInstanceResolver().resolve(this, group, attr);
246          if (val != null) return val;
247       }
248
249       return null;
250    }
251
252    public MetaDataResolver getInstanceResolver()
253    {
254       if (instanceResolver != null) return instanceResolver;
255       if (getTargetObject() != null)
256       {
257          if (getTargetObject() instanceof InstanceAdvised)
258          {
259             InstanceAdvisor ia = ((InstanceAdvised) getTargetObject())._getInstanceAdvisor();
260             if (ia != null)
261             {
262
263                instanceResolver = ia.getMetaData();
264                return instanceResolver;
265             }
266          }
267       }
268       return null;
269    }
270
271    public Advisor getAdvisor()
272    {
273       return advisor;
274    }
275
276    public Object JavaDoc getTargetObject()
277    {
278       return targetObject;
279    }
280
281    public void setTargetObject(Object JavaDoc targetObject)
282    {
283       this.targetObject = targetObject;
284    }
285
286    public void setAdvisor(Advisor advisor)
287    {
288       this.advisor = advisor;
289    }
290
291    public void setInstanceResolver(MetaDataResolver instanceResolver)
292    {
293       this.instanceResolver = instanceResolver;
294    }
295
296 }
297
Popular Tags