1 22 package org.jboss.aop.instrument; 23 24 import java.lang.reflect.Constructor ; 25 26 import org.jboss.aop.AspectManager; 27 import org.jboss.aop.classpool.AOPClassPool; 28 29 35 public class InstrumentorFactory 36 { 37 protected static InstrumentorEnum instrumentor; 38 39 protected static Constructor otherInstrumentorConstructor; 40 41 private static final Class [] CONSTRUCTOR_SIG = new Class [] {AOPClassPool.class, AspectManager.class, JoinpointClassifier.class, DynamicTransformationObserver.class}; 42 43 public static void initialise(String 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 otherInstrumentorClass = Thread.currentThread().getContextClassLoader().loadClass(property); 66 otherInstrumentorConstructor = otherInstrumentorClass.getConstructor(CONSTRUCTOR_SIG); 67 instrumentor = InstrumentorEnum.OTHER_INSTRUMENTOR; 68 } 69 catch (ClassNotFoundException e) 70 { 71 throw new RuntimeException ("Invalid instrumentor " + property + " was used"); 72 } 73 catch(NoSuchMethodException e) 74 { 75 throw new RuntimeException (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 [] {pool, manager, joinpointClassifier, observer}); 109 } 110 catch (Exception e) 111 { 112 throw new RuntimeException (e); 113 } 114 } 115 else 116 { 117 throw new RuntimeException ("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 ("Instrumentor is not set"); 135 } 136 } 137 138 public static String 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 ("Instrumentor is not set"); 155 } 156 } 157 158 } 159 | Popular Tags |