1 21 package oracle.toplink.essentials.internal.ejb.cmp3; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import java.lang.reflect.Constructor ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 31 import javax.persistence.spi.PersistenceUnitInfo; 32 import javax.persistence.spi.ClassTransformer; 33 import javax.persistence.PersistenceException; 34 35 import oracle.toplink.essentials.config.TopLinkProperties; 36 import oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider; 37 import oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor; 38 import oracle.toplink.essentials.internal.databaseaccess.DatasourcePlatform; 39 import oracle.toplink.essentials.internal.ejb.cmp3.base.PropertiesHandler; 40 import oracle.toplink.essentials.internal.weaving.TransformerFactory; 41 import oracle.toplink.essentials.jndi.JNDIConnector; 42 import oracle.toplink.essentials.logging.AbstractSessionLog; 43 import oracle.toplink.essentials.logging.SessionLog; 44 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 45 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 46 import oracle.toplink.essentials.internal.sessions.AbstractSession; 47 import oracle.toplink.essentials.sequencing.Sequence; 48 import oracle.toplink.essentials.sessions.*; 49 import oracle.toplink.essentials.threetier.ReadConnectionPool; 50 import oracle.toplink.essentials.threetier.ServerSession; 51 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor; 52 import oracle.toplink.essentials.tools.sessionmanagement.SessionManager; 53 import oracle.toplink.essentials.descriptors.ClassDescriptor; 54 import oracle.toplink.essentials.internal.ejb.cmp3.base.CMP3Policy; 55 import oracle.toplink.essentials.platform.server.CustomServerPlatform; 56 import oracle.toplink.essentials.platform.server.ServerPlatform; 57 import oracle.toplink.essentials.exceptions.*; 58 import oracle.toplink.essentials.internal.helper.EJB30ConversionManager; 59 import javax.persistence.spi.PersistenceUnitTransactionType; 60 61 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.DataSourceImpl; 62 import oracle.toplink.essentials.tools.sessionconfiguration.DescriptorCustomizer; 63 import oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer; 64 import oracle.toplink.essentials.internal.security.SecurableObjectHolder; 65 66 70 public class EntityManagerSetupImpl { 71 protected MetadataProcessor processor = null; 72 protected PersistenceUnitInfo persistenceUnitInfo = null; 73 protected Map predeployProperties = null; 74 protected int deploymentCount = 0; 76 protected ServerSession session = null; 77 protected boolean isInContainerMode = false; 78 protected boolean enableLazyForOneToOne = false; 80 protected SecurableObjectHolder securableObjectHolder = new SecurableObjectHolder(); 81 82 public static final String STATE_INITIAL = "Initial"; 83 public static final String STATE_PREDEPLOYED = "Predeployed"; 84 public static final String STATE_DEPLOYED = "Deployed"; 85 public static final String STATE_UNDEPLOYED = "Undeployed"; 86 87 protected String state = STATE_INITIAL; 88 89 public static final String ERROR_LOADING_XML_FILE = "error_loading_xml_file"; 90 public static final String EXCEPTION_LOADING_ENTITY_CLASS = "exception_loading_entity_class"; 91 92 96 protected void removeSessionFromGlobalSessionManager() { 97 if (session != null){ 98 if(session.isConnected()) { 99 session.logout(); 100 } 101 SessionManager.getManager().getSessions().remove(session.getName()); 102 } 103 } 104 105 115 public Set buildPersistentClassSetFromXMLDocuments(ClassLoader loader, PersistenceUnitInfo persistenceUnitInfo) { 116 List<String > mappingFileNames = getORMXMLFileNames(loader); 117 HashSet classSet = new HashSet(); 118 119 Iterator<String > fileNames = mappingFileNames.iterator(); 120 InputStream stream = null; 121 String fileName = null; 122 while (fileNames.hasNext()){ 123 try{ 124 fileName = fileNames.next(); 125 stream = PersistenceUnitProcessor.createInputStreamForFileInPersistenceUnit(fileName, persistenceUnitInfo, loader); 126 if (stream != null){ 127 classSet.addAll(MetadataProcessor.buildClassSet(stream, fileName, loader)); 128 } 129 } catch (IOException exception){ 130 handleORMException(PersistenceUnitLoadingException.exceptionLoadingORMXML(fileName, exception), session, fileName); 131 } finally { 132 try{ 133 if (stream != null){ 134 stream.close(); 135 } 136 } catch (IOException e){}; 137 } 138 } 139 return classSet; 140 } 141 142 154 public Collection buildEntityList(PersistenceUnitInfo info, ClassLoader loader) { 155 Set<String > classNames = PersistenceUnitProcessor.buildPersistentClassSet(info, loader); 156 157 classNames.addAll(buildPersistentClassSetFromXMLDocuments(loader, info)); 159 160 Vector entityList = new Vector(); 161 Iterator i = classNames.iterator(); 162 String className = null; 163 while (i.hasNext()){ 164 try{ 165 className = (String )i.next(); 166 Class entityClass = loader.loadClass(className); 167 entityList.add(entityClass); 168 } catch (ClassNotFoundException exc){ 169 AbstractSessionLog.getLog().log(SessionLog.CONFIG, "exception_loading_entity_class", className, exc); 170 } 171 } 172 return entityList; 173 } 174 175 193 public synchronized ServerSession deploy(ClassLoader realClassLoader, Map additionalProperties) { 194 if(state != STATE_PREDEPLOYED && state != STATE_DEPLOYED) { 195 throw new PersistenceException(EntityManagerSetupException.cannotDeployWithoutPredeploy(persistenceUnitInfo.getPersistenceUnitName())); 196 } 197 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "deploy_begin", new Object []{getPersistenceUnitInfo().getPersistenceUnitName(), state, deploymentCount}); 198 try { 199 Map deployProperties = EntityManagerFactoryProvider.mergeMaps(additionalProperties, predeployProperties); 200 EntityManagerFactoryProvider.translateOldProperties(deployProperties, session); 201 202 if(state == STATE_PREDEPLOYED) { 203 Collection entities = buildEntityList(persistenceUnitInfo, realClassLoader); 204 205 session.getProject().convertClassNamesToClasses(realClassLoader); 207 208 processor.setClassLoader(realClassLoader); 210 processor.addEntityListeners(); 211 processor.addNamedQueries(); 212 processor = null; 213 214 initServerSession(deployProperties); 215 216 if (session.getIntegrityChecker().hasErrors()){ 217 session.handleException(new IntegrityException(session.getIntegrityChecker())); 218 } 219 220 session.getDatasourcePlatform().getConversionManager().setLoader(realClassLoader); 221 } 222 deploymentCount++; 223 state = STATE_DEPLOYED; 224 try{ 225 updateServerSession(deployProperties); 226 if(!session.isConnected()) { 227 if(isValidationOnly(deployProperties, false)) { 228 session.initializeDescriptors(); 229 } else { 230 EntityManagerFactoryProvider.login(session, deployProperties); 231 EntityManagerFactoryProvider.generateDDLFiles(session, deployProperties, !isInContainerMode); 232 } 233 } 234 } catch (RuntimeException exception) { 235 cleanUpSessionManager(); 236 throw exception; 237 } 238 return session; 239 } catch (oracle.toplink.essentials.exceptions.ValidationException exception) { 240 throw new javax.persistence.PersistenceException(exception); 241 } finally { 242 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "deploy_end", new Object []{getPersistenceUnitInfo().getPersistenceUnitName(), state, deploymentCount}); 243 if(state == STATE_UNDEPLOYED) { 244 session = null; 245 } 246 } 247 } 248 249 250 254 protected void addProjectToSession(ServerSession session, Project project) { 255 DatasourcePlatform sessionPlatform = (DatasourcePlatform)session.getDatasourceLogin().getDatasourcePlatform(); 256 DatasourcePlatform projectPlatform = (DatasourcePlatform)project.getDatasourceLogin().getDatasourcePlatform(); 257 if (!sessionPlatform.hasDefaultSequence() && projectPlatform.hasDefaultSequence()) { 258 sessionPlatform.setDefaultSequence(projectPlatform.getDefaultSequence()); 259 } 260 if ((sessionPlatform.getSequences() == null) || sessionPlatform.getSequences().isEmpty()) { 261 if ((projectPlatform.getSequences() != null) && !projectPlatform.getSequences().isEmpty()) { 262 sessionPlatform.setSequences(projectPlatform.getSequences()); 263 } 264 } else { 265 if ((projectPlatform.getSequences() != null) && !projectPlatform.getSequences().isEmpty()) { 266 Iterator itProjectSequences = projectPlatform.getSequences().values().iterator(); 267 while (itProjectSequences.hasNext()) { 268 Sequence sequence = (Sequence)itProjectSequences.next(); 269 if (!sessionPlatform.getSequences().containsKey(sequence.getName())) { 270 sessionPlatform.addSequence(sequence); 271 } 272 } 273 } 274 } 275 session.addDescriptors(project); 276 } 277 278 282 protected void addSessionToGlobalSessionManager() { 283 AbstractSession oldSession = (AbstractSession)SessionManager.getManager().getSessions().get(session.getName()); 284 if(oldSession != null) { 285 throw new PersistenceException(EntityManagerSetupException.attemptedRedeployWithoutClose(session.getName())); 286 } 287 SessionManager.getManager().addSession(session); 288 } 289 290 294 protected void assignCMP3Policy() { 295 Project project = session.getProject(); 297 for (Iterator iterator = project.getDescriptors().values().iterator(); iterator.hasNext();){ 298 ClassDescriptor descriptor = (ClassDescriptor)iterator.next(); 300 301 if(descriptor.getCMPPolicy() == null) { 302 descriptor.setCMPPolicy(new CMP3Policy()); 303 } 304 } 305 } 306 307 311 protected void updateServerPlatform(Map m) { 312 String serverPlatformClassName = (String )PropertiesHandler.getPropertyValueLogDebug(TopLinkProperties.TARGET_SERVER, m, session); 313 if (serverPlatformClassName == null) { 314 return; 315 } 316 317 ServerPlatform serverPlatform = null; 318 Class cls = findClassForProperty(serverPlatformClassName, TopLinkProperties.TARGET_SERVER); 319 try { 320 Constructor constructor = cls.getConstructor(new Class []{oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.class}); 321 serverPlatform = (ServerPlatform)constructor.newInstance(new Object []{session}); 322 } catch (Exception ex) { 323 if(ExternalTransactionController.class.isAssignableFrom(cls)) { 324 CustomServerPlatform customServerPlatform = new CustomServerPlatform(session); 325 customServerPlatform.setExternalTransactionControllerClass(cls); 326 serverPlatform = customServerPlatform; 327 } else { 328 throw EntityManagerSetupException.failedToInstantiateServerPlatform(serverPlatformClassName, TopLinkProperties.TARGET_SERVER, ex); 329 } 330 } 331 332 if(!session.getLogin().shouldUseExternalTransactionController()) { 333 serverPlatform.disableJTA(); 334 } 335 session.setServerPlatform(serverPlatform); 336 337 setNewLog(serverPlatform.getServerLog()); 339 } 340 341 protected void setNewLog(SessionLog newLog) { 342 if(session.getSessionLog() == newLog) { 343 return; 344 } 345 346 newLog.setLevel(session.getSessionLog().getLevel()); 347 newLog.setShouldPrintDate(session.getSessionLog().shouldPrintDate()); 348 newLog.setShouldPrintThread(session.getSessionLog().shouldPrintThread()); 349 newLog.setShouldPrintSession(session.getSessionLog().shouldPrintSession()); 350 newLog.setShouldLogExceptionStackTrace(session.getSessionLog().shouldLogExceptionStackTrace()); 351 352 session.setSessionLog(newLog); 353 } 354 355 protected static Class findClass(String className) throws ClassNotFoundException , PrivilegedActionException { 356 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 357 return (Class )AccessController.doPrivileged(new PrivilegedClassForName(className)); 358 } else { 359 return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(className); 360 } 361 } 362 363 protected static Class findClassForProperty(String className, String propertyName) { 364 try { 365 return findClass(className); 366 } catch (PrivilegedActionException exception1) { 367 throw EntityManagerSetupException.classNotFoundForProperty(className, propertyName, exception1.getException()); 368 } catch (ClassNotFoundException exception2) { 369 throw EntityManagerSetupException.classNotFoundForProperty(className, propertyName, exception2); 370 } 371 } 372 373 protected void updateDescriptorCacheSettings(Map m) { 374 Map typeMap = PropertiesHandler.getPrefixValuesLogDebug(TopLinkProperties.CACHE_TYPE_, m, session); 375 Map sizeMap = PropertiesHandler.getPrefixValuesLogDebug(TopLinkProperties.CACHE_SIZE_, m, session); 376 Map sharedMap = PropertiesHandler.getPrefixValuesLogDebug(TopLinkProperties.CACHE_SHARED_, m, session); 377 if(typeMap.isEmpty() && sizeMap.isEmpty() && sharedMap.isEmpty()) { 378 return; 379 } 380 381 boolean hasDefault = false; 382 383 String defaultTypeName = (String )typeMap.remove(TopLinkProperties.DEFAULT); 384 Class defaultType = null; 385 if(defaultTypeName != null) { 386 defaultType = findClassForProperty(defaultTypeName, TopLinkProperties.CACHE_TYPE_DEFAULT); 387 hasDefault = true; 388 } 389 390 String defaultSizeString = (String )sizeMap.remove(TopLinkProperties.DEFAULT); 391 Integer defaultSize = null; 392 if(defaultSizeString != null) { 393 defaultSize = Integer.parseInt(defaultSizeString); 394 hasDefault = true; 395 } 396 397 String defaultSharedString = (String )sharedMap.remove(TopLinkProperties.CACHE_SHARED_DEFAULT); 398 Boolean defaultShared = null; 399 if(defaultSharedString != null) { 400 defaultShared = Boolean.parseBoolean(defaultSharedString); 401 hasDefault = true; 402 } 403 404 Iterator it = session.getDescriptors().values().iterator(); 405 while (it.hasNext() && (hasDefault || !typeMap.isEmpty() || !sizeMap.isEmpty() || !sharedMap.isEmpty())) { 406 ClassDescriptor descriptor = (ClassDescriptor)it.next(); 407 408 if(descriptor.isAggregateDescriptor() || descriptor.isAggregateCollectionDescriptor()) { 409 continue; 410 } 411 412 String entityName = descriptor.getAlias(); 413 String className = descriptor.getJavaClass().getName(); 414 String name; 415 416 Class type = defaultType; 417 name = entityName; 418 String typeName = (String )typeMap.remove(name); 419 if(typeName == null) { 420 name = className; 421 typeName = (String )typeMap.remove(name); 422 } 423 if(typeName != null) { 424 type = findClassForProperty(typeName, TopLinkProperties.CACHE_TYPE_ + name); 425 } 426 if(type != null) { 427 descriptor.setIdentityMapClass(type); 428 } 429 430 Integer size = defaultSize; 431 name = entityName; 432 String sizeString = (String )sizeMap.remove(name); 433 if(sizeString == null) { 434 name = className; 435 sizeString = (String )sizeMap.remove(name); 436 } 437 if(sizeString != null) { 438 size = Integer.parseInt(sizeString); 439 } 440 if(size != null) { 441 descriptor.setIdentityMapSize(size.intValue()); 442 } 443 444 Boolean shared = defaultShared; 445 name = entityName; 446 String sharedString = (String )sharedMap.remove(name); 447 if(sharedString == null) { 448 name = className; 449 sharedString = (String )sharedMap.remove(name); 450 } 451 if(sharedString != null) { 452 shared = Boolean.parseBoolean(sharedString); 453 } 454 if(shared != null) { 455 descriptor.setIsIsolated(!shared.booleanValue()); 456 } 457 } 458 } 459 460 468 public ClassTransformer predeploy(PersistenceUnitInfo info, Map extendedProperties) { 469 if(state == STATE_INITIAL) { 470 persistenceUnitInfo = info; 471 } 472 ClassLoader privateClassLoader = persistenceUnitInfo.getNewTempClassLoader(); 473 predeployProperties = EntityManagerFactoryProvider.mergeMaps(extendedProperties, persistenceUnitInfo.getProperties()); 474 475 initOrUpdateLogging(true, predeployProperties, AbstractSessionLog.getLog()); 477 Collection entities = buildEntityList(persistenceUnitInfo, privateClassLoader); 479 480 session = new ServerSession(new Project(new DatabaseLogin())); 481 EntityManagerFactoryProvider.translateOldProperties(predeployProperties, session); 482 initOrUpdateLogging(true, predeployProperties, session.getSessionLog()); 483 session.getPlatform().setConversionManager(new EJB30ConversionManager()); 484 485 if(!isValidationOnly(predeployProperties, false) && persistenceUnitInfo != null && persistenceUnitInfo.getTransactionType() == PersistenceUnitTransactionType.JTA) { 486 if(persistenceUnitInfo.getJtaDataSource() == null) { 487 throw new PersistenceException(EntityManagerSetupException.jtaPersistenceUnitInfoMissingJtaDataSource(persistenceUnitInfo.getPersistenceUnitName())); 488 } 489 } 490 491 if(state == STATE_INITIAL ) { 493 enableLazyForOneToOne = true; 494 String weaving = getConfigPropertyAsString(TopLinkProperties.WEAVING); 495 if (weaving != null && weaving.equalsIgnoreCase("false")) { 496 enableLazyForOneToOne = false; 497 } 498 } 499 500 processORMetadata(privateClassLoader, session, entities, enableLazyForOneToOne); 502 503 session.getProject().getLogin().setConnector(new DefaultConnector()); 505 506 if (session.getIntegrityChecker().hasErrors()){ 507 session.handleException(new IntegrityException(session.getIntegrityChecker())); 508 } 509 510 ClassTransformer transformer = null; 513 if (enableLazyForOneToOne){ 514 transformer = TransformerFactory.createTransformerAndModifyProject(session, entities, privateClassLoader); 515 } 516 517 state = STATE_PREDEPLOYED; 518 return transformer; 519 } 520 521 525 public ServerSession getServerSession(Map m) { 526 updateServerSession(m); 527 return session; 528 } 529 530 538 public String getConfigPropertyAsString(String propertyKey, String defaultValue){ 539 return EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(propertyKey, predeployProperties, defaultValue, session); 540 } 541 542 public String getConfigPropertyAsString(String propertyKey){ 543 return EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(propertyKey, predeployProperties, session); 544 } 545 546 551 public String getDeployedSessionName(){ 552 return session != null ? session.getName() : null; 553 } 554 555 public PersistenceUnitInfo getPersistenceUnitInfo(){ 556 return persistenceUnitInfo; 557 } 558 559 public boolean isValidationOnly(Map m) { 560 return isValidationOnly(m, true); 561 } 562 563 protected boolean isValidationOnly(Map m, boolean shouldMergeMap) { 564 if(shouldMergeMap) { 565 m = mergeWithExistingMap(m); 566 } 567 String validationOnlyString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerFactoryProvider.TOPLINK_VALIDATION_ONLY_PROPERTY, m, session); 568 if(validationOnlyString != null) { 569 return Boolean.parseBoolean(validationOnlyString); 570 } else { 571 return false; 572 } 573 } 574 575 public boolean shouldGetSessionOnCreateFactory(Map m) { 576 m = mergeWithExistingMap(m); 577 return isValidationOnly(m, false); 578 } 579 580 protected Map mergeWithExistingMap(Map m) { 581 if(predeployProperties != null) { 582 return EntityManagerFactoryProvider.mergeMaps(m, predeployProperties); 583 } else if(persistenceUnitInfo != null) { 584 return EntityManagerFactoryProvider.mergeMaps(m, persistenceUnitInfo.getProperties()); 585 } else { 586 return m; 587 } 588 } 589 590 public boolean isInContainerMode(){ 591 return isInContainerMode; 592 } 593 594 597 protected void handleORMException(RuntimeException e, AbstractSession session, String mappingFileResourceName){ 598 String throwXMLExceptions = getConfigPropertyAsString(EntityManagerFactoryProvider.TOPLINK_ORM_THROW_EXCEPTIONS, "true"); 599 if (throwXMLExceptions == null || throwXMLExceptions.equalsIgnoreCase("false")) { 601 session.log(SessionLog.CONFIG, SessionLog.EJB_ORM, ERROR_LOADING_XML_FILE, new Object [] {mappingFileResourceName, e}); 602 } else { 603 session.handleException(e); 605 } 606 } 607 608 private List<String > getORMXMLFileNames(ClassLoader loader){ 609 ArrayList<String > list = new ArrayList<String >(); 610 String ormXMLFile = "META-INF/orm.xml"; 611 InputStream stream = null; 612 try { 613 stream = PersistenceUnitProcessor.createInputStreamForFileInPersistenceUnit(ormXMLFile, persistenceUnitInfo, loader); 614 if (stream != null){ 615 list.add(ormXMLFile); 616 } 617 } catch (IOException e){ 618 } finally { 619 try{ 620 if (stream != null){ 621 stream.close(); 622 } 623 } catch (IOException e) {} 624 } 625 626 if (persistenceUnitInfo != null) { 627 if (persistenceUnitInfo.getMappingFileNames() != null) { 628 Iterator mappingFiles = persistenceUnitInfo.getMappingFileNames().iterator();{ 629 while (mappingFiles.hasNext()){ 630 list.add((String )mappingFiles.next()); 631 } 632 } 633 } 634 } 635 return list; 636 } 637 638 639 646 protected void updateLogins(Map m){ 647 DatasourceLogin login = session.getLogin(); 648 649 652 String user = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_USER, m, session); 653 String password = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_PASSWORD, m, session); 654 if(user != null) { 655 login.setUserName(user); 656 } 657 if(password != null) { 658 login.setPassword(securableObjectHolder.getSecurableObject().decryptPassword(password)); 659 } 660 661 String toplinkPlatform = (String )PropertiesHandler.getPropertyValueLogDebug(TopLinkProperties.TARGET_DATABASE, m, session); 662 if (toplinkPlatform != null) { 663 login.setPlatformClassName(toplinkPlatform); 664 } 665 666 if (isValidationOnly(m, false) && persistenceUnitInfo.getTransactionType() == PersistenceUnitTransactionType.JTA && persistenceUnitInfo.getJtaDataSource() == null){ 667 updateLoginDefaultConnector(login, m); 668 return; 669 } 670 671 login.setUsesExternalTransactionController(persistenceUnitInfo.getTransactionType() == PersistenceUnitTransactionType.JTA); 672 673 javax.sql.DataSource mainDatasource = null; 674 javax.sql.DataSource readDatasource = null; 675 if(login.shouldUseExternalTransactionController()) { 676 mainDatasource = persistenceUnitInfo.getJtaDataSource(); 678 readDatasource = persistenceUnitInfo.getNonJtaDataSource(); 680 } else { 681 if(persistenceUnitInfo.getJtaDataSource() != null) { 683 session.log(SessionLog.WARNING, SessionLog.TRANSACTION, "resource_local_persistence_init_info_ignores_jta_data_source", persistenceUnitInfo.getPersistenceUnitName()); 684 } 685 if(persistenceUnitInfo.getNonJtaDataSource() != null) { 686 mainDatasource = persistenceUnitInfo.getNonJtaDataSource(); 687 } else { 688 updateLoginDefaultConnector(login, m); 689 return; 690 } 691 } 692 693 if(!(login.getConnector() instanceof JNDIConnector)) { 695 JNDIConnector jndiConnector; 696 if (mainDatasource instanceof DataSourceImpl) { 697 jndiConnector = new JNDIConnector(((DataSourceImpl)mainDatasource).getName()); 699 } else { 700 jndiConnector = new JNDIConnector(mainDatasource); 701 } 702 login.setConnector(jndiConnector); 703 login.setUsesExternalConnectionPooling(true); 704 } 705 706 if(readDatasource != null) { 708 DatasourceLogin readLogin = (DatasourceLogin)login.clone(); 709 readLogin.dontUseExternalTransactionController(); 710 JNDIConnector jndiConnector; 711 if (readDatasource instanceof DataSourceImpl) { 712 jndiConnector = new JNDIConnector(((DataSourceImpl)readDatasource).getName()); 714 } else { 715 jndiConnector = new JNDIConnector(readDatasource); 716 } 717 readLogin.setConnector(jndiConnector); 718 session.setReadConnectionPool(readLogin); 719 } 720 721 } 722 723 729 protected void updateLoginDefaultConnector(DatasourceLogin login, Map m){ 730 if((login.getConnector() instanceof DefaultConnector)) { 731 DatabaseLogin dbLogin = (DatabaseLogin)login; 732 String jdbcDriver = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_DRIVER, m, session); 735 String connectionString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_URL, m, session); 736 if(connectionString != null) { 737 dbLogin.setConnectionString(connectionString); 738 } 739 if(jdbcDriver != null) { 740 dbLogin.setDriverClassName(jdbcDriver); 741 } 742 } 743 } 744 745 protected void updatePools(Map m) { 746 if(!session.getDefaultConnectionPool().getLogin().shouldUseExternalConnectionPooling()) { 748 String strWriteMin = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_WRITE_CONNECTIONS_MIN, m, session); 749 if(strWriteMin != null) { 750 session.getDefaultConnectionPool().setMinNumberOfConnections(Integer.parseInt(strWriteMin)); 751 } 752 String strWriteMax = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_WRITE_CONNECTIONS_MAX, m, session); 753 if(strWriteMax != null) { 754 session.getDefaultConnectionPool().setMaxNumberOfConnections(Integer.parseInt(strWriteMax)); 755 } 756 } 757 758 if(!session.getReadConnectionPool().getLogin().shouldUseExternalConnectionPooling()) { 760 String strReadMin = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_READ_CONNECTIONS_MIN, m, session); 761 if(strReadMin != null) { 762 session.getReadConnectionPool().setMinNumberOfConnections(Integer.parseInt(strReadMin)); 763 } 764 String strReadMax = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_READ_CONNECTIONS_MAX, m, session); 765 if(strReadMax != null) { 766 session.getReadConnectionPool().setMaxNumberOfConnections(Integer.parseInt(strReadMax)); 767 } 768 String strShouldUseShared = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_READ_CONNECTIONS_SHARED, m,session); 769 if(strShouldUseShared != null) { 770 boolean shouldUseShared = Boolean.parseBoolean(strShouldUseShared); 771 boolean sessionUsesShared = session.getReadConnectionPool() instanceof ReadConnectionPool; 772 if(shouldUseShared != sessionUsesShared) { 773 Login readLogin = session.getReadConnectionPool().getLogin(); 774 int nReadMin = session.getReadConnectionPool().getMinNumberOfConnections(); 775 int nReadMax = session.getReadConnectionPool().getMaxNumberOfConnections(); 776 if(shouldUseShared) { 777 session.useReadConnectionPool(nReadMin, nReadMax); 778 } else { 779 session.useExclusiveReadConnectionPool(nReadMin, nReadMax); 780 } 781 session.getReadConnectionPool().setLogin(readLogin); 783 } 784 } 785 } 786 } 787 788 800 protected void initServerSession(Map m) { 801 assignCMP3Policy(); 802 803 String name = EntityManagerFactoryProvider.getConfigPropertyAsString(TopLinkProperties.SESSION_NAME, m); 805 if(name == null) { 806 if (persistenceUnitInfo.getPersistenceUnitRootUrl() != null){ 807 name = persistenceUnitInfo.getPersistenceUnitRootUrl().toString() + "-" + persistenceUnitInfo.getPersistenceUnitName(); 808 } else { 809 name = persistenceUnitInfo.getPersistenceUnitName(); 810 } 811 session.setName(name); 812 addSessionToGlobalSessionManager(); 814 } 815 816 if (EntityManagerFactoryProvider.getConfigPropertyAsString(TopLinkProperties.JDBC_BIND_PARAMETERS, m) == null) { 818 session.getPlatform().setShouldBindAllParameters(true); 820 } 821 822 if (PropertiesHandler.getPrefixedPropertyValue(TopLinkProperties.CACHE_SIZE_, TopLinkProperties.DEFAULT, m) == null) { 824 int defaultCacheSize = Integer.parseInt(PropertiesHandler.getDefaultPropertyValue(TopLinkProperties.CACHE_SIZE_)); 826 Iterator descriptorsIterator = session.getDescriptors().values().iterator(); 827 while (descriptorsIterator.hasNext()) { 828 ((ClassDescriptor)descriptorsIterator.next()).setIdentityMapSize(defaultCacheSize); 829 } 830 } 831 } 832 833 834 839 protected void updateServerSession(Map m) { 840 if (session == null || session.isConnected()) { 841 return; 842 } 843 844 initOrUpdateLogging(false, m, AbstractSessionLog.getLog()); 845 initOrUpdateLogging(false, m, session.getSessionLog()); 846 847 updateSessionName(m); 848 String shouldBindString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.JDBC_BIND_PARAMETERS, m, session); 849 if (shouldBindString != null) { 850 session.getPlatform().setShouldBindAllParameters(Boolean.parseBoolean(shouldBindString)); 851 } 852 853 updateLogins(m); 854 855 updateServerPlatform(m); 857 858 updatePools(m); 859 860 updateDescriptorCacheSettings(m); 861 862 processDescriptorCustomizers(m); 864 processSessionCustomizer(m); 865 } 866 867 872 public void setIsInContainerMode(boolean isInContainerMode) { 873 this.isInContainerMode = isInContainerMode; 874 } 875 876 protected void processSessionCustomizer(Map m) { 877 String sessionCustomizerClassName = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.SESSION_CUSTOMIZER, m, session); 878 if(sessionCustomizerClassName == null) { 879 return; 880 } 881 882 Class sessionCustomizerClass = findClassForProperty(sessionCustomizerClassName, TopLinkProperties.SESSION_CUSTOMIZER); 883 SessionCustomizer sessionCustomizer; 884 try { 885 sessionCustomizer = (SessionCustomizer)sessionCustomizerClass.newInstance(); 886 sessionCustomizer.customize(session); 887 } catch (Exception ex) { 888 throw EntityManagerSetupException.failedWhileProcessingProperty(TopLinkProperties.SESSION_CUSTOMIZER, sessionCustomizerClassName, ex); 889 } 890 } 891 892 protected void initOrUpdateLogging(boolean init, Map m, SessionLog log) { 893 String logLevelString = PropertiesHandler.getPropertyValueLogDebug(TopLinkProperties.LOGGING_LEVEL, m, session); 894 if(logLevelString == null && init) { 895 logLevelString = PropertiesHandler.getDefaultPropertyValueLogDebug(TopLinkProperties.LOGGING_LEVEL, session); 896 } 897 if (logLevelString != null) { 898 log.setLevel(AbstractSessionLog.translateStringToLoggingLevel(logLevelString)); 899 } 900 String tsString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.LOGGING_TIMESTAMP, m, session); 901 if (tsString != null) { 902 log.setShouldPrintDate(Boolean.parseBoolean(tsString)); 903 } 904 String threadString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.LOGGING_THREAD, m, session); 905 if (threadString != null) { 906 log.setShouldPrintThread(Boolean.parseBoolean(threadString)); 907 } 908 String sessionString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.LOGGING_SESSION, m, session); 909 if (sessionString != null) { 910 log.setShouldPrintSession(Boolean.parseBoolean(sessionString)); 911 } 912 String exString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.LOGGING_EXCEPTIONS, m, session); 913 if (exString != null) { 914 log.setShouldLogExceptionStackTrace(Boolean.parseBoolean(exString)); 915 } 916 } 917 918 protected void updateSessionName(Map m) { 919 String newName = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(TopLinkProperties.SESSION_NAME, m, session); 920 if(newName == null || newName.equals(session.getName())) { 921 return; 922 } 923 924 removeSessionFromGlobalSessionManager(); 925 session.setName(newName); 926 addSessionToGlobalSessionManager(); 927 } 928 929 protected void processDescriptorCustomizers(Map m) { 930 Map customizerMap = PropertiesHandler.getPrefixValuesLogDebug(TopLinkProperties.DESCRIPTOR_CUSTOMIZER_, m, session); 931 if(customizerMap.isEmpty()) { 932 return; 933 } 934 935 Iterator it = customizerMap.entrySet().iterator(); 936 while (it.hasNext()) { 937 Map.Entry entry = (Map.Entry)it.next(); 938 String name = (String )entry.getKey(); 939 940 ClassDescriptor descriptor = session.getDescriptorForAlias(name); 941 if(descriptor == null) { 942 try { 943 Class javaClass = findClass(name); 944 descriptor = session.getDescriptor(javaClass); 945 } catch (Exception ex) { 946 } 948 } 949 if(descriptor != null) { 950 String customizerClassName = (String )entry.getValue(); 951 Class customizerClass = findClassForProperty(customizerClassName, TopLinkProperties.DESCRIPTOR_CUSTOMIZER_ + name); 952 try { 953 DescriptorCustomizer customizer = (DescriptorCustomizer)customizerClass.newInstance(); 954 customizer.customize(descriptor); 955 } catch (Exception ex) { 956 throw EntityManagerSetupException.failedWhileProcessingProperty(TopLinkProperties.DESCRIPTOR_CUSTOMIZER_ + name, customizerClassName, ex); 957 } 958 } 959 } 960 } 961 962 private void processORMetadata(ClassLoader privateClassLoader, AbstractSession session, Collection entities, boolean enableLazyForOneToOne){ 963 processor = new MetadataProcessor(session, privateClassLoader, enableLazyForOneToOne); 964 965 List<String > mappingFileNames = getORMXMLFileNames(privateClassLoader); 966 967 processor.processPersistenceUnitMetadata(session, persistenceUnitInfo, mappingFileNames, entities); 969 970 Iterator<String > fileNames = mappingFileNames.iterator(); 971 InputStream inputStream = null; 972 String fileName = null; 973 while (fileNames.hasNext()){ 974 try{ 975 fileName = fileNames.next(); 976 inputStream = PersistenceUnitProcessor.createInputStreamForFileInPersistenceUnit(fileName, persistenceUnitInfo, privateClassLoader); 977 if (inputStream != null){ 978 processor.processXML(inputStream, fileName); 979 } 980 } catch (RuntimeException e){ 981 handleORMException(e, session, fileName); 982 } catch (IOException exc){ 983 handleORMException(PersistenceUnitLoadingException.exceptionLoadingORMXML(fileName, exc), session, fileName); 984 } finally { 985 try{ 986 if (inputStream != null){ 987 inputStream.close(); 988 } 989 } catch (IOException exc){} 990 } 991 } 992 993 session = (AbstractSession) processor.processAnnotations(); 994 } 995 996 public boolean isPredeployed() { 997 return state == STATE_PREDEPLOYED; 998 } 999 1000 public boolean isDeployed() { 1001 return state == STATE_DEPLOYED; 1002 } 1003 1004 public boolean isUndeployed() { 1005 return state == STATE_UNDEPLOYED; 1006 } 1007 1008 protected void cleanUpSessionManager() { 1009 deploymentCount--; 1010 if(deploymentCount > 0) { 1011 return; 1012 } 1013 state = STATE_UNDEPLOYED; 1014 removeSessionFromGlobalSessionManager(); 1015 } 1016 1017 1023 public synchronized void undeploy() { 1024 if(state != STATE_DEPLOYED) { 1025 return; 1026 } 1027 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "undeploy_begin", new Object []{getPersistenceUnitInfo().getPersistenceUnitName(), state, deploymentCount}); 1028 try { 1029 cleanUpSessionManager(); 1030 } finally { 1031 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "undeploy_end", new Object []{getPersistenceUnitInfo().getPersistenceUnitName(), state, deploymentCount}); 1032 if(state == STATE_UNDEPLOYED) { 1033 session = null; 1034 } 1035 } 1036 } 1037} 1038 | Popular Tags |