1 23 24 29 30 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 31 32 import java.io.BufferedOutputStream ; 33 import java.io.File ; 34 import java.io.FileOutputStream ; 35 import java.io.OutputStream ; 36 import java.io.IOException ; 37 38 import java.util.*; 39 40 import java.sql.Connection ; 41 import java.sql.SQLException ; 42 43 import com.sun.ejb.codegen.CMPGenerator; 44 import com.sun.ejb.codegen.EjbcContext; 45 import com.sun.ejb.codegen.GeneratorException; 46 47 import com.sun.enterprise.deployment.EjbBundleDescriptor; 48 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor; 49 50 import com.sun.enterprise.deployment.backend.Deployer; 51 import com.sun.enterprise.deployment.io.DescriptorConstants; 52 import com.sun.enterprise.deployment.interfaces.QueryParser; 53 54 import com.sun.jdo.spi.persistence.utility.I18NHelper; 55 import com.sun.jdo.spi.persistence.utility.MergedBundle; 56 import com.sun.jdo.spi.persistence.utility.logging.Logger; 57 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants; 58 59 import com.sun.jdo.api.persistence.enhancer.generator.Main; 60 61 import com.sun.jdo.api.persistence.model.Model; 62 import com.sun.jdo.api.persistence.model.ModelException; 63 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement; 64 65 import com.sun.jdo.api.persistence.mapping.ejb.ConversionException; 66 67 import com.sun.jdo.spi.persistence.support.ejb.enhancer.meta.EJBMetaDataModelImpl; 68 import com.sun.jdo.spi.persistence.support.ejb.model.DeploymentDescriptorModel; 69 import com.sun.jdo.spi.persistence.support.ejb.ejbqlc.EJBQLException; 70 71 import com.sun.jdo.api.persistence.support.JDOUserException; 72 73 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper; 74 import com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc.JDOQLParameterDeclarationParser; 75 76 import com.sun.jdo.spi.persistence.generator.database.DDLGenerator; 77 import com.sun.jdo.spi.persistence.generator.database.DatabaseOutputStream; 78 79 import org.netbeans.modules.dbschema.DBException; 80 import org.netbeans.modules.dbschema.SchemaElement; 81 82 import org.netbeans.modules.schema2beans.Schema2BeansException; 83 84 90 public class JDOCodeGenerator 91 implements CMPGenerator, DatabaseConstants { 92 93 96 public static final String SIGNATURE = "$RCSfile: JDOCodeGenerator.java,v $ $Revision: 1.7 $"; 98 private static final String MAPPING_EXTENSION = ".mapping"; 100 101 private static final Logger logger = LogHelperEJBCompiler.getLogger(); 102 103 104 private static final ResourceBundle validationBundle = new MergedBundle( 105 I18NHelper.loadBundle(DeploymentDescriptorModel.class), 106 I18NHelper.loadBundle(Model.class)); 107 108 private ArrayList files = new ArrayList(); 109 110 private EjbcContext ejbcContext = null; 111 private ClassLoader loader = null; 112 private JDOConcreteBeanGenerator cmp11Generator; 113 private JDOConcreteBeanGenerator cmp20Generator; 114 private EjbBundleDescriptor bundle = null; 115 private NameMapper nameMapper; 116 private Model model; 117 private EJBMetaDataModelImpl ejbModel; 118 private static QueryParser jdoqlParamDeclParser = new JDOQLParameterDeclarationParser(); 119 private String inputFilesPath; 120 private String generatedXmlsPath; 121 private File classout; 122 private MappingGenerator mappingGenerator = null; 123 124 128 private static final boolean ignoreValidationResults = Deployer.getKeepFailedStubsValue(); 129 130 131 private static String signatures = null; 132 133 136 public void init(EjbBundleDescriptor bundle, EjbcContext ejbcContext, 137 String bundlePathName, String generatedXmlsPathName) 138 throws GeneratorException { 139 140 if (logger.isLoggable(Logger.FINE)) 141 logger.fine("cmp gen init"); 143 this.ejbcContext = ejbcContext; 144 this.generatedXmlsPath = generatedXmlsPathName; 145 this.inputFilesPath = bundlePathName; 146 this.classout = ejbcContext.getStubsDir(); 147 148 init(bundle, ejbcContext.getDescriptor().getClassLoader(), 149 bundlePathName, false); 150 } 151 152 160 public void init(EjbBundleDescriptor bundle, ClassLoader loader, 161 String bundlePathName) throws GeneratorException { 162 163 init(bundle, loader, bundlePathName, false); 164 } 165 166 174 public void init(EjbBundleDescriptor bundle, ClassLoader loader, 175 String bundlePathName, boolean ignoreSunDeploymentDescriptors) 176 throws GeneratorException { 177 if (logger.isLoggable(Logger.FINE)) 178 logger.fine("cmp gen init"); 180 this.bundle = bundle; 181 this.loader = loader; 182 183 inputFilesPath = bundlePathName; 184 185 try { 186 nameMapper = new NameMapper(bundle); 187 model = new DeploymentDescriptorModel(nameMapper, loader); 188 mappingGenerator = new MappingGenerator(bundle, model, nameMapper, loader); 189 loadOrCreateMappingClasses(ignoreSunDeploymentDescriptors); 190 191 ejbModel = new EJBMetaDataModelImpl(model); 192 193 } catch (IOException e) { 194 throw JDOCodeGeneratorHelper.createGeneratorException( 196 "CMG.IOExceptionInInit", bundle, e); } 198 } 199 200 203 public Collection validate(IASEjbCMPEntityDescriptor descr) { 204 205 Collection c = new ArrayList(); 206 207 c.addAll(validateModel(descr)); 208 209 if (getMappingMissingException(c) == null) 211 c.addAll(validateEJB(descr)); 212 213 if (logger.isLoggable(Logger.FINE)) { 214 for (Iterator i = c.iterator(); i.hasNext();) { 215 logger.log(Logger.FINE, "validation exception: ", (Exception )i.next()); 217 } 218 } 219 220 return c; 221 } 222 223 238 private Collection validateModel(IASEjbCMPEntityDescriptor descr) { 239 String beanName = descr.getName(); 240 String className = nameMapper.getPersistenceClassForEjbName(beanName); 241 242 if (model.getPersistenceClass(className) == null) { 243 return Collections.singletonList( 244 JDOCodeGeneratorHelper.createGeneratorException( 245 "CMG.MissingBeanMapping", beanName, bundle)); } 247 248 return model.validate(className, loader, validationBundle); 249 } 250 251 263 private GeneratorException getMappingMissingException(Collection c) { 264 if (c.size() == 1) { 265 Object firstElement = c.iterator().next(); 266 267 if (firstElement instanceof GeneratorException) 268 return (GeneratorException)firstElement; 269 } 270 271 return null; 272 } 273 274 282 private Collection validateEJB(IASEjbCMPEntityDescriptor descr) { 283 Collection c = null; 284 try { 285 JDOConcreteBeanGenerator cmpGenerator = getCMPGenerator(descr); 286 c = cmpGenerator.validate(new MethodHelper(descr), descr.getName()); 287 } catch (GeneratorException e) { 288 c = new ArrayList(); 289 c.add(e); 290 } 291 292 return c; 293 } 294 295 302 private Collection validateSupported(IASEjbCMPEntityDescriptor descr) { 303 Collection rc = new ArrayList(); 304 307 308 return rc; 309 } 310 311 314 public void generate(IASEjbCMPEntityDescriptor ejbcmp, File srcout, 315 File classout) 316 throws GeneratorException { 317 318 String beanName = ejbcmp.getName(); 319 320 StringBuffer validateex = null; 323 324 boolean debug = logger.isLoggable(Logger.FINE); 325 if (debug) 326 logger.fine("gen file in " + srcout.getAbsolutePath()); 328 Collection c = new ArrayList(validateModel(ejbcmp)); 333 334 GeneratorException mappingMissingEx = getMappingMissingException(c); 337 if (mappingMissingEx != null) 338 throw mappingMissingEx; 339 340 c.addAll(validateSupported(ejbcmp)); 341 342 JDOConcreteBeanGenerator cmpGenerator = getCMPGenerator(ejbcmp); 343 MethodHelper mh = new MethodHelper(ejbcmp); 344 c.addAll(cmpGenerator.validate(mh, beanName)); 345 346 if (!c.isEmpty()) { 347 validateex = new StringBuffer (); 351 Iterator iter = c.iterator(); 352 353 while (iter.hasNext()) { 354 Exception ex = (Exception )iter.next(); 355 if (debug) 356 logger.log(Logger.FINE,"validation exception: " , ex); validateex.append(ex.getMessage()).append('\n'); } 359 360 if (!ignoreValidationResults) 361 throw JDOCodeGeneratorHelper.createGeneratorException( 362 "CMG.ExceptionInValidate", beanName, bundle, validateex.toString()); 364 } 365 366 try { 367 Collection newfiles = null; 368 369 if (!ejbcmp.isEJB20()) 370 ejbcmp.setQueryParser(jdoqlParamDeclParser); 371 372 newfiles = cmpGenerator.generate(mh, beanName, srcout, classout); 377 files.addAll(newfiles); 378 379 newfiles = generatePC(ejbcmp, srcout, classout); 380 files.addAll(newfiles); 381 382 if (validateex != null) 383 throw JDOCodeGeneratorHelper.createGeneratorException( 384 "CMG.ExceptionInValidate", beanName, bundle, validateex.toString()); 386 } catch (JDOUserException e) { 387 throw JDOCodeGeneratorHelper.createGeneratorException( 390 "CMG.ExceptionInGenerate", beanName, bundle, e, validateex); 392 393 } catch (EJBQLException e) { 394 throw JDOCodeGeneratorHelper.createGeneratorException( 397 "CMG.ExceptionInGenerate", beanName, bundle, e, validateex); 399 400 } catch (IOException e) { 401 throw JDOCodeGeneratorHelper.createGeneratorException( 404 "CMG.IOExceptionInGenerate", beanName, bundle, e, validateex); 406 } 407 408 } 409 410 413 private Collection generatePC(IASEjbCMPEntityDescriptor ejbcmp, 414 File srcout, File classout) 415 throws IOException { 416 417 ArrayList fileList = new ArrayList(); 418 Main gen = new Main(ejbModel, srcout); 419 String className = nameMapper.getPersistenceClassForEjbName(ejbcmp.getName()); 420 421 if (className != null) { 422 String jvmClassName = className.replace('.', '/'); 426 File file = gen.generate(jvmClassName); 427 fileList.add(file); 428 429 MappingClassElement mappingClass 431 = model.getMappingClass(className); 432 BufferedOutputStream mapOut = null; 433 try { 434 String mapPath = className.replace('.', File.separatorChar); 435 String mappingFile = mapPath + MAPPING_EXTENSION; 436 mapOut = new BufferedOutputStream ( 437 new FileOutputStream (new File (classout, mappingFile))); 438 model.storeMappingClass(mappingClass, mapOut); 442 } finally { 443 if (mapOut != null) { 444 try { 445 mapOut.close(); 446 } catch(Exception ex) { 447 if (logger.isLoggable(Logger.FINE)) 448 logger.fine(ex.getMessage()); 449 } 450 } 451 } 452 453 } 454 return fileList; 455 } 456 457 460 public Collection cleanup() throws GeneratorException { 461 mappingGenerator.cleanup(); 466 467 cmp11Generator = null; 469 cmp20Generator = null; 470 471 return files; 472 } 473 474 478 private JDOConcreteBeanGenerator getCMPGenerator( 479 IASEjbCMPEntityDescriptor descr) throws GeneratorException { 480 481 JDOConcreteBeanGenerator cmpGenerator = null; 482 try { 483 if (descr.isEJB20()) { 484 cmpGenerator = getCMP20Generator(); 485 } else { 486 cmpGenerator = getCMP11Generator(); 487 } 488 489 cmpGenerator.setUpdateable( 490 !descr.getIASEjbExtraDescriptors().isIsReadOnlyBean()); 491 492 } catch (IOException e) { 493 throw JDOCodeGeneratorHelper.createGeneratorException( 495 "CMG.IOExceptionInInit", bundle, e); } 497 498 return cmpGenerator; 499 } 500 501 504 private JDOConcreteBeanGenerator getCMP20Generator() throws IOException { 505 if (cmp20Generator == null) { 506 cmp20Generator = new JDOConcreteBean20Generator(loader, model, 507 nameMapper); 508 509 addSignatures(cmp20Generator); 510 } 511 512 return cmp20Generator; 513 } 514 515 518 private JDOConcreteBeanGenerator getCMP11Generator() throws IOException { 519 if (cmp11Generator == null) { 520 cmp11Generator = new JDOConcreteBean11Generator(loader, model, 521 nameMapper); 522 523 addSignatures(cmp11Generator); 524 } 525 526 return cmp11Generator; 527 } 528 529 532 private void addSignatures(JDOConcreteBeanGenerator cmpGenerator) 533 throws IOException { 534 535 cmpGenerator.addCodeGenInputFilesSignature(getSignaturesOfInputFiles()); 539 540 cmpGenerator.addCodeGeneratorClassSignature( 542 getS1ASSpecificGeneratorClassesSignature()); 543 } 544 545 550 private static synchronized String 551 getS1ASSpecificGeneratorClassesSignature() 552 { 553 if (signatures == null) { 554 StringBuffer sb = new StringBuffer (). 555 556 append(JDOCodeGenerator.SIGNATURE). 558 append(CMPTemplateFormatter.signatureDelimiter_). 559 560 append(NameMapper.SIGNATURE); 562 563 signatures = sb.toString(); 564 } 565 return signatures; 566 } 567 568 574 private String getSignaturesOfInputFiles() throws IOException 575 { 576 StringBuffer sb = new StringBuffer (). 577 578 append(getFileInfoOfInputFile(inputFilesPath + 579 File.separator + DescriptorConstants.EJB_JAR_ENTRY)). 580 append(CMPTemplateFormatter.signatureDelimiter_). 581 582 append(getFileInfoOfInputFile(inputFilesPath + 583 File.separator + DescriptorConstants.S1AS_EJB_JAR_ENTRY)). 584 append(CMPTemplateFormatter.signatureDelimiter_). 585 586 append(getFileInfoOfInputFile(inputFilesPath + 587 File.separator + DescriptorConstants.S1AS_CMP_MAPPING_JAR_ENTRY)); 588 589 return sb.toString(); 590 } 591 592 600 private String getFileInfoOfInputFile(String pathname) throws IOException 601 { 602 File inputFile = new File (pathname); 603 604 StringBuffer inputFileInfo = new StringBuffer (). 605 append(inputFile.getCanonicalPath().replace('\\', '/')). 606 append(CMPTemplateFormatter.space_). 607 append(String.valueOf(inputFile.length())). 608 append(" bytes"); 609 610 return inputFileInfo.toString(); 611 } 612 613 623 private void loadOrCreateMappingClasses(boolean ignoreSunDeploymentDescriptors) 624 throws IOException , GeneratorException { 625 626 try { 627 SchemaElement schema = mappingGenerator.generateMapping( 628 ejbcContext, inputFilesPath, generatedXmlsPath, classout, 629 ignoreSunDeploymentDescriptors); 630 if (ejbcContext != null 632 && mappingGenerator.isJavaToDatabase()) { 633 createDDLs(schema, mappingGenerator.getDatabaseVendorName(), null); 634 } 635 } catch (SQLException ex) { 636 throw JDOCodeGeneratorHelper.createGeneratorException( 638 "CMG.SQLException", bundle, ex); 639 } catch (DBException ex) { 640 throw JDOCodeGeneratorHelper.createGeneratorException( 642 "CMG.DBException", bundle, ex); 644 } catch (ModelException ex) { 645 throw JDOCodeGeneratorHelper.createGeneratorException( 647 "CMG.ModelException", bundle, ex); 649 } catch (Schema2BeansException ex) { 650 throw JDOCodeGeneratorHelper.createGeneratorException( 652 "CMG.Schema2BeansException", bundle, ex); } catch (ConversionException ex) { 654 throw JDOCodeGeneratorHelper.createGeneratorException( 656 "CMG.MappingConversionException", bundle, ex); } 658 } 659 660 668 private void createDDLs(SchemaElement schema, String dbVendorName, Connection conn) 669 throws IOException , DBException { 670 671 File fileDir = new File (generatedXmlsPath); 674 675 if (!fileDir.exists()) { 676 fileDir.mkdirs(); 677 } 678 679 String namePrefix = DeploymentHelper.getDDLNamePrefix(bundle); 683 684 OutputStream createDDLSql = new FileOutputStream (new File (generatedXmlsPath, 686 namePrefix + NAME_SEPARATOR + dbVendorName + CREATE_SQL_FILE_SUFFIX)); 687 OutputStream dropDDLSql = new FileOutputStream (new File (generatedXmlsPath, 688 namePrefix + NAME_SEPARATOR + dbVendorName + DROP_SQL_FILE_SUFFIX)); 689 690 OutputStream dropDDLTxt = new FileOutputStream (new File (classout, 691 namePrefix + DROP_DDL_JDBC_FILE_SUFFIX)); 692 OutputStream createDDLTxt = new FileOutputStream (new File (classout, 693 namePrefix + CREATE_DDL_JDBC_FILE_SUFFIX)); 694 695 try { 696 OutputStream dbStream = null; 702 boolean createTablesAtDeploy = false; 703 boolean dropTablesAtDeploy = false; 704 if ((conn != null) && (createTablesAtDeploy || dropTablesAtDeploy)) { 705 dbStream = new DatabaseOutputStream(conn); 706 } 707 709 DDLGenerator.generateDDL(schema, dbVendorName, createDDLSql, 710 dropDDLSql, dropDDLTxt, createDDLTxt, dbStream, dropTablesAtDeploy); 711 } catch (SQLException ex) { 712 if (logger.isLoggable(Logger.WARNING)) 713 logger.warning(ex.toString()); 714 } 715 } 716 717 } 718 | Popular Tags |