1 4 package com.tc.aspectwerkz.definition; 5 6 11 import org.w3c.dom.Attr ; 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Element ; 14 import org.w3c.dom.NamedNodeMap ; 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 import org.w3c.dom.Text ; 18 19 import com.tc.aspectwerkz.DeploymentModel; 20 import com.tc.aspectwerkz.expression.ExpressionInfo; 21 import com.tc.aspectwerkz.expression.ExpressionNamespace; 22 import com.tc.aspectwerkz.annotation.AspectAnnotationParser; 23 import com.tc.aspectwerkz.annotation.MixinAnnotationParser; 24 import com.tc.aspectwerkz.aspect.AdviceType; 25 import com.tc.aspectwerkz.exception.DefinitionException; 26 import com.tc.aspectwerkz.intercept.AdvisableImpl; 27 import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfo; 28 import com.tc.aspectwerkz.reflect.impl.java.JavaClassInfo; 29 import com.tc.aspectwerkz.reflect.impl.java.JavaMethodInfo; 30 import com.tc.aspectwerkz.reflect.ClassInfo; 31 import com.tc.aspectwerkz.reflect.MethodInfo; 32 import com.tc.aspectwerkz.transform.inlining.AspectModelManager; 33 import com.tc.aspectwerkz.util.Strings; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Set ; 41 import java.util.StringTokenizer ; 42 43 49 public class DocumentParser { 50 51 57 public static List parseAspectClassNames(Document document) { 58 List aspectClassNames = new ArrayList (); 59 60 Element documentElement = document.getDocumentElement(); 61 NodeList systemNodes = documentElement.getChildNodes(); 62 for (int i = 0; i<systemNodes.getLength(); i++) { 63 Node systemNode = systemNodes.item(i); 64 String nodeName = systemNode.getNodeName(); 65 if(nodeName.equals("system")) { 66 String basePackage = getBasePackage((Element ) systemNode); 67 68 NodeList childNodes = systemNode.getChildNodes(); 69 for (int j = 0; j < childNodes.getLength(); j++) { 70 Node childNode = childNodes.item(j); 71 if(childNode.getNodeName().equals("aspect")) { 72 addAspectClassName(aspectClassNames, childNode, basePackage); 73 74 } else if(childNode.getNodeName().equals("package")) { 75 NodeList aspectNodes = childNode.getChildNodes(); 76 for (int k = 0; k < aspectNodes.getLength(); k++) { 77 Node aspectNode = aspectNodes.item(k); 78 if(aspectNode.getNodeName().equals("aspect")) { 79 addAspectClassName(aspectClassNames, aspectNode, basePackage); 80 } 81 } 82 } 83 } 84 } 85 } 86 87 aspectClassNames.add(Virtual.class.getName()); 88 return aspectClassNames; 89 } 90 91 private static void addAspectClassName(List aspectClassNames, Node aspectNode, String basePackage) { 92 if(aspectNode.getNodeName().equals("aspect")) { 93 NamedNodeMap attrs = aspectNode.getAttributes(); 94 for (int l = 0; l < attrs.getLength(); l++) { 95 Node attr = attrs.item(l); 96 if(attr.getNodeName().equals("class")) { 97 aspectClassNames.add(basePackage + ((Attr ) attr).getValue().trim()); 98 } 99 } 100 } 101 } 102 103 112 public static AspectDefinition parseAspectDefinition(String xmlDef, 113 SystemDefinition systemDef, 114 Class aspectClass) { 115 Document document; 116 try { 117 document = XmlParser.createDocument(xmlDef); 118 } catch (IOException e) { 119 throw new DefinitionException("Unable to parse definition; "+e.toString()); 120 } 121 122 Element aspectElement = document.getDocumentElement(); 123 124 if (!"aspect".equals(aspectElement.getNodeName())) { 125 throw new DefinitionException("XML definition for aspect is not well-formed: " + xmlDef); 126 } 127 String specialAspectName = null; 128 String className = null; 129 String deploymentModelAsString = null; 130 String containerClassName = null; 131 NamedNodeMap aspectAttributes = aspectElement.getAttributes(); 132 for (int i = 0; i < aspectAttributes.getLength(); i++) { 133 Node attribute = aspectAttributes.item(i); 134 String name = attribute.getNodeName().trim(); 135 if (name.equalsIgnoreCase("class")) { 136 className = ((Attr ) attribute).getValue().trim(); 137 } else if (name.equalsIgnoreCase("deployment-model")) { 138 deploymentModelAsString = ((Attr ) attribute).getValue().trim(); 139 } else if (name.equalsIgnoreCase("name")) { 140 specialAspectName = ((Attr ) attribute).getValue().trim(); 141 } else if (name.equalsIgnoreCase("container")) { 142 containerClassName = ((Attr ) attribute).getValue().trim(); 143 } 144 } 145 if (specialAspectName == null || specialAspectName.trim().length() == 0) { 146 specialAspectName = className; 147 } 148 149 ClassInfo classInfo = JavaClassInfo.getClassInfo(aspectClass); 150 ClassLoader loader = aspectClass.getClassLoader(); 151 152 AspectDefinition aspectDef = new AspectDefinition(specialAspectName, classInfo, systemDef); 154 aspectDef.setContainerClassName(containerClassName); 156 aspectDef.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModelAsString)); 157 158 parsePointcutElements(aspectElement, aspectDef); 160 AspectModelManager.defineAspect(classInfo, aspectDef, loader); 162 163 parseParameterElements(aspectElement, aspectDef); 165 parsePointcutElements(aspectElement, aspectDef); parseAdviceElements(aspectElement, aspectDef, JavaClassInfo.getClassInfo(aspectClass)); 167 parseIntroduceElements(aspectElement, aspectDef, "", aspectClass.getClassLoader()); 168 return aspectDef; 169 } 170 171 178 public static Set parse(ClassLoader loader, Document document) { 179 return parseSystemElements(loader, document.getDocumentElement()); 181 } 182 183 189 private static Set parseSystemElements(ClassLoader loader, Element root) { 190 Set systemDefs = new HashSet (); 191 192 NodeList rootNodes = root.getChildNodes(); 193 for (int i = 0; i < rootNodes.getLength(); i++) { 194 Node childNode = rootNodes.item(i); 195 if(childNode.getNodeName().equals("system")) { 196 Element systemElement = (Element ) childNode; 197 SystemDefinition definition = parseSystemElement(loader, systemElement, getBasePackage(systemElement)); 198 if (definition != null) { 199 systemDefs.add(definition); 200 } 201 } 202 } 203 return systemDefs; 204 } 205 206 214 private static SystemDefinition parseSystemElement(ClassLoader loader, 215 Element systemElement, 216 String basePackage) { 217 String uuid = systemElement.getAttribute("id"); 218 if (uuid == null || uuid.equals("")) { 219 throw new DefinitionException("system UUID must be specified"); 220 } 221 SystemDefinition definition = new SystemDefinition(uuid); 222 223 addVirtualAspect(definition); 225 226 List globalPointcuts = parseGlobalPointcutDefs(systemElement); 228 229 ExpressionNamespace systemNamespace = ExpressionNamespace.getNamespace(definition.getUuid()); 231 for (Iterator iterator = globalPointcuts.iterator(); iterator.hasNext();) { 232 PointcutInfo pointcutInfo = (PointcutInfo) iterator.next(); 233 systemNamespace.addExpressionInfo( 234 pointcutInfo.name, new ExpressionInfo(pointcutInfo.expression, systemNamespace.getName()) 235 ); 236 } 237 238 parseDeploymentScopeDefs(systemElement, definition); 240 241 parseAdvisableDefs(systemElement, definition); 243 244 parseIncludePackageElements(systemElement, definition, basePackage); 246 parseExcludePackageElements(systemElement, definition, basePackage); 247 parsePrepareElements(systemElement, definition, basePackage); 248 249 parseAspectElements(loader, systemElement, definition, basePackage, globalPointcuts); 251 252 parseMixinElements(loader, systemElement, definition, basePackage); 254 255 parsePackageElements(loader, systemElement, definition, basePackage, globalPointcuts); 257 258 DefinitionParserHelper.attachDeploymentScopeDefsToVirtualAdvice(definition); 260 261 return definition; 262 } 263 264 270 private static List parseGlobalPointcutDefs(Element systemElement) { 271 List globalPointcuts = new ArrayList (); 272 273 NodeList childNodes = systemElement.getChildNodes(); 274 for (int i = 0; i < childNodes.getLength(); i++) { 275 Node childNode = childNodes.item(i); 276 if (childNode.getNodeName().equals("pointcut")) { 277 Element pointcutElement = (Element ) childNode; 278 PointcutInfo pointcutInfo = new PointcutInfo(); 279 pointcutInfo.name = pointcutElement.getAttribute("name"); 280 pointcutInfo.expression = pointcutElement.getAttribute("expression").trim(); 281 if (pointcutInfo.expression == null || pointcutInfo.expression.trim().length() == 0) { 283 pointcutInfo.expression = getText(pointcutElement).trim().replace('\n', ' '); 284 } 285 globalPointcuts.add(pointcutInfo); 286 } 287 } 288 289 return globalPointcuts; 290 } 291 292 298 private static void parseDeploymentScopeDefs(Element systemElement, SystemDefinition definition) { 299 NodeList childNodes = systemElement.getChildNodes(); 300 for (int i = 0; i < childNodes.getLength(); i++) { 301 Node childNode = childNodes.item(i); 302 if (childNode.getNodeName().equals("deployment-scope")) { 303 Element deploymentScopeElement = (Element ) childNode; 304 String name = deploymentScopeElement.getAttribute("name"); 305 String expression = deploymentScopeElement.getAttribute("expression"); 306 if (expression == null || expression.trim().length() == 0) { 308 expression = getText(deploymentScopeElement).trim(); 309 } 310 DefinitionParserHelper.createAndAddDeploymentScopeDef(name, expression, definition); 311 } 312 } 313 } 314 315 321 private static void parseAdvisableDefs(Element systemElement, SystemDefinition definition) { 322 NodeList childNodes = systemElement.getChildNodes(); 323 for (int i = 0; i < childNodes.getLength(); i++) { 324 Node childNode = childNodes.item(i); 325 if (childNode.getNodeName().equals("advisable")) { 326 Element advisableElement = (Element ) childNode; 327 String pointcutTypes = advisableElement.getAttribute("pointcut-type"); 328 if (pointcutTypes == null || pointcutTypes.trim().length() == 0) { 329 pointcutTypes = "all"; 330 } 331 String expression = advisableElement.getAttribute("expression"); 332 if (expression == null || expression.trim().length() == 0) { 334 expression = getText(advisableElement).trim(); 335 } 336 handleAdvisableDefinition(definition, expression, pointcutTypes); 337 } 338 } 339 } 340 341 350 private static void parsePackageElements(ClassLoader loader, Element systemElement, SystemDefinition definition, 351 String basePackage, List globalPointcuts) { 352 NodeList childNodes = systemElement.getChildNodes(); 353 for (int i = 0; i < childNodes.getLength(); i++) { 354 Node childNode = childNodes.item(i); 355 if (childNode.getNodeName().equals("package")) { 356 Element packageElement = (Element ) childNode; 357 String packageName = basePackage + getPackage(packageElement); 358 parseAspectElements(loader, packageElement, definition, packageName, globalPointcuts); 359 parseMixinElements(loader, packageElement, definition, packageName); 360 parseAdvisableDefs(packageElement, definition); 361 } 362 } 363 } 364 365 374 private static void parseAspectElements(ClassLoader loader, 375 Element systemElement, 376 SystemDefinition definition, 377 String packageName, 378 List globalPointcuts) { 379 NodeList childNodes = systemElement.getChildNodes(); 380 for (int i = 0; i < childNodes.getLength(); i++) { 381 Node childNode = childNodes.item(i); 382 if (childNode.getNodeName().equals("aspect")) { 383 Element aspectElement = (Element ) childNode; 384 385 String aspectName = aspectElement.getAttribute("name"); 386 String className = aspectElement.getAttribute("class"); 387 String deploymentModel = aspectElement.getAttribute("deployment-model"); 388 String containerClassName = aspectElement.getAttribute("container"); 389 390 if (Strings.isNullOrEmpty(className)) { 392 System.err.println("Warning: could not load aspect without 'class=..' attribute"); 393 new Exception ().printStackTrace(); 394 continue; 395 } 396 397 String aspectClassName = packageName + className; 398 if (aspectName == null || aspectName.trim().length() == 0) { 399 aspectName = aspectClassName; 400 } 401 402 ClassInfo aspectClassInfo; 404 try { 405 aspectClassInfo = AsmClassInfo.getClassInfo(aspectClassName, loader); 406 } catch (Exception e) { 407 System.err.println("Warning: could not load aspect " + aspectClassName + " from " + loader + "; " 408 + e.toString()); 409 e.printStackTrace(); 410 continue; 411 } 412 413 AspectDefinition aspectDef = new AspectDefinition(aspectName, aspectClassInfo, definition); 414 415 for (Iterator it = globalPointcuts.iterator(); it.hasNext();) { 417 PointcutInfo pointcutInfo = (PointcutInfo) it.next(); 418 DefinitionParserHelper.createAndAddPointcutDefToAspectDef( 419 pointcutInfo.name, 420 pointcutInfo.expression, 421 aspectDef 422 ); 423 } 424 parsePointcutElements(aspectElement, aspectDef); 426 AspectModelManager.defineAspect(aspectClassInfo, aspectDef, loader); 428 429 AspectAnnotationParser.parse(aspectClassInfo, aspectDef, loader); 431 432 if (!Strings.isNullOrEmpty(deploymentModel)) { 434 aspectDef.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModel)); 435 } 436 if (!Strings.isNullOrEmpty(aspectName)) { 437 aspectDef.setName(aspectName); 438 } 439 if (!Strings.isNullOrEmpty(containerClassName)) { 440 aspectDef.setContainerClassName(containerClassName); 441 } 442 443 parseParameterElements(aspectElement, aspectDef); 445 parsePointcutElements(aspectElement, aspectDef); parseAdviceElements(aspectElement, aspectDef, aspectClassInfo); 447 parseIntroduceElements(aspectElement, aspectDef, packageName, loader); 448 449 definition.addAspect(aspectDef); 450 } 451 } 452 } 453 454 462 private static void parseMixinElements(ClassLoader loader, 463 Element systemElement, 464 SystemDefinition systemDefinition, 465 String packageName) { 466 NodeList childNodes = systemElement.getChildNodes(); 467 for (int i = 0; i < childNodes.getLength(); i++) { 468 Node childNode = childNodes.item(i); 469 if (childNode.getNodeName().equals("mixin")) { 470 Element mixinElement = (Element ) childNode; 471 472 String className = mixinElement.getAttribute("class"); 473 String deploymentModelAsString = mixinElement.getAttribute("deployment-model"); 474 boolean isTransient = false; 475 boolean isTransientSetInXML = false; 476 String transientValue = mixinElement.getAttribute("transient"); 477 if(transientValue!=null) { 478 isTransient = transientValue.equalsIgnoreCase("true"); 479 isTransientSetInXML = true; 480 } 481 482 String factoryClassName = mixinElement.getAttribute("factory"); 483 String expression = mixinElement.getAttribute("bind-to"); 484 485 String mixinClassName = packageName + className; 486 487 ClassInfo mixinClassInfo; 489 try { 490 mixinClassInfo = AsmClassInfo.getClassInfo(mixinClassName, loader); 491 } catch (Exception e) { 492 System.err.println("Warning: could not load mixin " + mixinClassName + " from " + loader + "; " 493 + e.toString()); 494 e.printStackTrace(); 495 continue; 496 } 497 498 DeploymentModel deploymentModel = deploymentModelAsString == null 499 || deploymentModelAsString.trim().length() == 0 ? DeploymentModel.PER_INSTANCE 500 : DeploymentModel.getDeploymentModelFor(deploymentModelAsString); 501 502 MixinDefinition mixinDefinition = DefinitionParserHelper.createAndAddMixinDefToSystemDef(mixinClassInfo, 503 expression, 504 deploymentModel, 505 isTransient, 506 systemDefinition); 507 508 MixinAnnotationParser.parse(mixinClassInfo, mixinDefinition); 510 511 if (!Strings.isNullOrEmpty(deploymentModelAsString)) { 513 mixinDefinition.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModelAsString)); 514 } 515 if (!Strings.isNullOrEmpty(factoryClassName)) { 516 mixinDefinition.setFactoryClassName(factoryClassName); 517 } 518 if (isTransientSetInXML) { 519 mixinDefinition.setTransient(isTransient); 520 } 521 522 parseParameterElements(mixinElement, mixinDefinition); 523 } 524 } 525 } 526 527 532 public static void addVirtualAspect(SystemDefinition definition) { 533 Class clazz = Virtual.class; 534 String aspectName = clazz.getName(); 535 ClassInfo aspectClassInfo = JavaClassInfo.getClassInfo(clazz); 536 AspectDefinition aspectDef = new AspectDefinition(aspectName, aspectClassInfo, definition); 537 try { 538 MethodInfo methodInfo = JavaMethodInfo.getMethodInfo(clazz.getDeclaredMethod("virtual", new Class []{})); 539 aspectDef.addBeforeAdviceDefinition( 540 new AdviceDefinition( 541 methodInfo.getName(), 542 AdviceType.BEFORE, 543 null, 544 aspectName, 545 aspectName, 546 null, 547 methodInfo, 548 aspectDef 549 ) 550 ); 551 } catch (NoSuchMethodException e) { 552 throw new Error ("virtual aspect [" + aspectName + "] does not have expected method: " + e.toString()); 553 } 554 definition.addAspect(aspectDef); 555 } 556 557 563 private static void parseParameterElements(Element aspectElement, AspectDefinition aspectDef) { 564 NodeList childNodes = aspectElement.getChildNodes(); 565 for (int i = 0; i < childNodes.getLength(); i++) { 566 Node childNode = childNodes.item(i); 567 if (childNode.getNodeName().equals("param")) { 568 Element paramElement = (Element ) childNode; 569 aspectDef.addParameter(paramElement.getAttribute("name"), paramElement.getAttribute("value")); 570 } 571 } 572 } 573 574 580 private static void parseParameterElements(Element mixinElement, MixinDefinition mixinDef) { 581 NodeList childNodes = mixinElement.getChildNodes(); 582 for (int i = 0; i < childNodes.getLength(); i++) { 583 Node childNode = childNodes.item(i); 584 if (childNode.getNodeName().equals("param")) { 585 Element paramElement = (Element ) childNode; 586 mixinDef.addParameter(paramElement.getAttribute("name"), paramElement.getAttribute("value")); 587 } 588 } 589 } 590 591 597 private static void parsePointcutElements(Element aspectElement, AspectDefinition aspectDef) { 598 NodeList aspectNodes = aspectElement.getChildNodes(); 599 for (int i = 0; i < aspectNodes.getLength(); i++) { 600 Node childNode = aspectNodes.item(i); 601 if(childNode.getNodeType()!=Node.ELEMENT_NODE) { 602 continue; 603 } 604 Element childElement = (Element ) childNode; 605 if (childElement.getNodeName().equals("pointcut")) { 606 String name = childElement.getAttribute("name"); 607 String expression = childElement.getAttribute("expression"); 608 if (expression == null || expression.trim().length() == 0) { 610 expression = getText(childElement).trim(); 611 } 612 DefinitionParserHelper.createAndAddPointcutDefToAspectDef(name, expression, aspectDef); 613 } else if (childElement.getNodeName().equals("deployment-scope")) { 614 String name = childElement.getAttribute("name"); 615 String expression = childElement.getAttribute("expression"); 616 if (expression == null) { 618 expression = getText(childElement).trim(); 619 } 620 DefinitionParserHelper.createAndAddDeploymentScopeDef( 621 name, expression, aspectDef.getSystemDefinition() 622 ); 623 } else if (childElement.getNodeName().equals("advisable")) { 624 String expression = childElement.getAttribute("expression"); 625 String pointcutTypes = childElement.getAttribute("pointcut-type"); 626 if (expression == null || expression.trim().length() == 0) { 627 expression = getText(childElement).trim(); 628 } 629 handleAdvisableDefinition(aspectDef.getSystemDefinition(), expression, pointcutTypes); 630 } 631 } 632 } 633 634 public static String getText(Element element) { 635 NodeList childNodes = element.getChildNodes(); 636 for (int i = 0; i < childNodes.getLength(); i++) { 637 Node childNode = childNodes.item(i); 638 if(childNode.getNodeType()==Node.TEXT_NODE) { 639 return ((Text ) childNode).getData(); 640 } 641 } 642 return ""; 643 } 644 645 652 private static void parseAdviceElements(Element aspectElement, AspectDefinition aspectDef, ClassInfo aspectClassInfo) { 653 NodeList childNodes = aspectElement.getChildNodes(); 654 for (int i = 0; i < childNodes.getLength(); i++) { 655 Node childNode = childNodes.item(i); 656 if (childNode.getNodeName().equals("advice")) { 657 Element adviceElement = (Element ) childNode; 658 659 String name = adviceElement.getAttribute("name"); 660 String type = adviceElement.getAttribute("type"); 661 String bindTo = adviceElement.getAttribute("bind-to"); 662 663 MethodInfo method = DefinitionParserHelper.createMethodInfoForAdviceFQN(name, aspectDef, aspectClassInfo); 664 DefinitionParserHelper.createAndAddAdviceDefsToAspectDef(type, bindTo, name, method, aspectDef); 665 666 NodeList bindNodes = adviceElement.getChildNodes(); 667 for (int j = 0; j < bindNodes.getLength(); j++) { 668 Node bindToNode = bindNodes.item(j); 669 if (bindToNode.getNodeName().equals("bind-to")) { 670 Element bindToElement = (Element ) bindToNode; 671 String pointcut = bindToElement.getAttribute("pointcut"); 672 DefinitionParserHelper.createAndAddAdviceDefsToAspectDef(type, pointcut, name, method, aspectDef); 673 } 674 } 675 } 676 } 677 } 678 679 687 private static void parseIntroduceElements(Element aspectElement, 688 AspectDefinition aspectDef, 689 String packageName, 690 ClassLoader loader) { 691 NodeList childNodes = aspectElement.getChildNodes(); 692 for (int i = 0; i < childNodes.getLength(); i++) { 693 Node childNode = childNodes.item(i); 694 if (childNode.getNodeName().equals("introduce")) { 695 Element introduceElement = (Element ) childNode; 696 697 String klass = introduceElement.getAttribute("class"); 698 String name = introduceElement.getAttribute("name"); 699 String bindTo = introduceElement.getAttribute("bind-to"); 700 701 String fullClassName = packageName + klass; 703 if (name == null || name.length() == 0) { 704 name = fullClassName; 705 } 706 707 ClassInfo introductionClassInfo; 709 try { 710 introductionClassInfo = AsmClassInfo.getClassInfo(fullClassName, loader); 711 } catch (Exception e) { 712 throw new DefinitionException("could not find interface introduction: " + packageName + klass + "; " 713 + e.getMessage()); 714 } 715 716 if (introductionClassInfo.isInterface()) { 718 DefinitionParserHelper 719 .createAndAddInterfaceIntroductionDefToAspectDef(bindTo, name, fullClassName, aspectDef); 720 721 NodeList bindToNodes = introduceElement.getChildNodes(); 723 for (int j = 0; j < bindToNodes.getLength(); j++) { 724 Node bindToNode = bindToNodes.item(j); 725 if (bindToNode.getNodeName().equals("bindTo")) { 726 Element bindToElement = (Element ) bindToNode; 727 String pointcut = bindToElement.getAttribute("pointcut"); 728 DefinitionParserHelper.createAndAddInterfaceIntroductionDefToAspectDef(pointcut, name, fullClassName, 729 aspectDef); 730 } 731 } 732 } 733 } 734 } 735 } 736 737 743 private static String getPackage(Element packageElement) { 744 String packageName = packageElement.getAttribute("name"); 745 if (packageName != null) { 746 if (packageName.endsWith(".*")) { 747 return packageName.substring(0, packageName.length() - 1); 748 } else if (!packageName.endsWith(".")) { 749 return packageName + "."; 750 } 751 } 752 return ""; 753 } 754 755 762 private static void parseIncludePackageElements(Element root, 763 SystemDefinition definition, 764 String packageName) { 765 NodeList childNodes = root.getChildNodes(); 766 for (int i = 0; i < childNodes.getLength(); i++) { 767 Node includeNode = childNodes.item(i); 768 if (includeNode.getNodeName().equals("include")) { 769 Element includeElement = (Element ) includeNode; 770 String packageValue = includeElement.getAttribute("package"); 771 772 String includePackage = ""; 773 if (packageName.endsWith(".*")) { 775 includePackage = packageName.substring(0, packageName.length() - 2); 776 } else if (packageName.endsWith(".")) { 777 includePackage = packageName.substring(0, packageName.length() - 1); 778 } 779 780 includePackage = packageName + packageValue.trim(); 782 if (includePackage.endsWith(".*")) { 783 includePackage = includePackage.substring(0, includePackage.length() - 2); 784 } else if (includePackage.endsWith(".")) { 785 includePackage = includePackage.substring(0, includePackage.length() - 1); 786 } 787 if (includePackage.length() != 0) { 788 definition.addIncludePackage(includePackage); 789 } 790 } 791 } 792 } 793 794 801 private static void parseExcludePackageElements(Element root, SystemDefinition definition, String packageName) { 802 NodeList childNodes = root.getChildNodes(); 803 for (int i = 0; i < childNodes.getLength(); i++) { 804 Node childNode = childNodes.item(i); 805 if (childNode.getNodeName().equals("exclude")) { 806 Element excludeElement = (Element ) childNode; 807 808 String excludeValue = excludeElement.getAttribute("package"); 809 810 String excludePackage = ""; 811 if (packageName.endsWith(".*")) { 813 excludePackage = packageName.substring(0, packageName.length() - 2); 814 } else if (packageName.endsWith(".")) { 815 excludePackage = packageName.substring(0, packageName.length() - 1); 816 } 817 818 excludePackage = packageName + excludeValue.trim(); 820 if (excludePackage.endsWith(".*")) { 821 excludePackage = excludePackage.substring(0, excludePackage.length() - 2); 822 } else if (excludePackage.endsWith(".")) { 823 excludePackage = excludePackage.substring(0, excludePackage.length() - 1); 824 } 825 if (excludePackage.length() != 0) { 826 definition.addExcludePackage(excludePackage); 827 } 828 } 829 } 830 } 831 832 839 private static void parsePrepareElements(Element root, 840 SystemDefinition definition, 841 String packageName) { 842 NodeList childNodes = root.getChildNodes(); 843 for (int i = 0; i < childNodes.getLength(); i++) { 844 Node childNode = childNodes.item(i); 845 if (childNode.getNodeName().equals("prepare")) { 846 Element prepareElement = (Element ) childNode; 847 String packageValue = prepareElement.getAttribute("package"); 848 849 String preparePackage = ""; 850 851 if (packageName.endsWith(".*")) { 853 preparePackage = packageName.substring(0, packageName.length() - 2); 854 } else if (packageName.endsWith(".")) { 855 preparePackage = packageName.substring(0, packageName.length() - 1); 856 } 857 858 preparePackage = packageName + packageValue.trim(); 860 if (preparePackage.endsWith(".*")) { 861 preparePackage = preparePackage.substring(0, preparePackage.length() - 2); 862 } else if (preparePackage.endsWith(".")) { 863 preparePackage = preparePackage.substring(0, preparePackage.length() - 1); 864 } 865 if (preparePackage.length() != 0) { 866 definition.addPreparePackage(preparePackage); 867 } 868 } 869 } 870 } 871 872 878 private static String getBasePackage(Element system) { 879 String basePackage = ""; 880 NamedNodeMap attrs = system.getAttributes(); 881 for (int i = 0; i < attrs.getLength(); i++) { 882 Node item = attrs.item(i); 883 if (item.getNodeName().equalsIgnoreCase("base-package")) { 884 basePackage = ((Attr ) item).getValue().trim(); 885 if (basePackage.endsWith(".*")) { 886 basePackage = basePackage.substring(0, basePackage.length() - 1); 887 } else if (!basePackage.endsWith(".")) { 888 basePackage += "."; 889 } 890 break; 891 } 892 } 893 894 912 return basePackage; 913 } 914 915 918 private static class PointcutInfo { 919 public String name; 920 public String expression; 921 } 922 923 930 private static void handleAdvisableDefinition(SystemDefinition definition, 931 String withinPointcut, 932 String pointcutTypes) { 933 definition.addMixinDefinition( 935 DefinitionParserHelper.createAndAddMixinDefToSystemDef( 936 AdvisableImpl.CLASS_INFO, 937 withinPointcut, 938 DeploymentModel.PER_INSTANCE, 939 false, definition 941 ) 942 ); 943 944 boolean hasAllPointcuts = false; 945 boolean hasExecutionPointcut = false; 946 boolean hasCallPointcut = false; 947 boolean hasSetPointcut = false; 948 boolean hasGetPointcut = false; 949 boolean hasHandlerPointcut = false; 950 if (pointcutTypes == null || 951 pointcutTypes.equals("") || 952 pointcutTypes.equalsIgnoreCase("all")) { 953 hasAllPointcuts = true; 954 } else { 955 StringTokenizer tokenizer = new StringTokenizer (pointcutTypes, "|"); 956 while (tokenizer.hasMoreTokens()) { 957 String token = tokenizer.nextToken(); 958 if (token.trim().equalsIgnoreCase("all")) { 959 hasAllPointcuts = true; 960 break; 961 } else if (token.trim().equalsIgnoreCase("execution")) { 962 hasExecutionPointcut = true; 963 } else if (token.trim().equalsIgnoreCase("call")) { 964 hasCallPointcut = true; 965 } else if (token.trim().equalsIgnoreCase("set")) { 966 hasSetPointcut = true; 967 } else if (token.trim().equalsIgnoreCase("getDefault")) { 968 hasGetPointcut = true; 969 } else if (token.trim().equalsIgnoreCase("handler")) { 970 hasHandlerPointcut = true; 971 } 972 } 973 } 974 if (hasAllPointcuts || hasExecutionPointcut) { 975 DefinitionParserHelper.createAndAddAdvisableDef( 976 "(execution(!static * *.*(..)) && " + withinPointcut + ')', 980 definition 981 ); 982 } 983 if (hasAllPointcuts || hasCallPointcut) { 984 DefinitionParserHelper.createAndAddAdvisableDef( 985 "(call(* *.*(..)) && withincode(!static * *.*(..)) && " + withinPointcut + ')', 989 definition 990 ); 991 } 992 if (hasAllPointcuts || hasSetPointcut) { 993 DefinitionParserHelper.createAndAddAdvisableDef( 994 "(set(* *.*) && withincode(!static * *.*(..)) && " + withinPointcut + ')', 997 definition 998 ); 999 } 1000 if (hasAllPointcuts || hasGetPointcut) { 1001 DefinitionParserHelper.createAndAddAdvisableDef( 1002 "(getDefault(* *.*) && withincode(!static * *.*(..)) && " + withinPointcut + ')', 1005 definition 1006 ); 1007 } 1008 if (hasAllPointcuts || hasHandlerPointcut) { 1009 DefinitionParserHelper.createAndAddAdvisableDef( 1010 "(handler(*..*) && withincode(!static * *.*(..)) && " + withinPointcut + ')', 1013 definition 1014 ); 1015 } 1016 } 1017} | Popular Tags |