1 23 24 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 25 26 import com.sun.enterprise.connectors.ConnectorRuntime; 27 import com.sun.enterprise.deployment.Application; 28 import com.sun.enterprise.deployment.EjbBundleDescriptor; 29 import com.sun.enterprise.deployment.PersistenceUnitsDescriptor; 30 import com.sun.enterprise.deployment.PersistenceUnitDescriptor; 31 import com.sun.enterprise.deployment.WebBundleDescriptor; 32 import com.sun.enterprise.deployment.backend.DeploymentEventInfo; 33 import com.sun.enterprise.deployment.backend.DeploymentStatus; 34 import com.sun.enterprise.deployment.io.DescriptorConstants; 35 import com.sun.enterprise.loader.InstrumentableClassLoader; 36 import com.sun.enterprise.server.PersistenceUnitInfoImpl; 37 import com.sun.enterprise.server.Constants; 38 39 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 40 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants; 41 import com.sun.jdo.spi.persistence.utility.I18NHelper; 42 43 import java.io.File ; 44 import java.io.IOException ; 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.logging.Level ; 50 51 import javax.naming.NamingException ; 52 import javax.persistence.EntityManagerFactory; 53 import javax.persistence.spi.PersistenceUnitInfo; 54 import javax.persistence.spi.PersistenceProvider; 55 import java.sql.Connection ; 56 import javax.sql.DataSource ; 57 import java.sql.Statement ; 58 import java.sql.SQLException ; 59 60 import oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider; 61 62 69 public class PersistenceProcessor 70 extends BaseProcessor { 71 72 private static final String TOPLINK_PERSISTENCE_PROVIDER_CLASS_NAME_OLD = 75 "oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider"; private static final String TOPLINK_PERSISTENCE_PROVIDER_CLASS_NAME_NEW = 77 "oracle.toplink.essentials.PersistenceProvider"; 79 83 private PersistenceUnitInfo pi; 84 85 97 public PersistenceProcessor( 98 DeploymentEventInfo info, boolean deploy, boolean redeploy, 99 String cliCreateTables, String cliDropAndCreateTables, String cliDropTables) { 100 super(info, deploy, redeploy, cliCreateTables, 101 cliDropAndCreateTables, cliDropTables); 102 } 103 104 109 protected void processApplication() { 110 Collection <PersistenceUnitsDescriptor> bundleCollection = 111 getAllPersistenceUnitDescriptors(application); 112 113 if ( bundleCollection.size() == 0) { 114 return; 115 } 116 117 setApplicationLocation(); 121 setGeneratedLocation(); 122 123 for (PersistenceUnitsDescriptor pus : bundleCollection) { 124 for (PersistenceUnitDescriptor bundle : pus.getPersistenceUnitDescriptors()) { 125 processAppBundle(bundle); 126 } 127 } 128 } 129 130 138 private Collection <PersistenceUnitsDescriptor> 139 getAllPersistenceUnitDescriptors(Application application) { 140 List <PersistenceUnitsDescriptor> allPUs = 141 new ArrayList <PersistenceUnitsDescriptor>( 142 application.getPersistenceUnitsDescriptors()); 143 144 for (Object o : application.getEjbBundleDescriptors()) { 146 EjbBundleDescriptor bundle = EjbBundleDescriptor.class.cast(o); 147 allPUs.addAll(bundle.getPersistenceUnitsDescriptors()); 148 } 149 150 for (Object o : application.getWebBundleDescriptors()) { 152 WebBundleDescriptor bundle = WebBundleDescriptor.class.cast(o); 153 allPUs.addAll(bundle.getPersistenceUnitsDescriptors()); 154 } 155 156 return allPUs; 157 } 158 159 165 private void processAppBundle(PersistenceUnitDescriptor bundle) { 166 String ddlGenerate = getPersistencePropVal(bundle, 167 EntityManagerFactoryProvider.DDL_GENERATION, 168 EntityManagerFactoryProvider.NONE); 169 boolean createTables = 170 getCreateTablesValue(bundle, ddlGenerate); 171 boolean dropTables = 172 getDropTablesValue(bundle, ddlGenerate); 173 174 if (debug) { 175 logger.fine("ejb.PersistenceProcessor.createanddroptables", new Object [] {new Boolean (createTables), new Boolean (dropTables)}); 177 } 178 179 if (!createTables && !dropTables) { 180 return; 182 } 183 184 constructJdbcFileNames(bundle); 185 if (debug) { 186 logger.fine("ejb.PersistenceProcessor.createanddropfilenames", createJdbcFileName, dropJdbcFileName); } 188 189 dropTablesFromDB(dropTables, bundle); 190 createTablesInDB(createTables, bundle); 191 } 192 193 203 protected boolean getCreateTablesValue( 204 PersistenceUnitDescriptor bundle, String ddlGenerate) { 205 boolean createTables = 206 deploy 207 && (cliCreateTables.equals(Constants.TRUE) 208 || (ddlGenerate.equals(EntityManagerFactoryProvider.CREATE_ONLY) 209 || ddlGenerate.equals(EntityManagerFactoryProvider.DROP_AND_CREATE) 210 && cliCreateTables.equals(Constants.UNDEFINED))); 211 212 return createTables; 213 } 214 215 227 protected boolean getDropTablesValue( 228 PersistenceUnitDescriptor bundle, String ddlGenerate) { 229 boolean dropTables = 230 (redeploy 231 && (cliDropAndCreateTables.equals(Constants.TRUE) 232 || (ddlGenerate.equals(EntityManagerFactoryProvider.DROP_AND_CREATE) 233 && cliDropAndCreateTables.equals(Constants.UNDEFINED)))) 234 || (!deploy 235 && (cliDropTables.equals(Constants.TRUE) 236 || (ddlGenerate.equals(EntityManagerFactoryProvider.DROP_AND_CREATE) 237 && cliDropTables.equals(Constants.UNDEFINED)))); 238 239 return dropTables; 240 } 241 242 251 private void constructJdbcFileNames(PersistenceUnitDescriptor parBundle) { 252 createJdbcFileName = 253 getPersistencePropVal(parBundle, 254 EntityManagerFactoryProvider.CREATE_JDBC_DDL_FILE, null); 255 dropJdbcFileName = 256 getPersistencePropVal(parBundle, 257 EntityManagerFactoryProvider.DROP_JDBC_DDL_FILE, null); 258 259 if((null != createJdbcFileName) && (null != dropJdbcFileName)) { 260 return; 261 } 262 263 String filePrefix = 264 EJBHelper.getDDLNamePrefix(parBundle.getParent().getParent()); 265 266 if(null == createJdbcFileName) { 267 createJdbcFileName = filePrefix + NAME_SEPARATOR + parBundle.getName() + 268 CREATE_DDL_JDBC_FILE_SUFFIX; 269 } 270 if(null == dropJdbcFileName) { 271 dropJdbcFileName = filePrefix + NAME_SEPARATOR + parBundle.getName() + 272 DROP_DDL_JDBC_FILE_SUFFIX; 273 } 274 } 275 276 292 protected boolean dropTablesFromDB(boolean dropTables, 293 PersistenceUnitDescriptor bundle) { 294 boolean dropResult = false; 295 File dropFile = null; 296 297 if(dropTables) { 298 dropFile = getDropDDLFile(dropJdbcFileName, deploy); 299 if (dropFile.exists()) { 300 PersistenceUnitInfo pi = new PersistenceUnitInfoImpl( 301 bundle, 302 appDeployedLocation, 303 null); 304 dropResult = executeDDLStatement(dropFile, 305 pi.getNonJtaDataSource()); 306 } 307 } 308 return dropResult; 309 } 310 311 321 protected boolean createTablesInDB(boolean createTables, 322 PersistenceUnitDescriptor bundle) { 323 boolean createResult = false; 324 if(createTables) { 325 pi = loadPersistenceUnitBundle(bundle, 326 createJdbcFileName, dropJdbcFileName); 327 328 if (null == pi) { 331 return createResult; 332 } 333 334 File createFile = getCreateDDLFile( 335 appGeneratedLocation + createJdbcFileName); 336 if(createFile.exists()) { 337 createResult = executeDDLStatement( createFile, 338 pi.getNonJtaDataSource()); 339 } 340 createResult = true; 341 } 342 return createResult; 343 } 344 345 346 356 private boolean executeDDLStatement(File fileName, DataSource nonJtaDataSource ) { 357 boolean result = false; 358 Connection conn = null; 359 Statement sql = null; 360 try { 361 try { 362 conn = nonJtaDataSource.getConnection(); 363 sql = conn.createStatement(); 364 result = true; 365 } catch (SQLException ex) { 366 logI18NWarnMessage( 367 "ejb.BaseProcessor.cannotConnect", 368 appRegisteredName, null, ex); 369 } 370 if(result) { 371 executeDDLs(fileName, sql); 372 } 373 } catch (IOException e) { 374 fileIOError(application.getRegistrationName(), e); 375 } finally { 376 closeConn(conn); 377 } 378 return result; 379 } 380 381 382 389 private void addPropertiesToPU(PersistenceUnitDescriptor puDescriptor) { 390 addPropertyToDescriptor(puDescriptor, 391 EntityManagerFactoryProvider.APP_LOCATION, 392 appGeneratedLocation); 393 addPropertyToDescriptor(puDescriptor, 394 EntityManagerFactoryProvider.DDL_GENERATION, 395 EntityManagerFactoryProvider.DROP_AND_CREATE); 396 addPropertyToDescriptor(puDescriptor, 401 EntityManagerFactoryProvider.DDL_GENERATION_MODE, 402 EntityManagerFactoryProvider.DDL_SQL_SCRIPT_GENERATION); 403 addPropertyToDescriptor(puDescriptor, 404 EntityManagerFactoryProvider.CREATE_JDBC_DDL_FILE, 405 createJdbcFileName); 406 addPropertyToDescriptor(puDescriptor, 407 EntityManagerFactoryProvider.DROP_JDBC_DDL_FILE, 408 dropJdbcFileName); 409 } 410 411 417 private void addPropertyToDescriptor(PersistenceUnitDescriptor descriptor, 418 String propertyName, String propertyValue) { 419 String oldPropertyValue = descriptor.getProperties().getProperty(propertyName); 420 if(null == oldPropertyValue) { 421 descriptor.addProperty(propertyName, propertyValue); 422 } 423 } 424 425 439 private PersistenceUnitInfo loadPersistenceUnitBundle( 440 PersistenceUnitDescriptor persistenceUnitDescriptor, 441 String createJdbcFileName, String dropJdbcFileName) { 442 logger.entering("loadPersistenceUnitBundle", "load", 443 new Object []{persistenceUnitDescriptor}); 444 445 if (! isSupportedPersistenceProvider(persistenceUnitDescriptor)) { 446 return null; 448 } 449 450 PersistenceProvider provider; 451 addPropertiesToPU(persistenceUnitDescriptor); 452 453 final InstrumentableClassLoader cl = 454 InstrumentableClassLoader.class.cast( 455 persistenceUnitDescriptor.getClassLoader()); 456 PersistenceUnitInfo pi = new PersistenceUnitInfoImpl( 457 persistenceUnitDescriptor, 458 appDeployedLocation, 459 cl); 460 461 if(debug) 462 logger.fine("PersistenceInfo for PU is :\n" + pi); 463 464 provider = new EntityManagerFactoryProvider(); 465 EntityManagerFactory emf = null; 466 try { 467 emf = provider.createContainerEntityManagerFactory(pi, null); 468 emf.createEntityManager(); 469 if(debug) 470 logger.fine("PersistenceProcessor", "loadPersistenceUnitBundle", 471 "emf = {0}", emf); 472 } finally { 473 if(emf != null) { 474 emf.close(); 475 } 476 } 477 return pi; 478 } 479 480 491 private String getPersistencePropVal(PersistenceUnitDescriptor parBundle, 492 String propertyName, String defaultValue) { 493 String propertyValue; 494 if(null == defaultValue) 495 propertyValue = parBundle.getProperties().getProperty(propertyName); 496 else 497 propertyValue = parBundle.getProperties(). 498 getProperty(propertyName, defaultValue); 499 500 return propertyValue; 501 } 502 503 511 private boolean isSupportedPersistenceProvider( 512 final PersistenceUnitDescriptor persistenceUnitDescriptor) { 513 String providerClassName = 514 PersistenceUnitInfoImpl.getPersistenceProviderClassNameForPuDesc(persistenceUnitDescriptor); 515 boolean result = providerClassName.equals(TOPLINK_PERSISTENCE_PROVIDER_CLASS_NAME_OLD) || 516 providerClassName.equals(TOPLINK_PERSISTENCE_PROVIDER_CLASS_NAME_NEW); 517 if (!result) { 518 logI18NInfoMessage( 519 "ejb.PersistenceProcessor.nondefaultprovider", 520 TOPLINK_PERSISTENCE_PROVIDER_CLASS_NAME_OLD, 521 providerClassName, null); 522 } 523 return result; 524 } 525 526 } 527 | Popular Tags |