1 5 package xdoclet.modules.ejb.intf; 6 7 import java.text.MessageFormat ; 8 import java.util.Arrays ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.Properties ; 12 13 import org.apache.commons.logging.Log; 14 15 import xjavadoc.*; 16 import xdoclet.DocletContext; 17 18 import xdoclet.DocletTask; 19 import xdoclet.XDocletException; 20 import xdoclet.modules.ejb.*; 21 import xdoclet.modules.ejb.entity.EntityTagsHandler; 22 import xdoclet.modules.ejb.home.HomeTagsHandler; 23 import xdoclet.tagshandler.MethodTagsHandler; 24 25 import xdoclet.util.LogUtil; 26 import xdoclet.util.Translator; 27 import xdoclet.util.TypeConversionUtil; 28 29 36 public class InterfaceTagsHandler extends EjbTagsHandler 37 { 38 public final static String SERVICE_ENDPOINT_INTERFACE = "ServiceEndpoint"; 40 public final static String SERVICE_ENDPOINT_EXTENDS_PARAM = "service-endpoint-extends"; 41 42 private String currentMethodViewType = null; 43 private String currentTagIntf = null; 44 45 72 public static String getComponentInterface(String type, XClass clazz) throws XDocletException 73 { 74 if (!"remote".equals(type) && !"local".equals(type) && !SERVICE_ENDPOINT.equals(type)) { 76 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.INTERFACE_INVALID_TYPE, new String []{type})); 77 } 78 79 String fileName = clazz.getContainingPackage().getName(); 80 String name_pattern = null; 81 String package_pattern = null; 82 String component_interface = null; 83 84 component_interface = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-class"); 85 if (component_interface != null) { 86 return component_interface; 87 } 88 89 name_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-pattern"); 90 if (name_pattern == null) { 91 name_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", "pattern"); 92 if (name_pattern == null) { 93 if ("remote".equals(type)) { 94 name_pattern = getRemoteClassPattern(); 95 } 96 else { 97 if (SERVICE_ENDPOINT.equals(type)) { 98 name_pattern = getServiceEndpointClassPattern(); 99 } 100 else { 101 name_pattern = getLocalClassPattern(); 102 } 103 } 104 } 105 } 106 107 package_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-package"); 108 if (package_pattern == null) { 109 package_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", "package"); 110 } 111 112 String ejb_name = null; 113 114 if (name_pattern.indexOf("{0}") != -1) { 115 ejb_name = MessageFormat.format(name_pattern, new Object []{getShortEjbNameFor(clazz)}); 116 } 117 else { 118 ejb_name = name_pattern; 119 } 120 121 String subtask_name = null; 122 123 if (type.equals("remote")) { 124 subtask_name = DocletTask.getSubTaskName(RemoteInterfaceSubTask.class); 125 } 126 else if (SERVICE_ENDPOINT.equals(type)) { 127 subtask_name = DocletTask.getSubTaskName(ServiceEndpointSubTask.class); 128 } 129 else { 130 subtask_name = DocletTask.getSubTaskName(LocalInterfaceSubTask.class); 131 } 132 133 StringBuffer sb = new StringBuffer (choosePackage(fileName, package_pattern, subtask_name)); 135 136 if (sb.length() > 0) { 137 sb.append('.'); 138 } 139 sb.append(ejb_name); 140 141 return sb.toString(); 142 } 143 144 151 public static boolean isInterfaceMethod(XMethod method) 152 { 153 boolean result = isComponentInterfaceMethod(method) || 154 HomeTagsHandler.isCreateMethod(method) || 155 HomeTagsHandler.isRemoveMethod(method) || 156 HomeTagsHandler.isFinderMethod(method) || 157 HomeTagsHandler.isHomeMethod(method); 158 159 return result; 160 } 161 162 169 public static boolean isComponentInterfaceMethod(XMethod method) 170 { 171 return method.getDoc().hasTag("ejb:interface-method"); 172 } 173 174 181 public static String getBeanClassNameFromInterfaceNameFor(String return_type) throws XDocletException 182 { 183 Log log = LogUtil.getLog(InterfaceTagsHandler.class, "getBeanClassNameFromInterfaceName"); 184 185 Collection classes = getXJavaDoc().getSourceClasses(); 186 187 if (log.isDebugEnabled()) { 188 log.debug("return_type=" + return_type); 189 } 190 if (return_type == null) { 191 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.ASK_FOR_BEAN_FROM_NULL_INTERFACE, new String []{return_type})); 192 } 193 194 for (Iterator i = classes.iterator(); i.hasNext(); ) { 195 XClass clazz = (XClass) i.next(); 196 197 if (log.isDebugEnabled()) { 198 log.debug("clazz=" + clazz); 199 } 200 201 if (EntityTagsHandler.isEntity(clazz)) { 202 String remote_intf_name = getComponentInterface("remote", clazz); 203 String local_intf_name = getComponentInterface("local", clazz); 204 205 if (log.isDebugEnabled()) { 206 log.debug("remote_intf_name=" + remote_intf_name); 207 log.debug("local_intf_name=" + local_intf_name); 208 } 209 210 if (return_type.equals(remote_intf_name) || return_type.equals(local_intf_name)) { 211 if (log.isDebugEnabled()) { 212 log.debug("Found! beanClassNameFromInterfaceName returns with: " + clazz.getQualifiedName()); 213 } 214 215 return clazz.getQualifiedName(); 216 } 217 } 218 } 219 if (log.isDebugEnabled()) { 220 log.warn("NOT FOUND! bean class coreesponding to IF " + return_type); 221 } 222 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.BEAN_CLASS_NOT_FOUND_FOR_INTERFACE, new String []{return_type})); 223 } 224 225 232 public static boolean isRemoteMethod(XMethod method) throws XDocletException 233 { 234 return isComponentInterfaceMethodOfViewType(method, "remote"); 235 } 236 237 244 public static boolean isLocalMethod(XMethod method) throws XDocletException 245 { 246 return isComponentInterfaceMethodOfViewType(method, "local"); 247 } 248 249 256 public static boolean isServiceEndpointMethod(XMethod method) throws XDocletException 257 { 258 return isComponentInterfaceMethodOfViewType(method, SERVICE_ENDPOINT); 259 } 260 261 266 protected static String getRemoteClassPattern() 267 { 268 RemoteInterfaceSubTask remoteintf_subtask = ((RemoteInterfaceSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(RemoteInterfaceSubTask.class))); 269 270 if (remoteintf_subtask != null) { 271 return remoteintf_subtask.getRemoteClassPattern(); 272 } 273 else { 274 return RemoteInterfaceSubTask.DEFAULT_REMOTE_CLASS_PATTERN; 275 } 276 } 277 278 283 protected static String getLocalClassPattern() 284 { 285 LocalInterfaceSubTask localintf_subtask = ((LocalInterfaceSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(LocalInterfaceSubTask.class))); 286 287 if (localintf_subtask != null) { 288 return localintf_subtask.getLocalClassPattern(); 289 } 290 else { 291 return LocalInterfaceSubTask.DEFAULT_LOCAL_CLASS_PATTERN; 292 } 293 } 294 295 300 protected static String getServiceEndpointClassPattern() 301 { 302 ServiceEndpointSubTask seintf_subtask = ((ServiceEndpointSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ServiceEndpointSubTask.class))); 303 304 if (seintf_subtask != null) { 305 return seintf_subtask.getServiceEndpointClassPattern(); 306 } 307 else { 308 return ServiceEndpointSubTask.DEFAULT_SERVICE_ENDPOINT_CLASS_PATTERN; 309 } 310 } 311 312 320 private static boolean isComponentInterfaceMethodOfViewType(XMethod method, String type) throws XDocletException 321 { 322 if (isComponentInterfaceMethod(method) == false) { 324 return false; 327 } 328 else { 329 String view_type = getTagValue( 330 FOR_CLASS, 331 method.getDoc(), 332 "ejb:interface-method", 333 "view-type", 334 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 335 "both", 336 false, 337 false 338 ); 339 340 if (view_type.indexOf(type) != -1) { 341 return true; 342 } 343 else { 344 if (view_type.indexOf(ALL) != -1) { 345 return true; 346 } 347 else if (!view_type.equals(SERVICE_ENDPOINT) && view_type.indexOf("both") != -1) { 348 return true; 349 } 350 else { 351 return false; 352 } 353 } 354 } 355 } 356 357 363 private static String [] getViewTypesFromString(String viewType) 364 { 365 if (viewType != null) { 366 if (viewType.equalsIgnoreCase(ALL)) { 367 return new String []{"local", "remote", SERVICE_ENDPOINT}; 368 } 369 370 if (viewType.equalsIgnoreCase("both")) { 371 return new String []{"local", "remote"}; 372 } 373 374 if (viewType.equalsIgnoreCase("remote")) { 375 return new String []{"remote"}; 376 } 377 378 if (viewType.equalsIgnoreCase("remote-" + SERVICE_ENDPOINT)) { 379 return new String []{"remote", SERVICE_ENDPOINT}; 380 } 381 382 if (viewType.equalsIgnoreCase("local")) { 383 return new String []{"local"}; 384 } 385 386 if (viewType.equalsIgnoreCase("local-" + SERVICE_ENDPOINT)) { 387 return new String []{"local", SERVICE_ENDPOINT}; 388 } 389 390 if (viewType.equalsIgnoreCase(SERVICE_ENDPOINT)) { 391 return new String []{SERVICE_ENDPOINT}; 392 } 393 } 394 395 if (EjbTagsHandler.getEjbSpec().equals("1.1")) 397 return new String []{"remote"}; 398 else 399 return new String []{"local", "remote"}; 400 } 401 402 412 public String componentInterface(Properties attributes) throws XDocletException 413 { 414 String type = attributes.getProperty("type"); 415 416 type = type != null ? type : "remote"; 417 418 return getComponentInterface(type, getCurrentClass()); 419 } 420 421 431 public void ifIsNotInterfaceMethod(String template, Properties attributes) throws XDocletException 432 { 433 String intFace = attributes.getProperty("interface"); 434 435 if (intFace == null) { 436 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.TAG_MISSING_INTERFACE_PARAMETER, new String []{"<XDtEjbIntf:ifIsNotInterfaceMethod>"})); 437 } 438 439 if (!isInterfaceMethod(intFace)) { 440 generate(template); 441 } 442 } 443 444 456 public void ifIsInterfaceMethod(String template, Properties attributes) throws XDocletException 457 { 458 String intf_view_type = attributes.getProperty("interface"); 459 String superclasses_str = attributes.getProperty("superclasses"); 460 boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true); 461 462 if (intf_view_type == null) { 463 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.TAG_MISSING_INTERFACE_PARAMETER, new String []{"<XDtEjbIntf:ifIsInterfaceMethod>"})); 464 } 465 466 if (isInterfaceMethod(intf_view_type)) { 467 boolean currentMethodDoesntBelongsToCurrentClass = !getCurrentMethod().getContainingClass().equals(getCurrentClass()); 468 469 if (superclasses == false && currentMethodDoesntBelongsToCurrentClass == true) { 470 return; 471 } 472 473 generate(template); 474 } 475 } 476 477 486 public void forAllInterfaceViewTypes(String template, Properties attributes) throws XDocletException 487 { 488 String view_type; 489 490 if (isComponentInterfaceMethod(getCurrentMethod())) { 491 view_type = getTagValue( 493 FOR_METHOD, 494 getCurrentMethod().getDoc(), 495 "ejb.interface-method", 496 "view-type", 497 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 498 null, 499 true, 500 false 501 ); 502 } 503 else { 504 view_type = getTagValue( 506 FOR_METHOD, 507 getCurrentMethod().getDoc(), 508 "ejb.home-method", 509 "view-type", 510 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 511 null, 512 true, 513 false 514 ); 515 516 if (view_type == null) { 517 view_type = getTagValue( 518 FOR_METHOD, 519 getCurrentMethod().getDoc(), 520 "ejb.create-method", 521 "view-type", 522 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 523 null, 524 true, 525 false 526 ); 527 } 528 } 529 530 if (view_type == null) { 532 view_type = getTagValue( 533 FOR_CLASS, 534 getCurrentClass().getDoc(), 535 "ejb.bean", 536 "view-type", 537 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 538 null, 539 true, 540 false 541 ); 542 } 543 544 String [] view_types = getViewTypesFromString(view_type); 545 546 for (int i = 0; i < view_types.length; i++) { 547 currentMethodViewType = view_types[i]; 548 549 generate(template); 550 } 551 552 currentMethodViewType = null; 553 } 554 555 563 public void ifIsInterfaceMethod(String template) throws XDocletException 564 { 565 if (isInterfaceMethod(getCurrentMethod())) { 566 generate(template); 567 } 568 } 569 570 579 public String methodIntf(Properties attributes) throws XDocletException 580 { 581 String view_type = attributes.getProperty("interface"); 582 583 if (EjbTagsHandler.getEjbSpec().equals("1.1")) 584 view_type = "remote"; 585 586 if (view_type == null) { 587 if (currentMethodViewType != null) { 589 view_type = currentMethodViewType; 590 } 591 else { 592 if (EjbTagsHandler.isOnlyLocalEjb(getCurrentClass())) { 593 view_type = "local"; 594 } 595 else if (EjbTagsHandler.isOnlyRemoteEjb(getCurrentClass())) { 596 view_type = "remote"; 597 } 598 else if (EjbTagsHandler.isOnlyServiceEndpointEjb(getCurrentClass())) { 599 view_type = SERVICE_ENDPOINT; 600 } 601 else if (EntityTagsHandler.isEntity(getCurrentClass())) { 602 view_type = "local"; 603 } 604 else { 605 view_type = "remote"; 606 } 607 } 608 } 609 610 if (view_type.equals("remote")) { 611 return (isRemoteMethod(getCurrentMethod()) ? "Remote" : "Home"); 612 } 613 else if (view_type.equals(SERVICE_ENDPOINT)) { 614 return SERVICE_ENDPOINT_INTERFACE; 615 } 616 else { 617 return (isLocalMethod(getCurrentMethod()) ? "Local" : "LocalHome"); 618 } 619 } 620 621 629 public String interfaceMethodName() throws XDocletException 630 { 631 return getInterfaceMethodName(getCurrentMethod().getName()); 632 } 633 634 643 public String beanClassNameFromInterfaceName(Properties attributes) throws XDocletException 644 { 645 String return_type = attributes.getProperty("interface"); 646 647 if (return_type == null) { 648 return_type = MethodTagsHandler.getMethodTypeFor(getCurrentMethod()); 649 } 650 651 String bean_class_name = getBeanClassNameFromInterfaceNameFor(return_type); 652 653 if (bean_class_name == null) { 654 throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.INTERFACE_IMPL_NOT_FOUND, new String []{return_type})); 655 } 656 657 return bean_class_name; 658 } 659 660 668 public String extendsFrom(Properties attributes) throws XDocletException 669 { 670 String type = attributes.getProperty("type"); 671 672 type = type != null ? type : "remote"; 673 674 String extends_param_name = ""; 675 676 if (type.equals("remote")) { 677 extends_param_name = "extends"; 678 } 679 else if (type.equals(SERVICE_ENDPOINT)) { 680 extends_param_name = SERVICE_ENDPOINT_EXTENDS_PARAM; 681 } 682 else { 683 extends_param_name = "local-extends"; 684 } 685 686 String def_base_class_name = ""; 687 688 if (type.equals("remote")) { 689 def_base_class_name = "javax.ejb.EJBObject"; 690 } 691 else if (type.equals(SERVICE_ENDPOINT)) { 692 def_base_class_name = "java.rmi.Remote"; 693 } 694 else { 695 def_base_class_name = "javax.ejb.EJBLocalObject"; 696 } 697 698 return extendsFromFor(getCurrentClass(), "ejb:interface", type, extends_param_name, def_base_class_name); 699 } 700 701 712 public void ifCurrentMethodViewTypeEquals(String template, Properties attributes) throws XDocletException 713 { 714 String param = attributes.getProperty("paramName"); 715 String value = getCurrentMethodTag().getAttributeValue(param); 716 717 if (value == null) { 718 generate(template); 719 } 720 else { 721 String [] viewTypes = getViewTypesFromString(value); 722 723 Arrays.sort(viewTypes); 724 if (Arrays.binarySearch(viewTypes, currentMethodViewType) >= 0) { 725 generate(template); 726 } 727 728 } 729 } 730 731 742 public void forAllClassTagIntf(String template, Properties attributes) throws XDocletException 743 { 744 String param = attributes.getProperty("paramName"); 745 String value = getCurrentClassTag().getAttributeValue(param); 746 747 if (value == null) { 748 currentTagIntf = null; 749 generate(template); 750 } 751 else { 752 String [] view_types = getViewTypesFromString(value); 753 754 for (int i = 0; i < view_types.length; i++) { 755 if (view_types[i].equals("remote")) { 756 currentTagIntf = "Remote"; 757 generate(template); 758 currentTagIntf = "Home"; 759 generate(template); 760 } 761 else if (view_types[i].equals("local")) { 762 currentTagIntf = "Local"; 763 generate(template); 764 currentTagIntf = "LocalHome"; 765 generate(template); 766 } 767 else if (view_types[i].equals(SERVICE_ENDPOINT)) { 768 currentTagIntf = SERVICE_ENDPOINT_INTERFACE; 769 generate(template); 770 } 771 } 772 currentTagIntf = null; 773 } 774 } 775 776 784 public String classTagIntf() throws XDocletException 785 { 786 return currentTagIntf; 787 } 788 789 797 public void ifHasClassTagIntf(String template) throws XDocletException 798 { 799 if (currentTagIntf != null) { 800 generate(template); 801 } 802 } 803 804 814 protected boolean isInterfaceMethod(String intFace) throws XDocletException 815 { 816 if (!getCurrentMethod().getDoc().hasTag("ejb:interface-method")) { 817 return false; 818 } 819 820 String viewType = getTagValue( 821 FOR_CLASS, 822 getCurrentMethod().getDoc(), 823 "ejb:interface-method", 824 "view-type", 825 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 826 null, 827 false, 828 false 829 ); 830 831 832 if (viewType == null) { 833 viewType = getTagValue( 834 FOR_CLASS, 835 getCurrentClass().getDoc(), 836 "ejb:bean", 837 "view-type", 838 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL, 839 "both", 840 true, 841 false 842 ); 843 } 844 845 if ("both".equals(viewType)) { 846 viewType = "local,remote"; 847 } 848 else if (ALL.equals(viewType)) { 849 viewType = "local,remote," + SERVICE_ENDPOINT; 850 } 851 852 return viewType.indexOf(intFace) >= 0; 853 } 854 855 863 protected String getInterfaceMethodName(String name) throws XDocletException 864 { 865 if (name.startsWith("ejbCreate")) { 866 return HomeTagsHandler.toCreateMethod(name); 867 } 868 else if (name.equals("ejbRemove")) { 869 return "remove"; 870 } 871 else if (name.startsWith("ejbFind")) { 872 return HomeTagsHandler.toFinderMethod(name); 873 } 874 else if (name.startsWith("ejbHome")) { 875 return HomeTagsHandler.toHomeMethod(name); 876 } 877 else { 878 return name; 879 } 880 } 881 882 890 protected String getDependentClassFor(XClass clazz, String type) throws XDocletException 891 { 892 if ((type.equals("local") && isLocalEjb(clazz)) || (type.equals("remote") && isRemoteEjb(clazz)) || type.equals(SERVICE_ENDPOINT) && isServiceEndpointEjb(clazz)) { 893 return getComponentInterface(type, clazz); 894 } 895 else { 896 return null; 897 } 898 } 899 900 908 protected String fromInterfaceToBean(String value) throws XDocletException 909 { 910 Collection classes = getXJavaDoc().getSourceClasses(); 911 912 for (Iterator i = classes.iterator(); i.hasNext(); ) { 913 XClass clazz = (XClass) i.next(); 914 915 if (isEjb(clazz)) { 916 String remote_interface_name = getComponentInterface("remote", clazz); 917 String local_interface_name = getComponentInterface("local", clazz); 918 String service_endpoint_interface_name = getComponentInterface(SERVICE_ENDPOINT, clazz); 919 920 if (value.equals(remote_interface_name) || value.equals(local_interface_name) || value.equals(service_endpoint_interface_name)) { 921 return clazz.getQualifiedName(); 922 } 923 } 924 } 925 926 return value; 927 } 928 } 929 | Popular Tags |