1 27 package ch.ethz.prose.crosscut; 28 29 import java.lang.reflect.InvocationTargetException ; 30 import java.util.Iterator ; 31 32 import ch.ethz.jvmai.ExceptionJoinPoint; 33 import ch.ethz.jvmai.ExceptionCatchJoinPoint; 34 import ch.ethz.jvmai.FieldAccessJoinPoint; 35 import ch.ethz.jvmai.FieldModificationJoinPoint; 36 import ch.ethz.jvmai.JoinPoint; 37 import ch.ethz.jvmai.MethodEntryJoinPoint; 38 import ch.ethz.jvmai.MethodExitJoinPoint; 39 import ch.ethz.prose.ProseSystem; 40 import ch.ethz.prose.Aspect; 41 import ch.ethz.prose.filter.PointCutter; 42 import ch.ethz.prose.filter.PointFilter; 43 import ch.ethz.prose.engine.JoinPointRequest; 44 import ch.ethz.prose.engine.JoinPointManager; 45 46 47 48 85 public 86 abstract class AbstractCrosscut extends Crosscut { 87 88 private Aspect theOwner = null; 89 private PointFilter theSpecializer = null; 90 private CrosscutGroup transactionGrp = new CrosscutGroup(); 91 private static ThreadLocal threadLocalJoinPoint = new ThreadLocal (); 92 93 94 99 protected transient JoinPointManager requestFactory = null; 100 101 108 public void insertionAction(boolean beforeInsertion) 109 { 110 if (requestFactory == null) 112 requestFactory = ProseSystem.getAspectManager().getJoinPointManager(); 113 } 114 115 116 public void withdrawalAction(boolean beforeInsertion) 117 { 118 } 120 121 122 126 public void setOwner(Aspect ext) throws IllegalStateException 127 { 128 if (theOwner == null) 129 theOwner = ext; 130 else 131 throw new IllegalStateException ("Cannot change ownership of aspects"); 132 133 } 134 135 136 137 public Aspect getOwner() 138 { 139 return theOwner; 140 } 141 142 public int getPriority() 143 { 144 if (getOwner() == null) 145 throw new IllegalStateException ("A crosscut cannot live outside of an aspect"); 146 147 return getOwner().getPriority(); 148 } 149 150 161 protected synchronized Class [] potentialCrosscutClasses() 162 { 163 return (Class [])requestFactory.getLoadedClasses().toArray(new Class []{}); 164 } 165 166 173 protected boolean isPotentialCrosscutClass(Class c) 174 { 175 return true; 176 } 177 178 179 180 195 public final CrosscutRequest createRequest(Class c) 196 { 197 requestFactory = ProseSystem.getAspectManager().getJoinPointManager(); 199 200 CrosscutRequest result = new CrosscutRequest(); 201 202 if (!isPotentialCrosscutClass(c)) 206 { 207 return result; 208 } 209 210 211 229 CrosscutRequest cr = doCreateRequest(c); 231 232 if (getSpecializer() == null) 233 return cr; 234 235 Iterator i = cr.iterator(); 236 while (i.hasNext()) 237 { 238 JoinPointRequest jpr = (JoinPointRequest)i.next(); 239 if (getSpecializer().isSpecialRequest(jpr)) 240 result.add(jpr); 241 } 242 243 return result; 244 245 } 246 247 248 255 protected abstract CrosscutRequest doCreateRequest(Class c); 256 257 264 protected JoinPoint thisJoinPoint() throws IllegalStateException 265 { 266 return (JoinPoint)threadLocalJoinPoint.get(); 267 } 268 269 280 public final CrosscutRequest createRequest() 281 { 282 requestFactory = ProseSystem.getAspectManager().getJoinPointManager(); 284 285 CrosscutRequest result = new CrosscutRequest(); 288 289 Class [] theClasses = potentialCrosscutClasses(); 291 if (theClasses == null) 292 return result; 293 294 298 for(int i =0; i <theClasses.length; i++) 301 result.addAll(createRequest(theClasses[i])); 302 return result; 303 } 304 305 316 public void joinPointReached(MethodEntryJoinPoint jp) throws Exception 317 { 318 319 if ( transactionGrp.executeAdvice && 320 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 321 { 322 threadLocalJoinPoint.set(jp); 323 joinPointAction(jp); 324 } 325 } 326 327 protected PointCutter NOT(PointCutter cs) 328 { 329 return new ch.ethz.prose.filter.NegatingPointCutter(cs); 330 } 331 332 343 public void joinPointReached(MethodExitJoinPoint jp) throws Exception 344 { 345 if ( transactionGrp.executeAdvice && 346 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 347 { 348 threadLocalJoinPoint.set(jp); 349 joinPointAction(jp); 350 } 351 } 352 353 354 355 366 public void joinPointReached(FieldAccessJoinPoint jp) throws Exception 367 { 368 if ( transactionGrp.executeAdvice && 369 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 370 { 371 threadLocalJoinPoint.set(jp); 372 joinPointAction(jp); 373 } 374 } 375 376 377 388 public void joinPointReached(FieldModificationJoinPoint jp) throws Exception 389 { 390 if ( transactionGrp.executeAdvice && 391 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 392 { 393 threadLocalJoinPoint.set(jp); 394 joinPointAction(jp); 395 } 396 } 397 398 399 410 public void joinPointReached(ExceptionJoinPoint jp) throws Exception 411 { 412 413 if ( transactionGrp.executeAdvice && 414 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 415 { 416 threadLocalJoinPoint.set(jp); 417 joinPointAction(jp); 418 } 419 } 420 421 432 public void joinPointReached(ExceptionCatchJoinPoint jp) throws Exception 433 { 434 if ( transactionGrp.executeAdvice && 436 ( (getSpecializer() == null) || getSpecializer().isSpecialEvent(jp))) 437 { 438 threadLocalJoinPoint.set(jp); 439 joinPointAction(jp); 440 } 441 } 442 443 444 449 protected void joinPointAction(FieldModificationJoinPoint e) 450 throws InvocationTargetException ,IllegalAccessException 451 { 452 throw new Error ("Should not be reached; it should be overriden in subclasses"); 453 } 454 455 460 protected void joinPointAction(FieldAccessJoinPoint e) 461 throws InvocationTargetException ,IllegalAccessException 462 { 463 throw new Error ("Should not be reached. it should be overriden in subclasses"); 464 } 465 466 471 protected void joinPointAction(MethodEntryJoinPoint e) 472 throws InvocationTargetException ,IllegalAccessException 473 { 474 throw new Error ("Should not be reached.it should be overriden in subclasses"); 475 } 476 477 482 protected void joinPointAction(MethodExitJoinPoint e) 483 throws InvocationTargetException ,IllegalAccessException 484 { 485 throw new Error ("Should not be reached.it should be overriden in subclasses"); 486 } 487 488 493 protected void joinPointAction(ExceptionJoinPoint e) 494 throws InvocationTargetException ,IllegalAccessException 495 { 496 throw new Error ("Should not be reached.it should be overriden in subclasses"); 497 } 498 499 504 protected void joinPointAction(ExceptionCatchJoinPoint e) 505 throws InvocationTargetException ,IllegalAccessException 506 { 507 throw new Error ("Should not be reached.it should be overriden in subclasses"); 508 } 509 510 511 516 public PointFilter getSpecializer() 517 { 518 if (theSpecializer == null) 519 theSpecializer = pointCutter(); 520 521 return theSpecializer; 522 } 523 524 protected abstract PointCutter pointCutter(); 525 526 527 531 public void associateToGroup(CrosscutGroup grp) throws IllegalArgumentException 532 { 533 transactionGrp = grp; 534 } 535 536 539 public String toString() 540 { 541 return "Crosscut '" + getClass().getName() + 542 "' specialized with [" + getSpecializer() + "]"; 543 } 544 } 545 546 547
| Popular Tags
|