1 22 package org.jboss.test.aop.jdk15.dynamic.common.scenario; 23 24 25 import java.util.Map ; 26 27 import javassist.CtClass; 28 import javassist.CtField; 29 import javassist.CtMethod; 30 import javassist.NotFoundException; 31 32 import org.jboss.aop.Advised; 33 import org.jboss.aop.AspectManager; 34 import org.jboss.aop.ClassAdvisor; 35 import org.jboss.aop.advice.AdviceBinding; 36 import org.jboss.aop.classpool.AOPClassPool; 37 import org.jboss.aop.pointcut.ast.ParseException; 38 import org.jboss.test.aop.jdk15.dynamic.common.BindingInterceptor; 39 import org.jboss.test.aop.jdk15.dynamic.common.InstanceInterceptor; 40 import org.jboss.test.aop.jdk15.dynamic.common.POJOWrappingInfo; 41 42 50 public class ScenarioRunner 51 { 52 private static final AdviceBinding constructorExecution; 54 private static final AdviceBinding methodExecution; 55 private static final AdviceBinding fieldRead; 56 private static final AdviceBinding fieldWrite; 57 private static CtClass POJO_CLASS; 59 private static CtField FIELD; 60 private static CtMethod METHOD; 61 private static String NOT_ADVISED_METHOD; 62 private static CtClass POJO_CLIENT; 63 64 static 65 { 66 try 67 { 68 constructorExecution = new AdviceBinding("execution(*.POJO->new(..))", null); 69 constructorExecution.addInterceptor(BindingInterceptor.class); 70 fieldRead = new AdviceBinding("get(public int *.POJO->counter)", null); 71 fieldRead.addInterceptor(BindingInterceptor.class); 72 fieldWrite = new AdviceBinding("set(public int *.POJO->counter)", null); 73 fieldWrite.addInterceptor(BindingInterceptor.class); 74 methodExecution = new AdviceBinding("execution(public void *.POJO->someMethod())", null); 75 methodExecution.addInterceptor(BindingInterceptor.class); 76 } 77 catch(ParseException e) 78 { 79 throw new RuntimeException ("Unexpected exception", e); 80 } 81 } 82 83 84 87 public ScenarioRunner() 88 { 89 InstanceInterceptor.resetCounts(); 90 BindingInterceptor.resetCounts(); 91 } 92 93 94 102 public POJOWrappingInfo[] interceptPerClassLoadBefore() throws Exception 103 { 104 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3]; 105 this.executeJoinpoints(false); 106 wrappingInfos[0] = getCurrentWrappingInfo(); 107 this.addBindings(); 108 wrappingInfos[1] = getCurrentWrappingInfo(); 109 this.executeJoinpoints(false); 110 this.removeBindings(); 111 wrappingInfos[2] = getCurrentWrappingInfo(); 112 this.executeJoinpoints(false); 113 return wrappingInfos; 114 } 115 116 private static POJOWrappingInfo getCurrentWrappingInfo() throws Exception 117 { 118 return new POJOWrappingInfo(POJO_CLASS, FIELD, METHOD, NOT_ADVISED_METHOD, POJO_CLIENT); 119 } 120 121 128 public POJOWrappingInfo[] interceptPerClassLoadAfter() throws Exception 129 { 130 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[2]; 131 this.addBindings(); 132 this.executeJoinpoints(false); 133 this.executeJoinpoints(false); 134 wrappingInfos[0] = getCurrentWrappingInfo(); 135 this.removeBindings(); 136 wrappingInfos[1] = getCurrentWrappingInfo(); 137 this.executeJoinpoints(false); 138 return wrappingInfos; 139 } 140 141 147 public POJOWrappingInfo[] addAndRemoveBindingTwice() throws Exception 148 { 149 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[5]; 150 this.executeJoinpoints(false); 151 wrappingInfos[0] = getCurrentWrappingInfo(); 152 this.addBindings(); 153 wrappingInfos[1] = getCurrentWrappingInfo(); 154 this.executeJoinpoints(false); 155 this.removeBindings(); 156 wrappingInfos[2] = getCurrentWrappingInfo(); 157 this.executeJoinpoints(false); 158 this.addBindings(); 159 wrappingInfos[3] = getCurrentWrappingInfo(); 160 this.executeJoinpoints(false); 161 this.removeBindings(); 162 wrappingInfos[4] = getCurrentWrappingInfo(); 163 this.executeJoinpoints(false); 164 return wrappingInfos; 165 } 166 167 175 public POJOWrappingInfo[] executeAfterBindingRemoval() throws Exception 176 { 177 this.addBindings(); 178 this.removeBindings(); 179 this.executeJoinpoints(false); 180 return new POJOWrappingInfo[]{getCurrentWrappingInfo()}; 181 } 182 183 189 public POJOWrappingInfo[] interceptPerInstance() throws Exception { 190 executeJoinpoints(false); 191 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[2]; 192 wrappingInfos[0] = getCurrentWrappingInfo(); 193 executeJoinpoints(true); 194 wrappingInfos[1] = getCurrentWrappingInfo(); 195 executeJoinpoints(true); 196 return wrappingInfos; 197 } 198 199 207 public POJOWrappingInfo[] interceptPerInstanceGC() throws Exception 208 { 209 this.instancesCollected = 0; 210 this.executeJoinpoints(false); 211 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3]; 212 wrappingInfos[0] = getCurrentWrappingInfo(); 213 this.executeJoinpoints(true); 214 wrappingInfos[1] = getCurrentWrappingInfo(); 215 this.executeJoinpoints(true); 216 while(this.instancesCollected < 3) { 217 for (int i = 0; i < 10000; i++) 218 { 219 String string = new String ("any string to fill memory space..."); 220 } 221 System.gc(); 222 try 223 { 224 Thread.sleep(10000); 225 } 226 catch (InterruptedException e) 227 { 228 throw new RuntimeException ("Unexpected exception, e"); 229 } 230 } 231 this.executeJoinpoints(false); 232 this.executeJoinpoints(false); 233 wrappingInfos[2] = getCurrentWrappingInfo(); 234 return wrappingInfos; 235 } 236 237 245 public POJOWrappingInfo[] interceptPerClassPerInstance() throws Exception 246 { 247 this.executeJoinpoints(false); 248 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3]; 249 wrappingInfos[0] = getCurrentWrappingInfo(); 250 this.addBindings(); 251 wrappingInfos[1] = getCurrentWrappingInfo(); 252 this.executeJoinpoints(true); 253 wrappingInfos[2] = getCurrentWrappingInfo(); 254 this.removeBindings(); 255 this.executeJoinpoints(false); 256 return wrappingInfos; 257 } 258 259 270 public POJOWrappingInfo[] interceptPerInstancePerClass() throws Exception 271 { 272 this.executeJoinpoints(true); 273 this.executeJoinpoints(true); 274 POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3]; 275 wrappingInfos[0] = getCurrentWrappingInfo(); 276 this.addBindings(); 277 wrappingInfos[1] = getCurrentWrappingInfo(); 278 this.executeJoinpoints(true); 279 wrappingInfos[2] = getCurrentWrappingInfo(); 280 this.removeBindings(); 281 this.executeJoinpoints(false); 282 return wrappingInfos; 283 } 284 285 286 private int instancesCollected = 0; 287 288 291 void pojoCollected() 292 { 293 instancesCollected++; 294 } 295 296 302 private void executeJoinpoints(boolean interceptInstance) 303 { 304 POJO pojo = new POJO(this); 305 InstanceInterceptor instanceInterceptor = null; 306 if (interceptInstance) 307 { 308 instanceInterceptor = new InstanceInterceptor(); 309 Advised advised = (Advised) pojo; 310 advised._getInstanceAdvisor().insertInterceptor(instanceInterceptor); 311 } 312 pojo.counter++; 313 pojo.someMethod(); 314 if (POJO_CLASS == null) 316 { 317 loadPOJOData(); 318 } 319 } 320 321 324 private static void loadPOJOData() 325 { 326 Map cls = AspectManager.instance().getRegisteredCLs(); 327 AOPClassPool classPool = (AOPClassPool) cls.get(POJO.class.getClassLoader()); 328 try 329 { 330 POJO_CLASS = classPool.getLocally(POJO.class.getName()); 331 FIELD = POJO_CLASS.getDeclaredField("counter"); 332 METHOD = POJO_CLASS.getDeclaredMethod("someMethod"); 333 NOT_ADVISED_METHOD = ClassAdvisor.notAdvisedMethodName(POJO.class.getName(), METHOD.getName()); 334 POJO_CLIENT= classPool.getLocally(ScenarioRunner.class.getName()); 335 } 336 catch (NotFoundException e) 337 { 338 throw new RuntimeException ("Unexpected exception", e); 339 } 340 } 341 342 346 void addBindings() 347 { 348 AspectManager aspectManager = AspectManager.instance(); 349 aspectManager.addBinding(constructorExecution); 350 aspectManager.addBinding(fieldRead); 351 aspectManager.addBinding(fieldWrite); 352 aspectManager.addBinding(methodExecution); 353 } 354 355 358 private void removeBindings() 359 { 360 AspectManager aspectManager = AspectManager.instance(); 361 aspectManager.removeBinding(constructorExecution.getName()); 362 aspectManager.removeBinding(fieldRead.getName()); 363 aspectManager.removeBinding(fieldWrite.getName()); 364 aspectManager.removeBinding(methodExecution.getName()); 365 } 366 } | Popular Tags |