1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 33 34 import java.util.*; 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.lang.reflect.Method ; 38 import java.lang.reflect.Modifier ; 39 import java.text.MessageFormat ; 40 41 import com.sun.jdo.api.persistence.model.Model; 42 import com.sun.jdo.api.persistence.model.jdo.*; 43 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement; 44 import com.sun.jdo.api.persistence.model.mapping.MappingFieldElement; 45 46 import com.sun.jdo.spi.persistence.support.ejb.model.DeploymentDescriptorModel; 47 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper; 48 import com.sun.jdo.spi.persistence.support.ejb.ejbqlc.JDOQLElements; 49 50 import com.sun.jdo.spi.persistence.utility.I18NHelper; 51 import com.sun.jdo.spi.persistence.utility.StringHelper; 52 import com.sun.jdo.spi.persistence.utility.generator.*; 53 import com.sun.jdo.spi.persistence.utility.generator.io.*; 54 import com.sun.jdo.spi.persistence.utility.logging.Logger; 55 56 62 abstract class JDOConcreteBeanGenerator { 63 64 static final Logger logger = LogHelperEJBCompiler.getLogger(); 65 66 NameMapper nameMapper = null; 68 69 Model model = null; 71 String beanName = null; 72 String helperName = null; 73 String concreteImplName = null; 74 String abstractBean = null; 75 String pkClass = null; 76 String pcname = null; 77 boolean hasLocalInterface; 78 boolean hasRemoteInterface; 79 boolean isUpdateable; 80 81 String setPKField = null; 82 83 String [] pcnameParam = new String [1]; 85 String [] pkClassParam = new String [1]; 86 87 static String [] objectType = new String []{CMPTemplateFormatter.Object_}; 88 static String [] param0 = new String []{CMPTemplateFormatter.param0_}; 89 static String [] param0PM = new String [] {CMPTemplateFormatter.param0_, 90 CMPTemplateFormatter.jdoPersistenceManager_}; 91 92 String [] oneParam = new String [1]; 94 String [] twoParams = new String [2]; 95 String [] threeParams = new String [3]; 96 String [] fourParams = new String [4]; 97 String [] fiveParams = new String [5]; 98 String [] sixParams = new String [6]; 99 100 String [] queryParams = new String [10]; 101 102 StringBuffer loadNonDFGBody = null; 104 105 ClassLoader loader; 106 107 JavaClassWriter concreteImplWriter; 109 110 JavaClassWriter jdoHelperWriter; 112 113 116 final static ResourceBundle messages = I18NHelper.loadBundle( 117 JDOConcreteBeanGenerator.class); 118 119 122 String inputFilesSignature; 123 124 127 String generatorClassesSignature; 128 129 132 static final String SIGNATURE = "$RCSfile: JDOConcreteBeanGenerator.java,v $ $Revision: 1.2 $"; 134 JDOConcreteBeanGenerator(ClassLoader loader, 135 Model model, 136 NameMapper nameMapper) 137 throws IOException { 138 this.loader = loader; 139 this.model = model; 140 this.nameMapper = nameMapper; 141 142 CMPTemplateFormatter.initHelpers(); 143 CMPROTemplateFormatter.initHelpers(); 144 } 145 146 void setUpdateable(boolean updateable) { 147 isUpdateable = updateable; 148 } 149 150 160 Collection validate(AbstractMethodHelper methodHelper, String beanName) { 161 return new ArrayList(); 162 } 163 164 173 Collection generate(AbstractMethodHelper methodHelper, String beanName, 174 File srcout, File classout) 175 throws IOException { 176 177 Collection files = new ArrayList(); 178 179 this.beanName = beanName; 180 this.abstractBean = nameMapper.getAbstractBeanClassForEjbName(beanName); 181 182 String pkgName = CMPTemplateFormatter.getPackageName(abstractBean); 183 concreteImplName = nameMapper.getConcreteBeanClassForEjbName(beanName); 184 String shortCmpName = CMPTemplateFormatter.getShortClassName(concreteImplName); 185 186 pcname = nameMapper.getPersistenceClassForEjbName(beanName); 187 pcnameParam[0] = pcname; 188 189 PersistenceClassElement pcClassElement = 190 model.getPersistenceClass(pcname); 191 192 pkClass = nameMapper.getKeyClassForEjbName(beanName). 193 replace('$', '.'); 194 pkClassParam[0] = pkClass; 195 196 PersistenceFieldElement[] allFields = pcClassElement.getFields(); 197 198 String prefix = srcout.getPath() + File.separator + 199 concreteImplName.replace('.', File.separatorChar); 200 201 String cmp_file_name = prefix + CMPTemplateFormatter.javaExtension_; 202 203 String hlp_file_name = prefix + CMPTemplateFormatter.Helper_ + 204 CMPTemplateFormatter.javaExtension_; 205 206 hasLocalInterface = 207 (nameMapper.getLocalInterfaceForEjbName(beanName) != null); 208 hasRemoteInterface = 209 (nameMapper.getRemoteInterfaceForEjbName(beanName) != null); 210 211 if (logger.isLoggable(Logger.FINE)) { 212 logger.fine("allFields: " + ((allFields != null) ? allFields.length : 0)); 214 logger.fine("cmp_file_name: " + cmp_file_name); logger.fine("hlp_file_name: " + hlp_file_name); logger.fine("cmp_name: " + concreteImplName); logger.fine("pkClass: " + pkClass); logger.fine("PCname: " + pcname); } 220 221 File cmp_file = new File (cmp_file_name); 222 JavaFileWriter concreteImplFileWriter = new IOJavaFileWriter(cmp_file); 223 concreteImplWriter = new IOJavaClassWriter(); 224 225 File hlp_file = new File (hlp_file_name); 226 JavaFileWriter helperFileWriter = new IOJavaFileWriter(hlp_file); 227 jdoHelperWriter = new IOJavaClassWriter(); 228 229 if (pkgName != null && pkgName.length() > 0) { 231 concreteImplFileWriter.setPackage(pkgName, null); 232 helperFileWriter.setPackage(pkgName, null); 233 } 234 235 addImportStatements(concreteImplFileWriter, helperFileWriter); 237 238 oneParam[0] = CMPTemplateFormatter.cmpImplCommentsTemplate; 240 concreteImplWriter.setClassDeclaration(Modifier.PUBLIC, 241 shortCmpName, oneParam); 242 243 addInterfaces(); 245 246 concreteImplWriter.setSuperclass(abstractBean); 247 248 concreteImplWriter.addConstructor(shortCmpName, 250 Modifier.PUBLIC, null, null, null, 251 CMPTemplateFormatter.super_, null); 252 253 helperName = shortCmpName + CMPTemplateFormatter.Helper_; 255 256 oneParam[0] = shortCmpName; 257 jdoHelperWriter.setClassDeclaration(Modifier.PUBLIC, 258 helperName, CMPTemplateFormatter.getBodyAsStrings( 259 CMPTemplateFormatter.hcomformatter.format(oneParam))); 260 261 setHelperSuperclass(); 262 263 generateFields(); 265 266 generateTypeSpecificMethods(allFields, methodHelper); 268 269 generateFinders(methodHelper); 271 272 generateCreateMethods(methodHelper.getCreateMethods()); 274 275 generateKnownMethods(methodHelper); 277 278 generateHelperClassMethods(); 280 281 generateConversions(); 283 284 generatePKObjectIdConversion(getKeyFields(allFields)); 286 287 concreteImplFileWriter.addClass(concreteImplWriter); 289 concreteImplFileWriter.save(); 290 291 helperFileWriter.addClass(jdoHelperWriter); 292 helperFileWriter.save(); 293 294 files.add(cmp_file); 295 files.add(hlp_file); 296 297 return files; 298 } 299 300 302 void addImportStatements(JavaFileWriter concreteImplFileWriter, 303 JavaFileWriter helperFileWriter) throws IOException { 304 305 String [] st = CMPTemplateFormatter.importsArray; 306 for (int i = 0; i < st.length; i++) { 307 concreteImplFileWriter.addImport(st[i], null); 308 } 309 310 st = CMPTemplateFormatter.helperImportsArray; 311 for (int i = 0; i < st.length; i++) { 312 helperFileWriter.addImport(st[i], null); 313 } 314 } 315 316 319 void addInterfaces() throws IOException { 320 321 String [] st = CMPTemplateFormatter.interfacesArray; 322 for (int i = 0; i < st.length; i++) { 323 concreteImplWriter.addInterface(st[i]); 324 } 325 } 326 327 330 abstract void setHelperSuperclass() throws IOException ; 331 332 337 void generateTypeSpecificMethods( 338 PersistenceFieldElement[] allFields, 339 AbstractMethodHelper methodHelper) 340 throws IOException { 341 342 if (isUpdateable) { 345 loadNonDFGBody = null; 346 } else { 347 loadNonDFGBody = new StringBuffer (); 348 } 349 } 350 351 355 void addCodeGenInputFilesSignature(String newSignature) 356 { 357 if ((inputFilesSignature == null) || 358 (inputFilesSignature.length() == 0)) { 359 inputFilesSignature = newSignature; 360 } 361 else { 362 inputFilesSignature = 363 inputFilesSignature + 364 CMPTemplateFormatter.signatureDelimiter_ + 365 newSignature; 366 } 367 } 368 369 373 void addCodeGeneratorClassSignature(String newSignature) 374 { 375 if ((generatorClassesSignature == null) || 376 (generatorClassesSignature.length() == 0)) { 377 generatorClassesSignature = newSignature; 378 } 379 else { 380 generatorClassesSignature = 381 generatorClassesSignature + 382 CMPTemplateFormatter.signatureDelimiter_ + 383 newSignature; 384 } 385 } 386 387 390 void generateFields() throws IOException { 391 392 CMPTemplateFormatter.addPrivateField( 394 CMPTemplateFormatter.privatetransientvformatter.format(pcnameParam), 395 Modifier.TRANSIENT, 396 concreteImplWriter); 397 398 CMPTemplateFormatter.addPrivateField( 400 CMPTemplateFormatter.privateStaticVariablesTemplate, 401 Modifier.STATIC, 402 concreteImplWriter); 403 404 twoParams[0] = pcname; 406 twoParams[1] = beanName; 407 CMPTemplateFormatter.addPrivateField( 408 CMPTemplateFormatter.privatestaticfinalvformatter.format(twoParams), 409 Modifier.STATIC + Modifier.FINAL, 410 concreteImplWriter); 411 412 twoParams[0] = generatorClassesSignature; 414 twoParams[1] = inputFilesSignature; 415 CMPTemplateFormatter.addFields( 416 CMPTemplateFormatter.publicstaticfinalvformatter.format(twoParams), 417 Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, 418 concreteImplWriter); 419 420 423 CMPTemplateFormatter.addPrivateField( 424 CMPTemplateFormatter.otherVariablesTemplate, 425 0, concreteImplWriter); 426 427 twoParams[0] = concreteImplName; 428 twoParams[1] = beanName; 429 CMPTemplateFormatter.addPrivateField( 430 CMPTemplateFormatter.hvformatter.format(twoParams), 431 Modifier.TRANSIENT+Modifier.STATIC, 432 jdoHelperWriter); 433 434 if (!isUpdateable) { 436 CMPTemplateFormatter.addPrivateField( 438 CMPROTemplateFormatter.privatetransientvformatter.format(pcnameParam), 439 Modifier.TRANSIENT, 440 concreteImplWriter); 441 442 CMPTemplateFormatter.addPrivateField( 444 CMPROTemplateFormatter.privateStaticVariablesTemplate, 445 Modifier.STATIC, 446 concreteImplWriter); 447 } 448 449 } 450 451 453 void generateFinders(AbstractMethodHelper methodHelper) 454 throws IOException { 455 456 boolean debug = logger.isLoggable(Logger.FINE); 457 List finders = methodHelper.getFinders(); 458 for (int i = 0; i < finders.size(); i++) { 459 Method m = (Method )finders.get(i); 460 String mname = CMPTemplateFormatter.ejb_ + 461 StringHelper.getCapitalizedString(m.getName()); 462 463 if (debug) { 464 logger.fine("Finder: " + mname); } 466 467 if (mname.equals(CMPTemplateFormatter.ejbFindByPrimaryKey_)) { 468 String [] exceptionTypes = 470 CMPTemplateFormatter.getExceptionNames(m); 471 472 oneParam[0] = CMPTemplateFormatter.key_; 473 474 concreteImplWriter.addMethod(CMPTemplateFormatter.ejbFindByPrimaryKey_, Modifier.PUBLIC , pkClass, oneParam, pkClassParam, exceptionTypes, CMPTemplateFormatter.ejbFindByPrimaryKeyBody, null); 483 } else { 484 JDOQLElements rs = getJDOQLElements(m, methodHelper); 485 486 String returnType = isSingleObjectFinder(m) ? 488 pkClass : m.getReturnType().getName(); 489 CMPTemplateFormatter.addGenericMethod( 490 m, mname, returnType, 491 generateFinderMethodBody(methodHelper, rs, mname, m, returnType, i), 492 concreteImplWriter); 493 } 494 } 495 496 497 } 498 499 505 abstract JDOQLElements getJDOQLElements(Method m, 506 AbstractMethodHelper methodHelper) throws IOException ; 507 508 510 private void generateCreateMethods(List createMethods) throws IOException { 511 Class beanClass = null; 512 try { 513 beanClass = Class.forName(abstractBean, true, loader); 514 } catch (Exception e) { 515 throw new RuntimeException (e.toString()); 516 } 517 518 HashSet generated = new HashSet(); 520 521 for (int i = 0; i < createMethods.size(); i++) { 522 Method m = (Method )createMethods.get(i); 523 Method m1 = m; 524 525 String createName =CMPTemplateFormatter.ejbCreate_; 527 String postCreateName =CMPTemplateFormatter.ejbPostCreate_; 528 if (m.getName().length() > 6) { 529 String suffix = m.getName().substring(6); 530 createName += suffix; 531 postCreateName += suffix; 532 } 533 534 boolean debug = logger.isLoggable(Logger.FINE); 535 if (debug) { 536 logger.fine("CreateMethod: " + abstractBean + "" + m.getName()); logger.fine("ejbCreateMethod: " + createName); logger.fine("ejbPostCreateMethod: " + postCreateName); } 540 541 try { 543 Class [] params = m.getParameterTypes(); 544 545 for (int j = 0; j < params.length; j++) { 548 if (params[j].isPrimitive() || 549 params[j].getClassLoader() == null || 550 params[j].getClassLoader().equals(loader)) { 551 continue; 552 } 553 String pname = params[j].getName(); 554 555 if (debug) { 556 logger.fine("Replacing parameter class for: " + pname); logger.fine("Param ClassLoader: " + params[j].getClassLoader()); 558 logger.fine("Need ClassLoader: " + loader); 559 } 560 561 params[j] = Class.forName(pname, true, loader); 562 } 563 565 m = beanClass.getMethod(createName, params); 568 m1 = beanClass.getMethod(postCreateName, params); 569 if (generated.contains(m)) { 570 if (debug) { 572 logger.fine("...generated..."); } 574 575 continue; 576 577 } 578 generated.add(m); 579 580 } catch (Exception e) { 581 continue; 583 } 584 585 String [] exc = CMPTemplateFormatter.getExceptionNames(m); 586 String parametersList = CMPTemplateFormatter.getParametersList(m); 587 String parametersListWithSeparator = makeLiteral( 588 CMPTemplateFormatter.getParametersListWithSeparator( 589 m, CMPTemplateFormatter.paramConcatenator_ )); 590 591 String body = getEJBCreateMethodBody(createName, exc, 592 parametersList, parametersListWithSeparator); 593 594 CMPTemplateFormatter.addGenericMethod( 595 m, createName, pkClass, body, concreteImplWriter); 596 597 body = getEJBPostCreateMethodBody(postCreateName, 598 parametersList, parametersListWithSeparator); 599 600 CMPTemplateFormatter.addGenericMethod( 601 m1, postCreateName, CMPTemplateFormatter.void_, 602 body, concreteImplWriter); 603 } 604 } 605 606 614 abstract String getEJBCreateMethodBody(String createName, 615 String [] exc, String parametersList, 616 String parametersListWithSeparator); 617 618 625 abstract String getEJBPostCreateMethodBody(String postCreateName, 626 String parametersList, String parametersListWithSeparator); 627 628 631 abstract String getEJBRemoveMethodBody(); 632 633 642 void generateKnownMethods(AbstractMethodHelper methodHelper) 643 throws IOException { 644 645 String [] exc = null; 646 String [] st = CMPTemplateFormatter.commonPublicMethodsArray; 647 for (int i = 0; i < st.length; i++) { 648 String mname = st[i]; 649 exc = getExceptionList(methodHelper, mname); 650 651 String body = null; 652 if (mname.equals(CMPTemplateFormatter.ejbRemove_)) { 653 body = getEJBRemoveMethodBody(); 654 655 } else if (mname.equals(CMPTemplateFormatter.ejb__flush_)) { 656 oneParam[0] = CMPTemplateFormatter.DuplicateKeyException_; 657 exc = oneParam; 658 body = CMPTemplateFormatter.helpers.getProperty(mname); 659 660 } else { 661 body = CMPTemplateFormatter.helpers.getProperty(mname); 662 } 663 664 concreteImplWriter.addMethod(mname, Modifier.PUBLIC, CMPTemplateFormatter.void_, null, null, exc, CMPTemplateFormatter.getBodyAsStrings(body), null); 673 } 674 675 oneParam[0] = CMPTemplateFormatter.int_; 677 concreteImplWriter.addMethod(CMPTemplateFormatter.afterCompletion_, Modifier.PUBLIC, CMPTemplateFormatter.void_, param0, oneParam, null, CMPTemplateFormatter.afterCompletionBody, null); 686 concreteImplWriter.addMethod(CMPTemplateFormatter.ejb__remove_, Modifier.PUBLIC , CMPTemplateFormatter.void_, param0, objectType, null, null, null); 696 String body; 697 st = CMPTemplateFormatter.commonPrivateMethodsArray; 698 699 for (int i = 0; i < st.length; i++) { 700 String mname = st[i]; 701 body = CMPTemplateFormatter.helpers.getProperty(mname); 702 703 CMPTemplateFormatter.addGenericMethod(mname, 704 CMPTemplateFormatter.getBodyAsStrings(body), concreteImplWriter); 705 } 706 707 oneParam[0] = CMPTemplateFormatter.byte_; 709 body = CMPTemplateFormatter.jdoarraycopyformatter.format(oneParam); 710 711 oneParam[0] = CMPTemplateFormatter.byteArray_; 712 concreteImplWriter.addMethod( 713 CMPTemplateFormatter.jdoArrayCopy_, Modifier.PRIVATE, CMPTemplateFormatter.byteArray_, param0, oneParam, null, CMPTemplateFormatter.getBodyAsStrings(body), null); 722 oneParam[0] = CMPTemplateFormatter.EntityContext_; 724 concreteImplWriter.addMethod(CMPTemplateFormatter.setEntityContext_, Modifier.PUBLIC, CMPTemplateFormatter.void_, param0, oneParam, getExceptionList(methodHelper, 730 CMPTemplateFormatter.setEntityContext_, 731 oneParam), CMPTemplateFormatter.setEntityContextBody, null); 735 oneParam[0] = CMPTemplateFormatter.key_; 737 738 String [] param = new String []{concreteImplName}; 739 740 concreteImplWriter.addMethod(CMPTemplateFormatter.getObjectId_, Modifier.PRIVATE , CMPTemplateFormatter.Object_, oneParam, pkClassParam, null, CMPTemplateFormatter.getBodyAsStrings( 747 CMPTemplateFormatter.goidformatter.format(param)), null); 750 oneParam[0] = CMPTemplateFormatter.jdoGetJdoInstanceClassTemplate; 752 753 CMPTemplateFormatter.addGenericMethod( 754 CMPTemplateFormatter.jdoGetJdoInstanceClass_, 755 Modifier.PUBLIC + Modifier.STATIC, 756 CMPTemplateFormatter.Class_, oneParam, 757 concreteImplWriter); 758 759 generateSpecialKnownMethods(); 760 } 761 762 765 void generateSpecialKnownMethods() throws IOException { 766 767 String [] body = null; 768 769 772 if (isUpdateable) { 774 body = CMPTemplateFormatter.jdoClosePersistenceManagerBody; 775 } 776 777 CMPTemplateFormatter.addGenericMethod( 778 CMPTemplateFormatter.jdoClosePersistenceManager_, 779 body, concreteImplWriter); 780 781 if (isUpdateable) { 783 body = CMPTemplateFormatter.assertPersistenceManagerIsNullBody; 784 } 785 CMPTemplateFormatter.addGenericMethod( 786 CMPTemplateFormatter.assertPersistenceManagerIsNull_, 787 body, concreteImplWriter); 788 789 if (isUpdateable) { 791 oneParam[0] = I18NHelper.getMessage(messages, "EXC_TransactionNotActive"); body = CMPTemplateFormatter.getBodyAsStrings( 793 CMPTemplateFormatter.intxformatter.format(oneParam)); 794 } 795 CMPTemplateFormatter.addGenericMethod( 796 CMPTemplateFormatter.assertInTransaction_, 797 body, concreteImplWriter); 798 799 802 if (isUpdateable) { 805 body = CMPTemplateFormatter.jdoGetPersistenceManagerBody; 806 } else { 807 body = CMPROTemplateFormatter.jdoGetPersistenceManagerBody; 808 } 809 CMPTemplateFormatter.addGenericMethod( 810 CMPTemplateFormatter.jdoGetPersistenceManager_, 811 CMPTemplateFormatter.jdoPersistenceManagerClass_, 812 body, concreteImplWriter); 813 814 oneParam[0] = concreteImplName; 816 MessageFormat mformat = null; 817 if (isUpdateable) { 818 mformat = CMPTemplateFormatter.jdolookuppmfformatter; 819 } else { 820 mformat = CMPROTemplateFormatter.jdolookuppmfformatter; 821 } 822 CMPTemplateFormatter.addGenericMethod( 823 CMPTemplateFormatter.jdoLookupPersistenceManagerFactory_, 824 Modifier.PRIVATE + Modifier.STATIC + Modifier.SYNCHRONIZED, CMPTemplateFormatter.getBodyAsStrings(mformat.format(oneParam)), 826 concreteImplWriter); 827 828 threeParams[0] = pkClass; 830 threeParams[1] = pcname; 831 threeParams[2] = CMPTemplateFormatter.none_; if (isUpdateable) { 833 mformat = CMPTemplateFormatter.giformatter; 834 } else { 835 if (loadNonDFGBody != null) { 836 threeParams[2] = loadNonDFGBody.toString(); 837 } 838 mformat = CMPROTemplateFormatter.giformatter; 839 } 840 CMPTemplateFormatter.addGenericMethod( 841 CMPTemplateFormatter.getInstance_, 842 CMPTemplateFormatter.getBodyAsStrings(mformat.format(threeParams)), 843 concreteImplWriter); 844 845 847 if (isUpdateable) { 849 body = null; 850 } else { 851 body = CMPTemplateFormatter.getBodyAsStrings( 853 CMPROTemplateFormatter.ejb__refreshformatter.format(threeParams)); 854 } 855 concreteImplWriter.addMethod(CMPTemplateFormatter.ejb__refresh_, Modifier.PUBLIC , CMPTemplateFormatter.void_, param0, objectType, null, body, null); 864 oneParam[0] = CMPTemplateFormatter.jdoPersistenceManagerClass_; 867 868 if (isUpdateable) { 869 body = CMPTemplateFormatter.jdoReleasePersistenceManagerBody; 870 } else { 871 body = CMPROTemplateFormatter.jdoReleasePersistenceManagerBody; 872 } 873 concreteImplWriter.addMethod(CMPTemplateFormatter.jdoReleasePersistenceManager_, Modifier.PRIVATE, CMPTemplateFormatter.void_, param0, oneParam, null, body, null); 882 } 883 884 887 void generateHelperClassMethods() throws IOException { 888 oneParam[0] = CMPTemplateFormatter.assertInstanceOfRemoteInterfaceImplTemplate; 890 891 jdoHelperWriter.addMethod(CMPTemplateFormatter.assertInstanceOfRemoteInterfaceImpl_, Modifier.PUBLIC, CMPTemplateFormatter.void_, param0, objectType, null, oneParam, null); 900 oneParam[0] = CMPTemplateFormatter.getHelperInstanceTemplate; 902 CMPTemplateFormatter.addGenericMethod( 903 CMPTemplateFormatter.getHelperInstance_, 904 Modifier.PUBLIC + Modifier.STATIC, 905 helperName, oneParam, 906 jdoHelperWriter); 907 908 CMPTemplateFormatter.addGenericMethod( 910 CMPTemplateFormatter.getContainer_, 911 Modifier.PUBLIC, CMPTemplateFormatter.Object_, 912 CMPTemplateFormatter.getContainerBody, 913 jdoHelperWriter); 914 915 oneParam[0] = concreteImplName; 917 CMPTemplateFormatter.addGenericMethod( 918 CMPTemplateFormatter.getPCClass_, 919 Modifier.PUBLIC, CMPTemplateFormatter.Class_, 920 CMPTemplateFormatter.getBodyAsStrings( 921 CMPTemplateFormatter.pcclassgetterformatter.format( 922 oneParam)), 923 jdoHelperWriter); 924 } 925 926 private String [] getKeyFields(PersistenceFieldElement[] fields) { 927 List returnList = new ArrayList(); 928 int i, count = ((fields != null) ? fields.length : 0); 929 930 for (i = 0; i < count; i++) { 931 PersistenceFieldElement pfe = fields[i]; 932 933 if (pfe.isKey()) 934 returnList.add(pfe.getName()); 935 } 936 937 return (String [])returnList.toArray(new String [returnList.size()]); 938 } 939 940 942 private void generatePKObjectIdConversion(String [] keyFields) 943 throws IOException { 944 int length = keyFields.length; 945 StringBuffer getOid = new StringBuffer (); StringBuffer getPK = new StringBuffer (); String [] pkfieldParam = new String [1]; 948 949 getOid.append(CMPTemplateFormatter.assertPKNotNullTemplate). 953 append(CMPTemplateFormatter.noidformatter.format(pcnameParam)); 954 getPK.append(CMPTemplateFormatter.assertOidNotNullTemplate). 955 append(CMPTemplateFormatter.oidcformatter.format(pcnameParam)); 956 957 boolean debug = logger.isLoggable(Logger.FINE); 958 if (length == 1) { 959 961 String pkfield = keyFields[0]; 963 String pkfieldType = model.getFieldType(pcname, pkfield); 964 pkfieldParam[0] = pkfield; 965 966 if (debug) { 967 logger.fine("pkfield: " + pkfield); } 969 970 if (model.isPrimitive(pcname, pkfield) || 971 (!pkClass.equals(pkfieldType) && 972 !pkClass.equals(Object .class.getName()))) { 973 974 979 getPK.append(CMPTemplateFormatter.npkformatter.format(pkClassParam)); 980 getOid.append(CMPTemplateFormatter.pkcformatter.format(pkClassParam)); 981 982 pkfieldParam[0] = pkfield; 983 984 getOid.append( 985 CMPTemplateFormatter.oidformatter.format(pkfieldParam)); 986 getPK.append( 987 CMPTemplateFormatter.pkformatter.format(pkfieldParam)); 988 989 getPK.append(CMPTemplateFormatter.returnKey_); 990 991 } else { 992 oneParam[0] = pkfieldType; 995 getOid.append(CMPTemplateFormatter.pkcformatter.format(oneParam)); 996 997 twoParams[0] = pkfield; 998 twoParams[1] = pkfieldType; 999 getOid.append( 1000 requireCloneOnGetAndSet(pkfieldType) ? 1001 CMPTemplateFormatter.oid1cloneformatter.format(twoParams) : 1002 (requireTrimOnSet(pkfieldType) ? 1003 CMPTemplateFormatter.oid1stringformatter.format(pkfieldParam) : 1004 CMPTemplateFormatter.oid1formatter.format(pkfieldParam))); 1005 1006 getPK.append( 1007 requireCloneOnGetAndSet(pkfieldType) ? 1008 CMPTemplateFormatter.pk1cloneformatter.format(pkfieldParam) : 1009 CMPTemplateFormatter.pk1formatter.format(pkfieldParam)); 1010 1011 } 1012 } else { 1013 getPK.append(CMPTemplateFormatter.npkformatter.format(pkClassParam)); 1015 getOid.append(CMPTemplateFormatter.pkcformatter.format(pkClassParam)); 1016 1017 for (int i = 0; i < length; i++) { 1018 String pkfield = keyFields[i]; 1019 pkfieldParam[0] = pkfield; 1020 1021 if (debug) { 1022 logger.fine("pkfield: " + pkfield); } 1024 1025 if (!model.isPrimitive(pcname, pkfield)) { 1026 getOid.append( 1027 CMPTemplateFormatter.assertpkfieldformatter.format(pkfieldParam)); 1028 } 1029 1030 String pkfieldType = model.getFieldType(pcname, pkfield); 1031 twoParams[0] = pkfield; 1032 twoParams[1] = pkfieldType; 1033 getOid.append( 1034 requireCloneOnGetAndSet(pkfieldType) ? 1035 CMPTemplateFormatter.oidcloneformatter.format(twoParams) : 1036 (requireTrimOnSet(pkfieldType) ? 1037 CMPTemplateFormatter.oidstringformatter.format(pkfieldParam) : 1038 CMPTemplateFormatter.oidformatter.format(pkfieldParam))); 1039 getPK.append( 1040 requireCloneOnGetAndSet(pkfieldType) ? 1041 CMPTemplateFormatter.pkcloneformatter.format(twoParams) : 1042 CMPTemplateFormatter.pkformatter.format(pkfieldParam)); 1043 } 1044 1045 getPK.append(CMPTemplateFormatter.returnKey_); 1046 } 1047 getOid.append(CMPTemplateFormatter.returnOid_); 1048 1049 1050 jdoHelperWriter.addMethod(CMPTemplateFormatter.convertPrimaryKeyToObjectId_, Modifier.PUBLIC, CMPTemplateFormatter.Object_, param0, objectType, null, CMPTemplateFormatter.getBodyAsStrings(getOid.toString()), null); 1060 jdoHelperWriter.addMethod(CMPTemplateFormatter.convertObjectIdToPrimaryKey_, Modifier.PUBLIC, CMPTemplateFormatter.Object_, param0, objectType, null, CMPTemplateFormatter.getBodyAsStrings(getPK.toString()), null); 1069 } 1070 1071 1077 String getConcreteBeanForPCClass(String pcClass) { 1078 return nameMapper.getConcreteBeanClassForEjbName( 1079 nameMapper.getEjbNameForPersistenceClass(pcClass)); 1080 } 1081 1082 1086 void generateConversions() throws IOException { 1087 String [] pcParams = new String [] {CMPTemplateFormatter.pc_, 1088 CMPTemplateFormatter.jdoPersistenceManager_}; 1089 String [] pcParamTypes = new String [] {CMPTemplateFormatter.Object_, 1090 CMPTemplateFormatter.jdoPersistenceManagerClass_}; 1091 1092 String [] collParamTypes = new String [] {CMPTemplateFormatter.Collection_, 1093 CMPTemplateFormatter.jdoPersistenceManagerClass_}; 1094 1095 String [] body = null; 1097 1098 if (hasRemoteInterface == false) { 1100 body = CMPTemplateFormatter.getBodyAsStrings( 1101 CMPTemplateFormatter.returnNull_ ); 1102 1103 jdoHelperWriter.addMethod(CMPTemplateFormatter.convertPCToEJBObject_, Modifier.PUBLIC, CMPTemplateFormatter.ejbObject_, pcParams, pcParamTypes, null, body, null); 1112 twoParams[0] = CMPTemplateFormatter.ejbObject_; 1113 twoParams[1] = CMPTemplateFormatter.jdoPersistenceManagerClass_; 1114 jdoHelperWriter.addMethod(CMPTemplateFormatter.convertEJBObjectToPC_, Modifier.PUBLIC, CMPTemplateFormatter.Object_, param0PM, twoParams, null, body, null); } 1123 1124 } 1125 1126 1133 boolean containsException(String [] exc, String checkExc) { 1134 boolean rc = false; 1135 if (exc != null) { 1136 for (int i = 0; i < exc.length; i++) { 1137 if (exc[i].equals(checkExc)) { 1138 rc = true; 1139 break; 1140 } 1141 } 1142 } 1143 return rc; 1144 } 1145 1146 1154 String getException(String [] exc, String checkExc) { 1155 return (containsException(exc, checkExc)? checkExc : 1156 CMPTemplateFormatter.ejbException_); 1157 } 1158 1159 1168 String getException(String [] exc, String checkExc, String superExc) { 1169 String rc = CMPTemplateFormatter.ejbException_; 1170 if (exc != null) { 1171 for (int i = 0; i < exc.length; i++) { 1172 if (exc[i].equals(checkExc) || exc[i].equals(superExc)) { 1173 rc = checkExc; 1174 break; 1175 } 1176 } 1177 } 1178 return rc; 1179 } 1180 1181 1183 1188 private boolean isSingleObjectFinder(Method finder) { 1189 return (!(finder.getReturnType().equals(java.util.Collection .class) || 1190 finder.getReturnType().equals(java.util.Enumeration .class))); 1191 } 1192 1193 1206 private String generateFinderMethodBody(AbstractMethodHelper methodHelper, 1207 JDOQLElements jdoqlElements, 1208 String mname, 1209 Method m, 1210 String returnType, 1211 int index) throws IOException { 1212 1213 StringBuffer body = new StringBuffer (); 1214 body.append(CMPTemplateFormatter.assertPersistenceManagerIsNullTemplate); 1215 body.append(CMPTemplateFormatter.endLine_); 1216 body.append(generateFinderSelectorCommonBody(methodHelper, 1217 jdoqlElements, 1218 mname, 1219 m, 1220 returnType, 1221 index)); 1222 1223 oneParam[0] = mname; 1225 1226 if (isSingleObjectFinder(m)) { 1228 fourParams[0] = mname; 1230 fourParams[1] = pkClass; 1231 fourParams[2] = concreteImplName; 1232 fourParams[3] = CMPTemplateFormatter.catchClauseTemplate; 1233 body.append(CMPTemplateFormatter.singlefinderformatter.format(fourParams)); 1234 } else { 1235 twoParams[0] = concreteImplName; 1239 twoParams[1] = CMPTemplateFormatter.catchClauseTemplate; 1240 if (isFinderReturningEnumeration(m)) { 1241 body.append(CMPTemplateFormatter.multifinderenumerationformatter.format(twoParams)); 1242 } else { 1243 body.append(CMPTemplateFormatter.multifinderformatter.format(twoParams)); 1244 } 1245 } 1246 1247 return body.toString(); 1248 } 1249 1250 1268 String generateFinderSelectorCommonBody(AbstractMethodHelper methodHelper, 1269 JDOQLElements jdoqlElements, 1270 String methodName, 1271 Method m, 1272 String returnType, 1273 int index) throws IOException { 1274 String queryVariableQualifier = m.getName() + '_' + index; 1276 1277 oneParam[0] = queryVariableQualifier; 1280 CMPTemplateFormatter.addPrivateField( 1281 CMPTemplateFormatter.finderselectorstaticvformatter.format(oneParam), 1282 Modifier.STATIC, 1283 concreteImplWriter); 1284 CMPTemplateFormatter.addPrivateField( 1285 CMPTemplateFormatter.finderselectorstaticfinalvformatter.format(oneParam), 1286 Modifier.STATIC + Modifier.FINAL, 1287 concreteImplWriter); 1288 1289 StringBuffer body = new StringBuffer (); 1290 1291 String [] parameterEjbNames = jdoqlElements.getParameterEjbNames(); 1292 1293 body.append(generateFinderSelectorParamCheck(m, parameterEjbNames)); 1295 1296 String pcClassName = jdoqlElements.getCandidateClassName(); 1298 String concreteBeanClassName = getConcreteBeanForPCClass(pcClassName); 1299 queryParams[0] = returnType; 1300 queryParams[1] = queryVariableQualifier; 1301 queryParams[2] = concreteBeanClassName; 1302 queryParams[3] = StringHelper.escape(jdoqlElements.getFilter()); 1303 queryParams[4] = StringHelper.escape(jdoqlElements.getParameters()); 1304 queryParams[5] = StringHelper.escape(jdoqlElements.getVariables()); 1305 queryParams[6] = StringHelper.escape(jdoqlElements.getResult()); 1306 queryParams[7] = StringHelper.escape(jdoqlElements.getOrdering()); 1307 queryParams[8] = Boolean.toString(methodHelper.isQueryPrefetchEnabled(m)); 1308 queryParams[9] = StringHelper.escape(generateQueryIgnoreCache()); 1309 body.append(CMPTemplateFormatter.finderselectorformatter.format(queryParams)); 1310 1311 String queryParam = generateParamConvBody(m, parameterEjbNames); 1314 1315 if (jdoqlElements.isAggregate()) { 1316 if (queryParam == null) { 1317 oneParam[0] = CMPTemplateFormatter.none_; 1318 body.append(CMPTemplateFormatter.aggqueryexecformatter.format(oneParam)); 1319 } else { 1320 oneParam[0] = queryParam; 1321 body.append( 1322 CMPTemplateFormatter.aggqueryexecparamconvformatter.format(oneParam)); 1323 } 1324 } else { 1325 if (queryParam == null) { 1326 oneParam[0] = CMPTemplateFormatter.none_; 1327 body.append(CMPTemplateFormatter.queryexecformatter.format(oneParam)); 1328 } else { 1329 oneParam[0] = queryParam; 1330 body.append( 1331 CMPTemplateFormatter.queryexecparamconvformatter.format(oneParam)); 1332 } 1333 } 1334 1335 return body.toString(); 1336 } 1337 1338 1346 String generateFinderSelectorParamCheck(Method m, 1347 String [] parameterEjbNames) { 1348 StringBuffer checkBody = new StringBuffer (); 1349 1350 return checkBody.toString(); 1351 } 1352 1353 1358 String generateQueryIgnoreCache() 1359 { 1360 return CMPTemplateFormatter.none_; 1361 } 1362 1363 1368 abstract boolean isFinderReturningEnumeration(Method finder); 1369 1370 1380 private String generateParamConvBody(Method m, String [] parameterEjbNames) { 1381 1382 StringBuffer paramString = new StringBuffer (); 1383 Class [] paramTypes = m.getParameterTypes(); 1384 int paramLength = paramTypes.length; 1385 MessageFormat mformat = null; 1386 String paramClassName = null; 1387 1388 if (paramLength > 0) { 1389 for (int i = 0; i < paramLength; i++) { 1391 paramClassName = paramTypes[i].getName(); 1392 1393 if (nameMapper.isLocalInterface(paramClassName) || 1395 nameMapper.isRemoteInterface(paramClassName)) { 1396 1397 if (parameterEjbNames[i] != null) { 1398 mformat = CMPTemplateFormatter.queryexecparamconvargumentformatter; 1399 String concreteImplName = 1400 nameMapper.getConcreteBeanClassForEjbName( 1401 parameterEjbNames[i]); 1402 threeParams[0] = concreteImplName; 1403 threeParams[1] = String.valueOf(i); 1404 threeParams[2] = 1405 nameMapper.isLocalInterface(paramClassName) ? 1406 CMPTemplateFormatter.convertEJBLocalObjectToPC_ : 1407 CMPTemplateFormatter.convertEJBObjectToPC_; 1408 paramString.append(mformat.format(threeParams)); 1409 } else { 1410 paramString.append(CMPTemplateFormatter.param_ + i); 1411 } 1412 1413 } else if(paramTypes[i].isPrimitive()) { 1415 paramString.append( 1416 JavaClassWriterHelper.getWrapperExpr( 1417 paramTypes[i], 1418 JavaClassWriterHelper.param_ + i 1419 )); 1420 1421 } else { 1423 paramString.append(CMPTemplateFormatter.param_ + i); 1424 } 1425 if (i < paramLength - 1) paramString.append( 1427 CMPTemplateFormatter.paramSeparator_); 1428 } 1429 } else return null; 1430 1431 return paramString.toString(); 1432 } 1433 1434 1444 String [] getExceptionList(AbstractMethodHelper methodHelper, 1445 String mname, 1446 String [] paramTypeNames) { 1447 String [] rc = null; 1448 Class [] paramTypes = null; 1449 1450 Map methodNames = methodHelper.getMethodNames(); 1451 Method m = (Method ) methodNames.get(mname); 1452 1453 boolean debug = logger.isLoggable(Logger.FINE); 1454 if (debug) { 1455 logger.fine("Processing method: " + mname); 1456 logger.fine("Known method: " + m); 1457 } 1458 1459 if (m == null) { 1460 if( paramTypeNames != null ) { 1462 paramTypes = new Class [ paramTypeNames.length ]; 1463 try { 1464 for( int i = paramTypeNames.length - 1; i >= 0; i-- ) { 1465 paramTypes[i] = Class.forName( paramTypeNames[i], true, loader ); 1466 } 1467 } catch( Exception e ) { 1468 } 1470 } 1471 1472 try { 1473 Class beanClass = Class.forName(abstractBean, true, loader); 1474 m = beanClass.getMethod(mname, paramTypes); 1475 if (debug) { 1476 logger.fine("Found method: " + m); 1477 } 1478 1479 } catch (Exception e) { 1480 } 1482 } 1483 1484 if (m != null) { 1485 rc = CMPTemplateFormatter.getExceptionNames(m); 1486 } 1487 1488 return rc; 1489 } 1490 1491 1500 String [] getExceptionList( AbstractMethodHelper methodHelper, String mname ) { 1501 return getExceptionList( methodHelper, mname, null ); 1502 } 1503 1504 1509 private String makeLiteral(String st) { 1510 return (StringHelper.isEmpty(st)) ? 1511 CMPTemplateFormatter.escapedEmptyString_ : 1512 CMPTemplateFormatter.paramInitializer_ + st; 1513 } 1514 1515 1520 String getSignaturesOfGeneratorClasses() 1521 { 1522 StringBuffer signatures = new StringBuffer (). 1523 1524 append(JDOConcreteBeanGenerator.SIGNATURE). 1526 append(CMPTemplateFormatter.signatureDelimiter_). 1527 1528 append(CMPTemplateFormatter.signatureTemplate). 1530 append(CMPTemplateFormatter.signatureDelimiter_). 1531 1532 append(DeploymentDescriptorModel.SIGNATURE); 1534 1535 return signatures.toString(); 1536 } 1537 1538 1543 boolean requireCloneOnGetAndSet(String fieldType) { 1544 return (CMPTemplateFormatter.Date_.equals(fieldType) || 1545 CMPTemplateFormatter.SqlDate_.equals(fieldType) || 1546 CMPTemplateFormatter.SqlTime_.equals(fieldType) || 1547 CMPTemplateFormatter.SqlTimestamp_.equals(fieldType)); 1548 } 1549 1550 1554 boolean requireTrimOnSet(String fieldType) { 1555 return CMPTemplateFormatter.String_.equals(fieldType); 1556 } 1557 1558 1561 void loadNonDFGField(FieldInfo fieldInfo) { 1562 if( !isUpdateable && !fieldInfo.isDFG ) { 1563 oneParam[0] = fieldInfo.getter; 1564 loadNonDFGBody.append( 1565 CMPROTemplateFormatter.loadNonDFGformatter.format(oneParam)); 1566 } 1567 } 1568 1569 1573 class FieldInfo { 1574 1575 final PersistenceFieldElement pfe; 1576 1577 final String name; 1578 final String type; 1579 final String getter; 1580 final String setter; 1581 1582 final boolean isKey; 1583 final boolean isPrimitive; 1584 final boolean isByteArray; 1585 final boolean isSerializable; 1586 final boolean requireCloneOnGetAndSet; 1587 final boolean isGeneratedField; 1588 final boolean isDFG; 1589 1590 FieldInfo(Model model, NameMapper nameMapper, 1591 PersistenceFieldElement pfe, 1592 String beanName, String pcname) { 1593 1594 this.pfe = pfe; 1595 1596 String pfn = pfe.getName(); 1597 name = nameMapper.getEjbFieldForPersistenceField(pcname, pfn); 1598 1599 String fname = StringHelper.getCapitalizedString(name); 1600 getter = CMPTemplateFormatter.get_ + fname; 1601 setter = CMPTemplateFormatter.set_ + fname; 1602 1603 boolean debug = logger.isLoggable(Logger.FINE); 1604 if (debug) { 1605 logger.fine("-Methods: " + getter + " " + setter); } 1607 1608 isKey = pfe.isKey(); 1609 isPrimitive = model.isPrimitive(pcname, pfn); 1610 isByteArray = model.isByteArray(beanName, name); 1611 isSerializable = model.isByteArray(pcname, pfn); 1612 1613 if (isSerializable) { 1614 type = model.getFieldType(beanName, name).replace('$', '.'); 1616 } else { 1617 type = model.getFieldType(beanName, name); 1618 } 1619 1620 if (debug) { 1621 logger.fine("Field: " + name + " " + type); } 1623 1624 requireCloneOnGetAndSet = requireCloneOnGetAndSet(type); 1625 isGeneratedField = nameMapper.isGeneratedField(beanName, name); 1626 1627 MappingClassElement mce = model.getMappingClass(pcname); 1629 MappingFieldElement mfe = mce.getField(name); 1630 isDFG = (mfe.getFetchGroup() == MappingFieldElement.GROUP_DEFAULT); 1631 } 1632 } 1633 1634} 1635 1636 | Popular Tags |