KickJava   Java API By Example, From Geeks To Geeks.

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


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

38 public class ConstructorInvocation extends InvocationBase
39 {
40    private static final long serialVersionUID = -7880020293056198584L;
41
42    protected Object JavaDoc[] arguments = null; // MARSHALLED
43
protected transient Constructor JavaDoc constructor = null;
44
45    public ConstructorInvocation(Interceptor[] interceptors)
46    {
47       super(interceptors);
48    }
49
50    public ConstructorInvocation(ConstructorInfo info, Interceptor[] interceptors)
51    {
52       super(interceptors);
53       super.advisor = info.getAdvisor();
54       constructor = info.getConstructor();
55    }
56
57
58    /**
59     * Invoke on the next interceptor in the chain. If this is already
60     * the end of the chain, reflection will call the constructor, field, or
61     * method you are invoking on.
62     */

63    public Object JavaDoc invokeNext() throws Throwable JavaDoc
64    {
65       if (interceptors != null && currentInterceptor < interceptors.length)
66       {
67          try
68          {
69             return interceptors[currentInterceptor++].invoke(this);
70          }
71          finally
72          {
73             // so that interceptors like clustering can reinvoke down the chain
74
currentInterceptor--;
75          }
76       }
77
78       return invokeTarget();
79    }
80
81    /**
82     * Invokes the target joinpoint for this invocation skipping any subsequent
83     * interceptors in the chain.
84     */

85    public Object JavaDoc invokeTarget() throws Throwable JavaDoc
86    {
87       try
88       {
89          Constructor JavaDoc con = getConstructor();
90          Object JavaDoc[] args = getArguments();
91          setTargetObject(con.newInstance(args));
92          return getTargetObject();
93       }
94       catch (InstantiationException JavaDoc in)
95       {
96          throw new RuntimeException JavaDoc("failed to call constructor", in);
97       }
98       catch (IllegalAccessException JavaDoc ill)
99       {
100          throw new RuntimeException JavaDoc("illegal access", ill);
101       }
102       catch (InvocationTargetException JavaDoc ite)
103       {
104          throw ite.getCause();
105       }
106       catch (IllegalArgumentException JavaDoc iae)
107       {
108          //System.err.println(constructor.toString());
109
throw iae;
110       }
111    }
112
113
114    /**
115     * This method resolves an annotation based on the context of the invocation.
116     *
117     */

118    public Object JavaDoc resolveAnnotation(Class JavaDoc annotation)
119    {
120       Object JavaDoc val = super.resolveAnnotation(annotation);
121       if (val != null) return val;
122
123       if (getAdvisor() != null)
124       {
125          val = getAdvisor().resolveAnnotation(constructor, annotation);
126          if (val != null) return val;
127       }
128
129       return null;
130    }
131
132    /**
133     * This method resolves metadata based on the context of the invocation.
134     * It iterates through its list of MetaDataResolvers to find out the
135     * value of the metadata desired.
136     *
137     * This list usually is ThreadMetaData, InstanceAdvisor.getMetaData
138     * ClassAdvisor.getMethodMetaData (or field, or constructor)
139     * ClassAdvisor.getDefaultMetaData
140     */

141    public Object JavaDoc getMetaData(Object JavaDoc group, Object JavaDoc attr)
142    {
143       Object JavaDoc val = super.getMetaData(group, attr);
144       if (val != null) return val;
145
146       if (getAdvisor() != null)
147       {
148          val = getAdvisor().getConstructorMetaData().resolve(this, group, attr);
149          if (val != null) return val;
150       }
151
152       if (getAdvisor() != null)
153       {
154          val = getAdvisor().getDefaultMetaData().resolve(this, group, attr);
155          if (val != null) return val;
156       }
157
158       return null;
159    }
160
161    /**
162     * Get a wrapper invocation object that can insert a new chain of interceptors
163     * at runtime to the invocation flow. CFlow makes use of this.
164     * When the wrapper object finishes its invocation chain it delegates back to
165     * the wrapped invocation.
166     * @param newchain
167     * @return
168     */

169    public Invocation getWrapper(Interceptor[] newchain)
170    {
171       ConstructorInvocationWrapper wrapper = new ConstructorInvocationWrapper(this, newchain);
172       return wrapper;
173    }
174
175    /**
176     * Copies complete state of Invocation object.
177     * @return
178     */

179    public Invocation copy()
180    {
181       ConstructorInvocation wrapper = new ConstructorInvocation(interceptors);
182       wrapper.arguments = this.arguments;
183       wrapper.constructor = this.constructor;
184       wrapper.setAdvisor(this.getAdvisor());
185       wrapper.currentInterceptor = this.currentInterceptor;
186       wrapper.metadata = this.metadata;
187       return wrapper;
188    }
189
190    public Object JavaDoc[] getArguments()
191    {
192       return arguments;
193    }
194
195    public void setArguments(Object JavaDoc[] arguments)
196    {
197       this.arguments = arguments;
198    }
199
200    public Constructor JavaDoc getConstructor()
201    {
202       return constructor;
203    }
204
205    public void setConstructor(Constructor JavaDoc constructor)
206    {
207       this.constructor = constructor;
208    }
209 }
210
Popular Tags