1 4 package com.tc.aspectwerkz.definition; 5 6 7 import com.tc.aspectwerkz.DeploymentModel; 8 import com.tc.aspectwerkz.expression.ExpressionContext; 9 import com.tc.aspectwerkz.expression.ExpressionInfo; 10 import com.tc.aspectwerkz.expression.ExpressionVisitor; 11 import com.tc.aspectwerkz.cflow.CflowBinding; 12 import com.tc.aspectwerkz.perx.PerObjectAspect; 13 import com.tc.aspectwerkz.transform.Properties; 14 import com.tc.aspectwerkz.util.SequencedHashMap; 15 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 34 public class SystemDefinition { 35 36 39 public static final Map EMPTY_HASH_MAP = new HashMap(); 40 41 44 private final Map m_aspectMap = new HashMap(); 45 46 49 private final Map m_mixinMap = new SequencedHashMap(); 50 51 54 private String m_uuid = "default"; 55 56 59 private final Set m_includePackages = new HashSet (); 60 61 64 private final Set m_excludePackages = new HashSet (); 65 66 69 private final Set m_preparePackages = new HashSet (); 70 71 74 private final Map m_deploymentScopes = new HashMap(); 75 76 79 public SystemDefinition(final String uuid) { 80 setUuid(uuid); 81 } 82 83 88 private void setUuid(final String uuid) { 89 m_uuid = uuid; 90 } 91 92 97 public String getUuid() { 98 return m_uuid; 99 } 100 101 106 public Set getIncludePackages() { 107 return m_includePackages; 108 } 109 110 115 public Set getExcludePackages() { 116 return m_excludePackages; 117 } 118 119 124 public Collection getAspectDefinitions() { 125 Collection clone = new ArrayList (m_aspectMap.size()); 126 for (Iterator it = m_aspectMap.values().iterator(); it.hasNext();) { 127 clone.add(it.next()); 128 } 129 return clone; 130 } 131 132 137 public Collection getMixinDefinitions() { 138 Collection clone = new ArrayList (m_mixinMap.size()); 139 for (Iterator it = m_mixinMap.values().iterator(); it.hasNext();) { 140 clone.add(it.next()); 141 } 142 return clone; 143 } 144 145 150 public Collection getAdviceDefinitions() { 151 final Collection adviceDefs = new ArrayList (); 152 for (Iterator it = m_aspectMap.values().iterator(); it.hasNext();) { 153 AspectDefinition aspectDef = (AspectDefinition) it.next(); 154 adviceDefs.addAll(aspectDef.getAroundAdviceDefinitions()); 155 adviceDefs.addAll(aspectDef.getBeforeAdviceDefinitions()); 156 adviceDefs.addAll(aspectDef.getAfterAdviceDefinitions()); 157 } 158 return adviceDefs; 159 } 160 161 167 public AspectDefinition getAspectDefinition(final String name) { 168 return (AspectDefinition) m_aspectMap.get(name); 169 } 170 171 177 public List getMixinDefinitions(final ExpressionContext ctx) { 178 final List introDefs = new ArrayList (); 179 for (Iterator it = m_mixinMap.values().iterator(); it.hasNext();) { 180 MixinDefinition introDef = (MixinDefinition) it.next(); 181 for (int i = 0; i < introDef.getExpressionInfos().length; i++) { 182 if (introDef.getExpressionInfos()[i].getExpression().match(ctx)) { 183 introDefs.add(introDef); 184 } 185 } 186 } 187 return introDefs; 188 } 189 190 197 public List getInterfaceIntroductionDefinitions(final ExpressionContext ctx) { 198 if (ctx == null) { 199 throw new IllegalArgumentException ("context can not be null"); 200 } 201 List interfaceIntroductionDefs = new ArrayList (); 202 for (Iterator iterator = m_aspectMap.values().iterator(); iterator.hasNext();) { 203 AspectDefinition aspectDef = (AspectDefinition) iterator.next(); 204 for (Iterator it = aspectDef.getInterfaceIntroductionDefinitions().iterator(); it.hasNext();) { 205 InterfaceIntroductionDefinition introDef = (InterfaceIntroductionDefinition) it.next(); 206 ExpressionInfo[] expressionInfos = introDef.getExpressionInfos(); 207 for (int i = 0; i < expressionInfos.length; i++) { 208 ExpressionInfo expressionInfo = expressionInfos[i]; 209 ExpressionVisitor expression = expressionInfo.getExpression(); 210 if (expression.match(ctx)) { 211 interfaceIntroductionDefs.add(introDef); 212 } 213 } 214 } 215 } 216 return interfaceIntroductionDefs; 217 } 218 219 225 public void addAspect(final AspectDefinition aspectDef) { 226 if (aspectDef == null) { 227 throw new IllegalArgumentException ("aspect definition can not be null"); 228 } 229 230 synchronized (m_aspectMap) { 231 if (m_aspectMap.containsKey(aspectDef.getName())) { 232 return; 233 } 234 235 if (DeploymentModel.PER_TARGET.equals(aspectDef.getDeploymentModel())) { 237 addAspect(PerObjectAspect.getAspectDefinition(this, aspectDef)); 238 } else if (DeploymentModel.PER_THIS.equals(aspectDef.getDeploymentModel())) { 239 addAspect(PerObjectAspect.getAspectDefinition(this, aspectDef)); 240 } 241 242 for (Iterator iterator = aspectDef.getAdviceDefinitions().iterator(); iterator.hasNext();) { 247 AdviceDefinition adviceDefinition = (AdviceDefinition) iterator.next(); 248 List cflowBindings = CflowBinding.getCflowBindingsForCflowOf(adviceDefinition.getExpressionInfo()); 249 for (Iterator cflows = cflowBindings.iterator(); cflows.hasNext();) { 250 CflowBinding cflowBinding = (CflowBinding) cflows.next(); 251 if (!cflowBinding.isCflowBelow()) { 252 addAspect(cflowBinding.getAspectDefinition(this, aspectDef.getClassInfo().getClassLoader())); 253 } 254 } 255 } 256 257 m_aspectMap.put(aspectDef.getName(), aspectDef); 259 260 for (Iterator iterator = aspectDef.getAdviceDefinitions().iterator(); iterator.hasNext();) { 265 AdviceDefinition adviceDefinition = (AdviceDefinition) iterator.next(); 266 List cflowBindings = CflowBinding.getCflowBindingsForCflowOf(adviceDefinition.getExpressionInfo()); 267 for (Iterator cflows = cflowBindings.iterator(); cflows.hasNext();) { 268 CflowBinding cflowBinding = (CflowBinding) cflows.next(); 269 if (cflowBinding.isCflowBelow()) { 270 addAspect(cflowBinding.getAspectDefinition(this, aspectDef.getClassInfo().getClassLoader())); 271 } 272 } 273 } 274 } 275 } 276 277 282 public void addAspectOverwriteIfExists(final AspectDefinition aspectDef) { 283 if (aspectDef == null) { 284 throw new IllegalArgumentException ("aspect definition can not be null"); 285 } 286 synchronized (m_aspectMap) { 287 m_aspectMap.put(aspectDef.getName(), aspectDef); 288 } 289 } 290 291 296 public void addMixinDefinition(final MixinDefinition mixinDef) { 297 if (mixinDef == null) { 298 throw new IllegalArgumentException ("mixin definition can not be null"); 299 } 300 synchronized (m_mixinMap) { 301 if (m_mixinMap.containsKey(mixinDef.getMixinImpl().getName())) { 302 MixinDefinition def = (MixinDefinition) m_mixinMap.get(mixinDef.getMixinImpl().getName()); 303 def.addExpressionInfos(mixinDef.getExpressionInfos()); 304 return; 305 } 306 m_mixinMap.put(mixinDef.getMixinImpl().getName(), mixinDef); 307 } 308 } 309 310 315 public void addIncludePackage(final String includePackage) { 316 synchronized (m_includePackages) { 317 m_includePackages.add(includePackage + '.'); 318 } 319 } 320 321 326 public void addExcludePackage(final String excludePackage) { 327 synchronized (m_excludePackages) { 328 m_excludePackages.add(excludePackage + '.'); 329 } 330 } 331 332 337 public void addPreparePackage(final String preparePackage) { 338 synchronized (m_preparePackages) { 339 m_preparePackages.add(preparePackage + '.'); 340 } 341 } 342 343 348 public Set getPreparePackages() { 349 return m_preparePackages; 350 } 351 352 358 public boolean inIncludePackage(final String className) { 359 if (className == null) { 360 throw new IllegalArgumentException ("class name can not be null"); 361 } 362 if (m_includePackages.isEmpty()) { 363 return true; 364 } 365 for (Iterator it = m_includePackages.iterator(); it.hasNext();) { 366 String packageName = (String ) it.next(); 367 if (className.startsWith(packageName)) { 368 return true; 369 } 370 } 371 return false; 372 } 373 374 380 public boolean inExcludePackage(final String className) { 381 if (className == null) { 382 throw new IllegalArgumentException ("class name can not be null"); 383 } 384 for (Iterator it = m_excludePackages.iterator(); it.hasNext();) { 385 String packageName = (String ) it.next(); 386 if (className.startsWith(packageName)) { 387 return true; 388 } 389 } 390 return false; 391 } 392 393 399 public boolean inPreparePackage(String className) { 400 if (className == null) { 401 throw new IllegalArgumentException ("class name can not be null"); 402 } 403 for (Iterator it = m_preparePackages.iterator(); it.hasNext();) { 404 String packageName = (String ) it.next(); 405 if (className.startsWith(packageName)) { 406 return true; 407 } 408 } 409 return false; 410 } 411 412 418 public boolean hasPointcut(final ExpressionContext ctx) { 419 if (ctx == null) { 420 throw new IllegalArgumentException ("context can not be null"); 421 } 422 for (Iterator it = m_aspectMap.values().iterator(); it.hasNext();) { 423 AspectDefinition aspectDef = (AspectDefinition) it.next(); 424 for (Iterator it2 = aspectDef.getAdviceDefinitions().iterator(); it2.hasNext();) { 425 AdviceDefinition adviceDef = (AdviceDefinition) it2.next(); 426 final ExpressionInfo expressionInfo = adviceDef.getExpressionInfo(); 427 if (expressionInfo == null) { 428 continue; 429 } 430 ExpressionVisitor expression = expressionInfo.getExpression(); 431 432 if (expression.match(ctx)) { 433 if (Properties.VERBOSE_LOGGING) { 434 System.out.println("-----------------------------------------------------"); 435 System.out.println("Pointcut match"); 436 System.out.println(" Pointcut " + expression); 437 System.out.println(" Aspect " + aspectDef.getName()); 438 System.out.println(" Advice " + adviceDef.getName()); 439 System.out.println(" Target " + ctx.getReflectionInfo()); 440 System.out.println(" Within " + ctx.getWithinReflectionInfo()); 441 } 442 return true; 443 } 444 } 445 } 446 return false; 447 } 448 449 455 public boolean isAdvised(final ExpressionContext[] ctxs) { 456 if (ctxs == null) { 457 throw new IllegalArgumentException ("context array can not be null"); 458 } 459 for (Iterator it = m_aspectMap.values().iterator(); it.hasNext();) { 460 AspectDefinition aspectDef = (AspectDefinition) it.next(); 461 List advices = aspectDef.getAdviceDefinitions(); 462 for (Iterator it2 = advices.iterator(); it2.hasNext();) { 463 AdviceDefinition adviceDef = (AdviceDefinition) it2.next(); 464 for (int i = 0; i < ctxs.length; i++) { 465 ExpressionContext ctx = ctxs[i]; 466 final ExpressionInfo expressionInfo = adviceDef.getExpressionInfo(); 467 if (expressionInfo == null) { 468 continue; 469 } 470 if (expressionInfo.getAdvisedClassFilterExpression().match(ctx)) { 471 return true; 472 } 473 } 474 } 475 } 476 return false; 477 } 478 479 485 public boolean isAdvised(final ExpressionContext ctx) { 486 if (ctx == null) { 487 throw new IllegalArgumentException ("context can not be null"); 488 } 489 for (Iterator it = m_aspectMap.values().iterator(); it.hasNext();) { 490 AspectDefinition aspectDef = (AspectDefinition) it.next(); 491 List advices = aspectDef.getAdviceDefinitions(); 492 for (Iterator it2 = advices.iterator(); it2.hasNext();) { 493 AdviceDefinition adviceDef = (AdviceDefinition) it2.next(); 494 final ExpressionInfo expressionInfo = adviceDef.getExpressionInfo(); 495 if (expressionInfo == null) { 496 continue; 497 } 498 if (expressionInfo.getAdvisedClassFilterExpression().match(ctx) ) { 500 return true; 501 } 502 } 503 } 504 return false; 505 } 506 507 513 public boolean hasMixin(final ExpressionContext[] ctxs) { 514 if (ctxs == null) { 515 throw new IllegalArgumentException ("context array can not be null"); 516 } 517 for (Iterator it = m_mixinMap.values().iterator(); it.hasNext();) { 518 MixinDefinition introDef = (MixinDefinition) it.next(); 519 ExpressionInfo[] expressionInfos = introDef.getExpressionInfos(); 520 for (int i = 0; i < expressionInfos.length; i++) { 521 ExpressionInfo expressionInfo = expressionInfos[i]; 522 for (int j = 0; j < ctxs.length; j++) { 523 if (expressionInfo.getExpression().match(ctxs[j])) { 524 return true; 525 } 526 } 527 } 528 } 529 return false; 530 } 531 532 538 public boolean hasMixin(final ExpressionContext ctx) { 539 if (ctx == null) { 540 throw new IllegalArgumentException ("context can not be null"); 541 } 542 for (Iterator it = m_mixinMap.values().iterator(); it.hasNext();) { 543 MixinDefinition introDef = (MixinDefinition) it.next(); 544 ExpressionInfo[] expressionInfos = introDef.getExpressionInfos(); 545 for (int i = 0; i < expressionInfos.length; i++) { 546 ExpressionInfo expressionInfo = expressionInfos[i]; 547 if (expressionInfo.getExpression().match(ctx)) { 548 return true; 549 } 550 } 551 } 552 return false; 553 } 554 555 561 public boolean hasIntroducedInterface(final ExpressionContext[] ctxs) { 562 if (ctxs == null) { 563 throw new IllegalArgumentException ("context array can not be null"); 564 } 565 for (Iterator iterator = m_aspectMap.values().iterator(); iterator.hasNext();) { 566 AspectDefinition aspectDef = (AspectDefinition) iterator.next(); 567 for (Iterator it = aspectDef.getInterfaceIntroductionDefinitions().iterator(); it.hasNext();) { 568 InterfaceIntroductionDefinition introDef = (InterfaceIntroductionDefinition) it.next(); 569 ExpressionInfo[] expressionInfos = introDef.getExpressionInfos(); 570 for (int i = 0; i < expressionInfos.length; i++) { 571 ExpressionInfo expressionInfo = expressionInfos[i]; 572 for (int j = 0; j < ctxs.length; j++) { 573 if (expressionInfo.getExpression().match(ctxs[i])) { 574 return true; 575 } 576 } 577 } 578 } 579 } 580 return false; 581 } 582 583 589 public boolean hasIntroducedInterface(final ExpressionContext ctx) { 590 if (ctx == null) { 591 throw new IllegalArgumentException ("context can not be null"); 592 } 593 for (Iterator iterator = m_aspectMap.values().iterator(); iterator.hasNext();) { 594 AspectDefinition aspectDefinition = (AspectDefinition) iterator.next(); 595 for (Iterator it = aspectDefinition.getInterfaceIntroductionDefinitions().iterator(); it.hasNext();) { 596 InterfaceIntroductionDefinition introDef = (InterfaceIntroductionDefinition) it.next(); 597 ExpressionInfo[] expressionInfos = introDef.getExpressionInfos(); 598 for (int i = 0; i < expressionInfos.length; i++) { 599 ExpressionInfo expressionInfo = expressionInfos[i]; 600 if (expressionInfo.getExpression().match(ctx)) { 601 return true; 602 } 603 } 604 } 605 } 606 return false; 607 } 608 609 614 public Collection getDeploymentScopes() { 615 return m_deploymentScopes.values(); 616 } 617 618 624 public DeploymentScope getDeploymentScope(final String name) { 625 return (DeploymentScope) m_deploymentScopes.get(name); 626 } 627 628 633 public void addDeploymentScope(final DeploymentScope deploymentScope) { 634 m_deploymentScopes.put(deploymentScope.getName(), deploymentScope); 635 636 } 638 639 public boolean equals(Object o) { 640 return ((SystemDefinition) o).m_uuid.equals(m_uuid); 641 } 642 643 public int hashCode() { 644 return m_uuid.hashCode(); 645 } 646 647 653 public static SystemDefinition createVirtualDefinitionAt(ClassLoader loader) { 654 SystemDefinition def = new SystemDefinition(SystemDefinitionContainer.getVirtualDefinitionUuidFor(loader)); 655 DocumentParser.addVirtualAspect(def); 656 return def; 657 } 658 } 659 | Popular Tags |