1 22 package org.jboss.aop; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Method ; 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 39 public class AdvisorFactory 40 { 41 42 protected static final int CLASS = 1; 43 protected static final int OTHER_ADVISOR = 1000; protected static int advisor = 0; 45 46 protected static Constructor otherAdvisorConstructor; 47 48 private static final Class [] NO_ARGS = new Class [0]; 49 private static final Class [] CONSTRUCTOR_SIG = new Class [] {String .class, AspectManager.class}; 50 51 52 public static void initialise(String 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 otherAdvisorClass = Thread.currentThread().getContextClassLoader().loadClass(property); 70 otherAdvisorConstructor = otherAdvisorClass.getConstructor(CONSTRUCTOR_SIG); 71 } 72 catch (ClassNotFoundException e) 73 { 74 throw new RuntimeException ("Invalid advisor " + property + " was used"); 75 } 76 catch(NoSuchMethodException e) 77 { 78 throw new RuntimeException (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 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 className, AspectManager am, Class loadedClass) 103 { 104 if(advisor == CLASS) 105 { 106 if (loadedClass != null) 107 { 108 if (InstrumentorFactory.getInstrumentor() == InstrumentorEnum.GENERATED_ADVISOR) 109 { 110 try 112 { 113 Method getAdvisor = loadedClass.getMethod(GeneratedAdvisorInstrumentor.GET_CLASS_ADVISOR, NO_ARGS); 114 ClassAdvisor advisor = (ClassAdvisor)getAdvisor.invoke(null, null); 115 if (advisor.getClazz() == loadedClass) 116 { 117 return advisor; 119 } 120 } 121 catch (NoSuchMethodException e) 122 { 123 } 125 catch (Exception e) 126 { 127 throw new RuntimeException (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 [] {className, am}); 139 } 140 catch (Exception e) 141 { 142 throw new RuntimeException (e); 143 } 144 } 145 else 146 { 147 throw new RuntimeException ("Advisor is not set"); 148 } 149 } 150 151 } 152 | Popular Tags |