KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodByMethodInfo;
26 import org.jboss.aop.advice.Interceptor;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31
32 /**
33  * This is a helper wrapper class for an Invocation object.
34  * It is used to add or get values or metadata that pertains to
35  * an AOP method invocation.
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 46058 $
39  */

40 public class MethodCalledByMethodInvocation extends CallerInvocation
41 {
42    private static final long serialVersionUID = -156920151151728318L;
43    
44    //info fields
45
Class JavaDoc callingClass;
46    Method JavaDoc callingMethod;
47    Method JavaDoc method;
48    
49    protected Object JavaDoc[] arguments = null;
50    protected Object JavaDoc callingObject;
51
52    public MethodCalledByMethodInvocation(MethodByMethodInfo info, Object JavaDoc callingObject, Object JavaDoc targetObject, Object JavaDoc[] arguments, Interceptor[] interceptors)
53    {
54       this(info.getAdvisor(), info.getCallingClass(), info.getCallingMethod(), info.getMethod(), callingObject, targetObject, arguments, interceptors);
55    }
56
57    public MethodCalledByMethodInvocation(MethodByMethodInfo info, Object JavaDoc callingObject, Object JavaDoc targetObject, Interceptor[] interceptors)
58    {
59       this(info.getAdvisor(), info.getCallingClass(), info.getCallingMethod(), info.getMethod(), callingObject, targetObject, null, interceptors);
60    }
61
62    public MethodCalledByMethodInvocation(Advisor advisor, Class JavaDoc callingClass,
63          Method JavaDoc callingMethod, Method JavaDoc method, Object JavaDoc callingObject, Object JavaDoc targetObject, Object JavaDoc[] args, Interceptor[] interceptors)
64    {
65       super(advisor, interceptors);
66       this.callingClass = callingClass;
67       this.callingMethod = callingMethod;
68       this.method = method;
69       this.callingObject = callingObject;
70       setTargetObject(targetObject);
71       this.arguments = args;
72    }
73    
74    public MethodCalledByMethodInvocation(Interceptor[] interceptors)
75    {
76       super(interceptors);
77    }
78
79    /**
80     * Invoke on the next interceptor in the chain. If this is already
81     * the end of the chain, reflection will call the constructor, field, or
82     * method you are invoking on.
83     */

84    public Object JavaDoc invokeNext() throws Throwable JavaDoc
85    {
86       if (interceptors != null && currentInterceptor < interceptors.length)
87       {
88          try
89          {
90             return interceptors[currentInterceptor++].invoke(this);
91          }
92          finally
93          {
94             // so that interceptors like clustering can reinvoke down the chain
95
currentInterceptor--;
96          }
97       }
98
99       return invokeTarget();
100    }
101
102    /**
103     * Invokes the target joinpoint for this invocation skipping any subsequent
104     * interceptors in the chain.
105     */

106    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
107    {
108       try
109       {
110          return method.invoke(getTargetObject(), arguments);
111       }
112       catch (InvocationTargetException JavaDoc e)
113       {
114          throw e.getTargetException();
115       }
116    }
117
118    
119    /**
120     * This method resolves metadata based on the context of the invocation.
121     * It iterates through its list of MetaDataResolvers to find out the
122     * value of the metadata desired.
123     * <p/>
124     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
125     * ClassAdvisor.getMethodMetaData (or field, or constructor)
126     * ClassAdvisor.getDefaultMetaData
127     */

128    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
129    {
130       Object JavaDoc val = super.getMetaData(group, attr);
131       if (val != null) return val;
132
133       // todo what to do for caller side metadata?
134
return null;
135    }
136
137    /**
138     * Get a wrapper invocation object that can insert a new chain of interceptors
139     * at runtime to the invocation flow. CFlow makes use of this.
140     * When the wrapper object finishes its invocation chain it delegates back to
141     * the wrapped invocation.
142     *
143     * @param newchain
144     * @return
145     */

146    public Invocation getWrapper(Interceptor[] newchain)
147    {
148       MethodCalledByMethodInvocationWrapper wrapper = new MethodCalledByMethodInvocationWrapper(this, newchain);
149       return wrapper;
150    }
151
152    /**
153     * Copies complete state of Invocation object.
154     *
155     * @return
156     */

157    public Invocation copy()
158    {
159       MethodCalledByMethodInvocation wrapper = new MethodCalledByMethodInvocation(advisor, callingClass,
160             callingMethod, method, callingObject, targetObject, arguments, interceptors);
161       wrapper.currentInterceptor = this.currentInterceptor;
162       wrapper.instanceResolver = this.instanceResolver;
163       wrapper.metadata = this.metadata;
164       return wrapper;
165    }
166
167    /**
168     * @return the arguments of the called method
169     */

170    public Object JavaDoc[] getArguments()
171    {
172       return arguments;
173    }
174
175    /**
176     * change the arguments to the called method
177     *
178     * @param arguments
179     */

180    public void setArguments(Object JavaDoc[] arguments)
181    {
182       this.arguments = arguments;
183    }
184
185    /**
186     * @return The class that is making the call on the method
187     */

188    public Class JavaDoc getCallingClass()
189    {
190       return callingClass;
191    }
192
193    /**
194     * @return The method that is making the call on the method
195     */

196    public Method JavaDoc getCallingMethod()
197    {
198       return callingMethod;
199    }
200
201    public Method JavaDoc getCalledMethod()
202    {
203       return method;
204    }
205
206    public Object JavaDoc getCallingObject()
207    {
208       return callingObject;
209    }
210
211 }
212
Popular Tags