KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.joinpoint.plugins.BasicConstructorJoinPoint;
25 import org.jboss.joinpoint.plugins.BasicJoinpointFactory;
26 import org.jboss.joinpoint.spi.ConstructorJoinpoint;
27 import org.jboss.joinpoint.spi.JoinpointException;
28 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
29 import org.jboss.reflect.spi.ClassInfo;
30 import org.jboss.reflect.spi.ConstructorInfo;
31 import org.jboss.reflect.spi.TypeInfoFactory;
32 import org.jboss.repository.spi.MetaDataContext;
33
34 /**
35  * The existence of this class is the signal to the kernel that we want to use the aop-mc integration.
36  * When deployed in jboss the AOPConstructorJoinpoint will be deployed as part of the AspectDeployer,
37  * so we use "normal" constructor joinpoints until that has been deployed
38  *
39  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @version $Revision: 58238 $
42  */

43 public class AOPJoinpointFactory extends BasicJoinpointFactory
44 {
45    /** The AOPConstructorJoinpoint constructor one it has been loaded */
46    static volatile ConstructorInfo ctorInfo;
47    
48    /**
49     * Create a new AOPJoinpointFactory.
50     *
51     * @param classInfo
52     */

53    public AOPJoinpointFactory(ClassInfo classInfo, MetaDataContext metaDataContext)
54    {
55       super(classInfo, metaDataContext);
56    }
57
58    public ConstructorJoinpoint getConstructorJoinpoint(ConstructorInfo constructorInfo) throws JoinpointException
59    {
60       ConstructorInfo info = getAOPJoinpointConstructorInfo(constructorInfo);
61       
62       if (info != null)
63       {
64          return createAOPConstructorJoinpoint(info, constructorInfo);
65       }
66       else
67       {
68          return super.getConstructorJoinpoint(constructorInfo);
69       }
70    }
71    
72    private synchronized ConstructorInfo getAOPJoinpointConstructorInfo(ConstructorInfo currentConstructorInfo) throws JoinpointException
73    {
74       if (ctorInfo != null)
75       {
76          return ctorInfo;
77       }
78       
79       Class JavaDoc clazz = AOPDeployedChecker.getClassIfExists(
80             classInfo.getType().getClassLoader(),
81             "org.jboss.aop.microcontainer.integration.AOPConstructorJoinpoint");
82       
83       if (clazz == null)
84       {
85          return null;
86       }
87       
88       TypeInfoFactory factory = new IntrospectionTypeInfoFactory();
89       ClassInfo info = (ClassInfo)factory.getTypeInfo(clazz);
90       ConstructorInfo[] ctors = info.getDeclaredConstructors();
91       for (int i = 0 ; i < ctors.length ; i++)
92       {
93          if (ctors[i].getParameterTypes().length == 2)
94          {
95             if (ctors[i].getParameterTypes()[0].getName().equals(ConstructorInfo.class.getName()) == false)
96             {
97                continue;
98             }
99             
100             if (ctors[i].getParameterTypes()[1].getName().equals(MetaDataContext.class.getName()) == false)
101             {
102                continue;
103             }
104             ctorInfo = ctors[i];
105             break;
106          }
107       }
108       
109       if (ctorInfo == null)
110       {
111          throw new JoinpointException("No constructor found with the reqiured signature AOPConstructorJoinpoint(ConstructorInfo, MetadataContext)");
112       }
113       return ctorInfo;
114    }
115    
116    private ConstructorJoinpoint createAOPConstructorJoinpoint(ConstructorInfo info, ConstructorInfo aopCtorInfo) throws JoinpointException
117    {
118       ConstructorJoinpoint jp = new BasicConstructorJoinPoint(info);
119       jp.setArguments(new Object JavaDoc[] {aopCtorInfo, metaDataContext});
120       try
121       {
122          return (ConstructorJoinpoint)jp.dispatch();
123       }
124       catch (Throwable JavaDoc e)
125       {
126          throw new JoinpointException("Error calling AOPConstructorJoinpoint constructor", e);
127       }
128       
129    }
130 }
131
Popular Tags