1 22 package org.jboss.test.aop.jdk15.dynamic.common; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.security.AccessController ; 30 import java.security.PrivilegedActionException ; 31 import java.security.PrivilegedExceptionAction ; 32 import java.util.Arrays ; 33 import java.util.Properties ; 34 35 import org.jboss.aop.AspectManager; 36 37 47 public class ScenarioLoader 48 { 49 private URL [] urls; 50 private ClassLoader classLoader; 51 private Object scenarioRunner; 52 private Class scenarioRunnerClass; 53 private Field totalInterceptions; 54 private Field fieldReadInterceptions; 55 private Field fieldWriteInterceptions; 56 private Field constructorInterceptions; 57 private Field methodInterceptions; 58 private boolean scenarioRunned = false; 59 60 63 public ScenarioLoader(Properties properties) throws Exception 64 { 65 urls = new URL []{new URL ("file:" + properties.getProperty("scenario.jar"))}; 66 67 System.out.println("URLS " + Arrays.asList(urls)); 68 this.classLoader = new URLClassLoader (urls, this.getClass().getClassLoader()); 70 this.scenarioRunnerClass = classLoader.loadClass(getClass().getPackage().getName() + ".scenario.ScenarioRunner"); 72 Class interceptionsCountClass = this.classLoader.loadClass(InterceptionsCount.class.getName()); 73 this.totalInterceptions = interceptionsCountClass.getField("total"); 74 this.fieldReadInterceptions = interceptionsCountClass.getField("fieldRead"); 75 this.fieldWriteInterceptions = interceptionsCountClass.getField("fieldWrite"); 76 this.constructorInterceptions = interceptionsCountClass.getField("constructor"); 77 this.methodInterceptions = interceptionsCountClass.getField("method"); 78 79 this.scenarioRunner = scenarioRunnerClass.newInstance(); 80 } 81 82 87 public POJOWrappingInfo[] interceptPerClassLoadBefore() throws Throwable 88 { 89 return this.executeScenario("interceptPerClassLoadBefore"); 90 } 91 92 97 public POJOWrappingInfo[] interceptPerClassLoadAfter() throws Throwable 98 { 99 return this.executeScenario("interceptPerClassLoadAfter"); 100 } 101 102 107 public POJOWrappingInfo[] addAndRemoveBindingTwice() throws Throwable 108 { 109 return this.executeScenario("addAndRemoveBindingTwice"); 110 } 111 112 117 public POJOWrappingInfo[] executeAfterBindingRemoval() throws Throwable 118 { 119 return this.executeScenario("executeAfterBindingRemoval"); 120 } 121 122 127 public POJOWrappingInfo[] interceptPerInstance() throws Throwable { 128 return this.executeScenario("interceptPerInstance"); 129 } 130 131 136 public POJOWrappingInfo[] interceptPerInstanceGC() throws Throwable 137 { 138 return this.executeScenario("interceptPerInstanceGC"); 139 } 140 141 146 public POJOWrappingInfo[] interceptPerClassPerInstance() throws Throwable 147 { 148 return this.executeScenario("interceptPerClassPerInstance"); 149 } 150 151 156 public POJOWrappingInfo[] interceptPerInstancePerClass() throws Throwable 157 { 158 return this.executeScenario("interceptPerInstancePerClass"); 159 } 160 161 166 private POJOWrappingInfo[] executeScenario(String scenarioName) throws Throwable 167 { 168 if (scenarioRunned) 169 throw new RuntimeException ("Should not run more than one scenario."); 170 Method method = this.scenarioRunnerClass.getMethod(scenarioName); 171 POJOWrappingInfo[] result = null; 172 try 173 { 174 result = (POJOWrappingInfo[]) method.invoke(scenarioRunner, new Object [0]); 175 } catch (InvocationTargetException e) 176 { 177 throw e.getCause(); 178 } 179 scenarioRunned = true; 180 this.removeClassLoaderFromAspectManager(); 181 return result; 182 } 183 184 192 public void removeClassLoaderFromAspectManager() 193 { 194 AspectManager.instance().unregisterClassLoader(classLoader); 195 } 196 } | Popular Tags |