KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > integration > AOPConstructorJoinpoint


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.microcontainer.integration;
23
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import org.jboss.aop.Advisor;
27 import org.jboss.aop.AspectManager;
28 import org.jboss.aop.advice.Interceptor;
29 import org.jboss.aop.joinpoint.ConstructorInvocation;
30 import org.jboss.aop.proxy.container.AOPProxyFactory;
31 import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
32 import org.jboss.aop.proxy.container.ContainerCache;
33 import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
34 import org.jboss.joinpoint.plugins.BasicConstructorJoinPoint;
35 import org.jboss.reflect.spi.ConstructorInfo;
36 import org.jboss.reflect.spi.TypeInfo;
37 import org.jboss.repository.spi.MetaDataContext;
38
39 /**
40  * An AOPConstructorJoinpoint.
41  *
42  * @TODO This is not correct if the target is already advised
43  * there is no need for the proxy advisor.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 58323 $
47  */

48 public class AOPConstructorJoinpoint extends BasicConstructorJoinPoint
49 {
50    AOPProxyFactory proxyFactory = new GeneratedAOPProxyFactory();
51
52    MetaDataContext metaDataContext;
53    
54    /**
55     * Create a new AOPConstructorJoinpoint.
56     *
57     * @param constructorInfo the constructor info
58     */

59    public AOPConstructorJoinpoint(ConstructorInfo constructorInfo, MetaDataContext metaDataContext)
60    {
61       super(constructorInfo);
62       this.metaDataContext = metaDataContext;
63    }
64
65    public Object JavaDoc dispatch() throws Throwable JavaDoc
66    {
67       Class JavaDoc clazz = constructorInfo.getDeclaringClass().getType();
68       AspectManager manager = AspectManager.instance();
69       if (manager.isNonAdvisableClassName(clazz.getName()))
70       {
71          return super.dispatch();
72       }
73       ContainerCache cache = ContainerCache.initialise(manager, clazz, metaDataContext);
74       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
75       Object JavaDoc target = createTarget(cache, params);
76
77       params.setProxiedClass(target.getClass());
78       params.setMetaDataContext(metaDataContext);
79       params.setTarget(target);
80       params.setContainerCache(cache);
81
82       return proxyFactory.createAdvisedProxy(params);
83    }
84
85    private Object JavaDoc createTarget(ContainerCache cache, AOPProxyFactoryParameters params) throws Throwable JavaDoc
86    {
87       Advisor advisor = cache.getAdvisor();
88       if (advisor != null)
89       {
90          org.jboss.aop.ConstructorInfo aopinfo = findAopConstructorInfo(advisor);
91          
92          Interceptor[] interceptors = (aopinfo != null) ? aopinfo.getInterceptors() : null;
93
94          if (interceptors != null)
95          {
96             ConstructorInvocation inv = new ConstructorInvocation(aopinfo, aopinfo.getInterceptors());
97             inv.setArguments(getArguments());
98             return inv.invokeNext();
99          }
100          
101          if (getConstructorInfo().getParameterTypes().length > 0)
102          {
103             Constructor JavaDoc constructor = null;
104             if (aopinfo == null)
105             {
106                //Fall back to using the class;
107
Class JavaDoc clazz = advisor.getClazz();
108                Constructor JavaDoc[] ctors = clazz.getConstructors();
109                for (Constructor JavaDoc ctor : ctors)
110                {
111                   if (matchConstructor(ctor))
112                   {
113                      constructor = ctor;
114                      break;
115                   }
116                }
117             }
118             else
119             {
120                constructor = aopinfo.getConstructor();
121             }
122             params.setCtor(constructor.getParameterTypes(), getArguments());
123          }
124       }
125
126       return super.dispatch();
127    }
128
129    private org.jboss.aop.ConstructorInfo findAopConstructorInfo(Advisor advisor)
130    {
131       org.jboss.aop.ConstructorInfo[] infos = advisor.getConstructorInfos();
132       for (int i = 0 ; i < infos.length ; i++)
133       {
134          if (matchConstructor(infos[i].getConstructor()))
135          {
136             return infos[i];
137          }
138       }
139       return null;
140    }
141    
142    private boolean matchConstructor(Constructor JavaDoc ctor)
143    {
144       TypeInfo[] params = constructorInfo.getParameterTypes();
145       Class JavaDoc[] ctorParams = ctor.getParameterTypes();
146       if (params.length == ctorParams.length)
147       {
148          boolean match = true;
149          for (int p = 0 ; p < params.length ; p++)
150          {
151             if (!params[p].getName().equals(ctorParams[p].getName()))
152             {
153                match = false;
154                break;
155             }
156          }
157
158          if (match)
159          {
160             return true;
161          }
162       }
163       return false;
164    }
165
166 }
167
Popular Tags