KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > AdvisorFactory


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;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import javassist.CtClass;
28
29 import org.jboss.aop.instrument.GeneratedAdvisorInstrumentor;
30 import org.jboss.aop.instrument.InstrumentorEnum;
31 import org.jboss.aop.instrument.InstrumentorFactory;
32
33 /**
34  *
35  *
36  * @author <a HREF="mailto:stalep@conduct.no">Staale W. Pedersen</a>
37  * @version $Revision
38  */

39 public class AdvisorFactory
40 {
41    
42    protected static final int CLASS = 1;
43    protected static final int OTHER_ADVISOR = 1000; //The jrockit aop advisor is in another jar which we should not depend on
44
protected static int advisor = 0;
45    
46    protected static Constructor JavaDoc otherAdvisorConstructor;
47    
48    private static final Class JavaDoc[] NO_ARGS = new Class JavaDoc[0];
49    private static final Class JavaDoc[] CONSTRUCTOR_SIG = new Class JavaDoc[] {String JavaDoc.class, AspectManager.class};
50    
51    
52    public static void initialise(String JavaDoc property)
53    {
54       if (AspectManager.verbose)
55       {
56          System.out.println("[debug] Passed in advisor: " + property);
57       }
58
59       if(property != null)
60       {
61          if (property.equals(ClassAdvisor.class.getName()))
62          {
63             advisor = CLASS;
64          }
65          else
66          {
67             try
68             {
69                Class JavaDoc otherAdvisorClass = Thread.currentThread().getContextClassLoader().loadClass(property);
70                otherAdvisorConstructor = otherAdvisorClass.getConstructor(CONSTRUCTOR_SIG);
71             }
72             catch (ClassNotFoundException JavaDoc e)
73             {
74                throw new RuntimeException JavaDoc("Invalid advisor " + property + " was used");
75             }
76             catch(NoSuchMethodException JavaDoc e)
77             {
78                throw new RuntimeException JavaDoc(property + " does not have a constructor with the expected signature");
79             }
80          }
81       }
82       else
83       {
84          if (AspectManager.verbose)
85          {
86             System.out.println("[debug] Defaulting advisor to: " + ClassAdvisor.class.getName());
87          }
88          advisor = CLASS;
89       }
90    }
91
92    public static ClassAdvisor getClassAdvisor(Class JavaDoc clazz, AspectManager am)
93    {
94       return getClassAdvisor(clazz.getName(), am, clazz);
95    }
96    
97    public static ClassAdvisor getClassAdvisor(CtClass clazz, AspectManager am)
98    {
99       return getClassAdvisor(clazz.getName(), am, null);
100    }
101    
102    private static ClassAdvisor getClassAdvisor(String JavaDoc className, AspectManager am, Class JavaDoc loadedClass)
103    {
104       if(advisor == CLASS)
105       {
106          if (loadedClass != null)
107          {
108             if (InstrumentorFactory.getInstrumentor() == InstrumentorEnum.GENERATED_ADVISOR)
109             {
110                //Generated advisors need to be initialised via the class itself
111
try
112                {
113                   Method JavaDoc getAdvisor = loadedClass.getMethod(GeneratedAdvisorInstrumentor.GET_CLASS_ADVISOR, NO_ARGS);
114                   ClassAdvisor advisor = (ClassAdvisor)getAdvisor.invoke(null, null);
115                   if (advisor.getClazz() == loadedClass)
116                   {
117                      //Sub classes may not be instrumented, in which case we have the super class advisor here, which we don't want
118
return advisor;
119                   }
120                }
121                catch (NoSuchMethodException JavaDoc e)
122                {
123                   //This is an interface or the class is not advised, fall back to creating a normalClassAdvisor
124
}
125                catch (Exception JavaDoc e)
126                {
127                   throw new RuntimeException JavaDoc(e);
128                }
129             }
130          }
131
132          return new ClassAdvisor(className, am);
133       }
134       else if(otherAdvisorConstructor != null)
135       {
136          try
137          {
138             return (ClassAdvisor) otherAdvisorConstructor.newInstance(new Object JavaDoc[] {className, am});
139          }
140          catch (Exception JavaDoc e)
141          {
142             throw new RuntimeException JavaDoc(e);
143          }
144       }
145       else
146       {
147          throw new RuntimeException JavaDoc("Advisor is not set");
148       }
149    }
150    
151 }
152
Popular Tags