KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > instrument > InstrumentorFactory


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.instrument;
23
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import org.jboss.aop.AspectManager;
27 import org.jboss.aop.classpool.AOPClassPool;
28
29 /**
30  * Comment
31  *
32  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
33  * @version $Revision$
34  */

35 public class InstrumentorFactory
36 {
37    protected static InstrumentorEnum instrumentor;
38    
39    protected static Constructor JavaDoc otherInstrumentorConstructor;
40    
41    private static final Class JavaDoc[] CONSTRUCTOR_SIG = new Class JavaDoc[] {AOPClassPool.class, AspectManager.class, JoinpointClassifier.class, DynamicTransformationObserver.class};
42    
43    public static void initialise(String JavaDoc property)
44    {
45       if (AspectManager.verbose)
46       {
47          System.out.println("[debug] Passed in instrumentor: " + property);
48       }
49       
50       if (property != null)
51       {
52          if (property.equals(ClassicInstrumentor.class.getName()))
53          {
54             instrumentor = InstrumentorEnum.CLASSIC;
55          }
56          else if (property.equals(GeneratedAdvisorInstrumentor.class.getName()))
57          {
58             instrumentor = InstrumentorEnum.GENERATED_ADVISOR;
59          }
60          else
61          {
62             
63             try
64             {
65                Class JavaDoc otherInstrumentorClass = Thread.currentThread().getContextClassLoader().loadClass(property);
66                otherInstrumentorConstructor = otherInstrumentorClass.getConstructor(CONSTRUCTOR_SIG);
67                instrumentor = InstrumentorEnum.OTHER_INSTRUMENTOR;
68             }
69             catch (ClassNotFoundException JavaDoc e)
70             {
71                throw new RuntimeException JavaDoc("Invalid instrumentor " + property + " was used");
72             }
73             catch(NoSuchMethodException JavaDoc e)
74             {
75                throw new RuntimeException JavaDoc(property + " does not have a constructor with the expected signature");
76             }
77          }
78       }
79       else
80       {
81          if (AspectManager.verbose)
82          {
83             System.out.println("[debug] Defaulting instrumentor to: " + ClassicInstrumentor.class.getName());
84          }
85          instrumentor = InstrumentorEnum.GENERATED_ADVISOR;
86       }
87    }
88    
89    public static InstrumentorEnum getInstrumentor()
90    {
91       return instrumentor;
92    }
93    
94    public static Instrumentor getInstrumentor(AOPClassPool pool, AspectManager manager, JoinpointClassifier joinpointClassifier, DynamicTransformationObserver observer)
95    {
96       if (instrumentor == InstrumentorEnum.CLASSIC)
97       {
98          return new ClassicInstrumentor(pool, manager, joinpointClassifier, observer);
99       }
100       else if (instrumentor == InstrumentorEnum.GENERATED_ADVISOR)
101       {
102          return new GeneratedAdvisorInstrumentor(pool, manager, joinpointClassifier, observer);
103       }
104       else if (otherInstrumentorConstructor != null)
105       {
106          try
107          {
108             return (Instrumentor) otherInstrumentorConstructor.newInstance(new Object JavaDoc[] {pool, manager, joinpointClassifier, observer});
109          }
110          catch (Exception JavaDoc e)
111          {
112             throw new RuntimeException JavaDoc(e);
113          }
114       }
115       else
116       {
117          throw new RuntimeException JavaDoc("Instrumentor is not set");
118       }
119    }
120    
121
122    public static Instrumentor getInstrumentor(AspectManager manager, JoinpointClassifier joinpointClassifier)
123    {
124       if (instrumentor == InstrumentorEnum.CLASSIC)
125       {
126          return new ClassicInstrumentor(manager, joinpointClassifier);
127       }
128       else if (instrumentor == InstrumentorEnum.GENERATED_ADVISOR)
129       {
130          return new GeneratedAdvisorInstrumentor(manager, joinpointClassifier);
131       }
132       else
133       {
134          throw new RuntimeException JavaDoc("Instrumentor is not set");
135       }
136    }
137    
138    public static String JavaDoc getInstrumentorName()
139    {
140       if (instrumentor == InstrumentorEnum.CLASSIC)
141       {
142          return ClassicInstrumentor.class.getName();
143       }
144       else if (instrumentor == InstrumentorEnum.GENERATED_ADVISOR)
145       {
146          return GeneratedAdvisorInstrumentor.class.getName();
147       }
148       else if (instrumentor == InstrumentorEnum.OTHER_INSTRUMENTOR)
149       {
150          return otherInstrumentorConstructor.getName();
151       }
152       else
153       {
154          throw new RuntimeException JavaDoc("Instrumentor is not set");
155       }
156    }
157    
158 }
159
Popular Tags