FileDocCategorySizeDatePackage
EntityManagerFactoryProvider.javaAPI DocGlassfish v2 API23616Fri Jul 27 10:48:16 BST 2007oracle.toplink.essentials.ejb.cmp3

EntityManagerFactoryProvider

public class EntityManagerFactoryProvider extends Object implements oracle.toplink.essentials.internal.ejb.cmp3.PersistenceInitializationActivator, PersistenceProvider
This is the TopLink EJB 3.0 provider The default constructor can be used to build the provider by reflection, after which it can be used to create EntityManagerFactories

Fields Summary
public static final String
TOPLINK_ORM_THROW_EXCEPTIONS
public static final String
TOPLINK_VALIDATION_ONLY_PROPERTY
public static final String
DDL_GENERATION
public static final String
CREATE_ONLY
public static final String
DROP_AND_CREATE
public static final String
NONE
public static final String
APP_LOCATION
public static final String
CREATE_JDBC_DDL_FILE
public static final String
DROP_JDBC_DDL_FILE
public static final String
DEFAULT_APP_LOCATION
public static final String
DEFAULT_CREATE_JDBC_FILE_NAME
public static final String
DEFAULT_DROP_JDBC_FILE_NAME
public static final String
JAVASE_DB_INTERACTION
public static final String
DDL_GENERATION_MODE
public static final String
DDL_SQL_SCRIPT_GENERATION
public static final String
DDL_DATABASE_GENERATION
public static final String
DDL_BOTH_GENERATION
public static final String
DEFAULT_DDL_GENERATION_MODE
protected static final HashMap
emSetupImpls
protected static final String[]
oldPropertyNames
Constructors Summary
public EntityManagerFactoryProvider()
A default constructor is required by all Providers accoring the the EJB 3.0 specification


                       
            
    
Methods Summary
public static voidaddEntityManagerSetupImpl(java.lang.String name, oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl setup)
Add an EntityManagerSetupImpl to the cached list These are used to ensure all persistence units that are the same get the same underlying session

param
name
param
setup

          if (name == null){
              emSetupImpls.put("", setup);
          }
          emSetupImpls.put(name, setup);
      
public static java.lang.StringaddFileSeperator(java.lang.String appLocation)

        int strLength = appLocation.length();
        if (appLocation.substring(strLength -1, strLength).equals(File.separator)) {
            return appLocation;
        } else {
            return appLocation + File.separator;
        }
    
public javax.persistence.EntityManagerFactorycreateContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo info, java.util.Map properties)
Called by the container when an EntityManagerFactory is to be created.

param
info Metadata for use by the persistence provider
return
EntityManagerFactory for the persistence unit specified by the metadata
param
map A Map of integration-level properties for use by the persistence provider.

        Map nonNullProperties = (properties == null) ? new HashMap() : properties;
        
        EntityManagerSetupImpl emSetupImpl = null;
        synchronized (EntityManagerFactoryProvider.emSetupImpls) {
            String urlAndName = info.getPersistenceUnitRootUrl() + info.getPersistenceUnitName();
            emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(urlAndName);
            if (emSetupImpl == null){
                emSetupImpl = new EntityManagerSetupImpl();
                emSetupImpl.setIsInContainerMode(true);        
                EntityManagerFactoryProvider.addEntityManagerSetupImpl(urlAndName, emSetupImpl);
            }
        }
        
        ClassTransformer transformer = null;
        if(!emSetupImpl.isDeployed()) {
            transformer = emSetupImpl.predeploy(info, nonNullProperties);
        }
        if (transformer != null){
            info.addTransformer(transformer);
        }
        // When EntityManagerFactory is created, the session is only partially created
        // When the factory is actually accessed, the emSetupImpl will be used to complete the session construction
        EntityManagerFactoryImpl factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties);

        // This code has been added to allow validation to occur without actually calling createEntityManager
        if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
        	factory.getServerSession();
        }
        return factory;
    
public javax.persistence.EntityManagerFactorycreateEntityManagerFactory(java.lang.String emName, java.util.Map properties)
Called by Persistence class when an EntityManagerFactory is to be created.

param
emName The name of the persistence unit
param
map A Map of properties for use by the persistence provider. These properties may be used to override the values of the corresponding elements in the persistence.xml file or specify values for properties not specified in the persistence.xml.
return
EntityManagerFactory for the persistence unit, or null if the provider is not the right provider

        Map nonNullProperties = (properties == null) ? new HashMap() : properties;
        String name = emName;
        if (name == null){
            name = "";
        }

        JavaSECMPInitializer initializer = JavaSECMPInitializer.getJavaSECMPInitializer();
        EntityManagerSetupImpl emSetupImpl = null;
        ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();

        try {
            Enumeration<URL> resources = currentLoader.getResources("META-INF/persistence.xml");
            boolean initialized = false;
            while (resources.hasMoreElements()) {
                URL url = PersistenceUnitProcessor.computePURootURL(resources.nextElement());
                String urlAndName = url + name;
            
                synchronized (EntityManagerFactoryProvider.emSetupImpls){
                    emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(urlAndName);
                    if (emSetupImpl == null || emSetupImpl.isUndeployed()){
                        if (!initialized) {
                            initializer.initialize(nonNullProperties, this);
                            initialized = true;
                        }
                        
                        emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(urlAndName);
                    }
                }

                // We found a match, stop looking.
                if (emSetupImpl != null) {
                    break;
                }
            }
        } catch (Exception e){
            throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(currentLoader, e);
        }

        //gf bug 854  Returns null if EntityManagerSetupImpl for the name doesn't exist (e.g. a non-existant PU)
        if (emSetupImpl == null) {
            return null;
        }
        
        if (!isPersistenceProviderSupported(emSetupImpl.getPersistenceUnitInfo().getPersistenceProviderClassName())){
            return null;
        }

        // synchronized to prevent overriding of the class loader
        // and also calls to predeploy and undeploy by other threads -
        // the latter may alter result of shouldRedeploy method.
        synchronized(emSetupImpl) {
            if(emSetupImpl.shouldRedeploy()) {
                SEPersistenceUnitInfo persistenceInfo = (SEPersistenceUnitInfo)emSetupImpl.getPersistenceUnitInfo();
                persistenceInfo.setClassLoader(JavaSECMPInitializer.getMainLoader());
                // if we are in undeployed state, this is a redeploy of an initial deployment that worked
                // so if weaving were going to occur, it already occured.  Therefore we can use the main classloader
                if (emSetupImpl.isUndeployed()){
                    persistenceInfo.setNewTempClassLoader(JavaSECMPInitializer.getMainLoader());
                }
            }
            // call predeploy
            // this will just increment the factory count since we should already be deployed
            emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), nonNullProperties);
        }
        
        EntityManagerFactoryImpl factory = null;
        try {
            factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties);
    
            // This code has been added to allow validation to occur without actually calling createEntityManager
            if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
                factory.getServerSession();
            }
            return factory;
        } catch (RuntimeException ex) {
            if(factory != null) {
                factory.close();
            } else {
                emSetupImpl.undeploy();
            }
            throw ex;
        }
    
public static voidcreateOrReplaceDefaultTables(oracle.toplink.essentials.tools.schemaframework.SchemaManager mgr, boolean shouldDropFirst)

          
        if (shouldDropFirst){
            mgr.replaceDefaultTables(true); 
        } else { 
            mgr.createDefaultTables(); 
        }
    
public static voidgenerateDDLFiles(oracle.toplink.essentials.threetier.ServerSession session, java.util.Map props, boolean inSEmode)

        boolean createTables = false, shouldDropFirst = false;
        String appLocation; 
        String createDDLJdbc;
        String dropDDLJdbc;
        String ddlGeneration = NONE;
        
        if(null == props){
            return;
        }

        ddlGeneration = (String)getConfigPropertyAsString(DDL_GENERATION, props, NONE);
        ddlGeneration = ddlGeneration.toLowerCase();
        if(ddlGeneration.equals(NONE)) {
            return;
        }

        if(ddlGeneration.equals(CREATE_ONLY) || 
            ddlGeneration.equals(DROP_AND_CREATE)) {
            createTables = true;
            if(ddlGeneration.equals(DROP_AND_CREATE)) {
                shouldDropFirst = true;
            }
        } 
        
        if (createTables) {
            String ddlGenerationMode = (String) getConfigPropertyAsString(DDL_GENERATION_MODE, props, DEFAULT_DDL_GENERATION_MODE);
            // Optimize for cases where the value is explicitly set to NONE 
            if (ddlGenerationMode.equals(NONE)) {                
                return;
            }
            
            appLocation = (String)getConfigPropertyAsString( APP_LOCATION, props, DEFAULT_APP_LOCATION);
            createDDLJdbc = (String)getConfigPropertyAsString( CREATE_JDBC_DDL_FILE, props, DEFAULT_CREATE_JDBC_FILE_NAME);
            dropDDLJdbc = (String)getConfigPropertyAsString( DROP_JDBC_DDL_FILE, props,  DEFAULT_DROP_JDBC_FILE_NAME);
            
            SchemaManager mgr = new SchemaManager(session);
            
            // The inSEmode checks here are only temporary to ensure we still 
            // play nicely with Glassfish.
            if (ddlGenerationMode.equals(DDL_DATABASE_GENERATION) || inSEmode) {
                runInSEMode(mgr, shouldDropFirst);                
                
                if (inSEmode) {
                    writeDDLsToFiles(mgr, appLocation,  createDDLJdbc,  dropDDLJdbc);      
                }
            } else if (ddlGenerationMode.equals(DDL_SQL_SCRIPT_GENERATION)) {
                writeDDLsToFiles(mgr, appLocation,  createDDLJdbc,  dropDDLJdbc);                
            } else if (ddlGenerationMode.equals(DDL_BOTH_GENERATION)) {
                runInSEMode(mgr, shouldDropFirst);
                writeDDLsToFiles(mgr, appLocation,  createDDLJdbc,  dropDDLJdbc);
            }
        }
    
public static java.lang.StringgetConfigPropertyAsString(java.lang.String propertyKey, java.util.Map overrides, java.lang.String defaultValue)
Check the provided map for an object with the given key. If that object is not available, check the System properties. If it is not available from either location, return the default value.

param
propertyKey
param
map
param
defaultValue
return

        String value = getConfigPropertyAsString(propertyKey, overrides);
        if (value == null){
            value = defaultValue;
        }
        return value;
    
public static java.lang.StringgetConfigPropertyAsString(java.lang.String propertyKey, java.util.Map overrides)

        String value = null;
        if (overrides != null){
            value = (String)overrides.get(propertyKey);
        }
        if (value == null){
            value = System.getProperty(propertyKey);
        }
        
        return value;
    
public static java.lang.StringgetConfigPropertyAsStringLogDebug(java.lang.String propertyKey, java.util.Map overrides, java.lang.String defaultValue, oracle.toplink.essentials.internal.sessions.AbstractSession session)

        String value = getConfigPropertyAsStringLogDebug(propertyKey, overrides, session);
        if (value == null){
            value = defaultValue;
            session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_default", new Object[]{propertyKey, value});
        }
        return value;
    
public static java.lang.StringgetConfigPropertyAsStringLogDebug(java.lang.String propertyKey, java.util.Map overrides, oracle.toplink.essentials.internal.sessions.AbstractSession session)

        String value = null;
        if (overrides != null){
            value = (String)overrides.get(propertyKey);
        }
        if (value == null){
            value = System.getProperty(propertyKey);
        }
        if(value != null && session !=  null) {
            String overrideValue = TopLinkProperties.getOverriddenLogStringForProperty(propertyKey);;           
            String logValue = (overrideValue == null) ? value : overrideValue;
            session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_specified", new Object[]{propertyKey, logValue});
        }
        
        return value;
    
public static java.lang.ObjectgetConfigPropertyLogDebug(java.lang.String propertyKey, java.util.Map overrides, oracle.toplink.essentials.internal.sessions.AbstractSession session)

        Object value = null;
        if (overrides != null){
            value = overrides.get(propertyKey);
        }
        if (value == null){
            value = System.getProperty(propertyKey);
        }
        if(value != null && session !=  null) {
            String overrideValue = TopLinkProperties.getOverriddenLogStringForProperty(propertyKey);;           
            Object logValue = (overrideValue == null) ? value : overrideValue;
            session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_specified", new Object[]{propertyKey, logValue});
        }
        
        return value;
    
public static oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImplgetEntityManagerSetupImpl(java.lang.String emName)
Return the setup class for a given entity manager name

param
emName

          if (emName == null){
              return (EntityManagerSetupImpl)emSetupImpls.get("");
          }
          return (EntityManagerSetupImpl)emSetupImpls.get(emName);
      
public booleanisPersistenceProviderSupported(java.lang.String providerClassName)
Returns whether the given persistence provider class is supported by this implementation

param
providerClassName
return

        return (providerClassName == null) || providerClassName.equals("") || providerClassName.equals(EntityManagerFactoryProvider.class.getName());
    
public static voidlogin(oracle.toplink.essentials.threetier.ServerSession session, java.util.Map properties)
Logs in to given session. If user has not specified the plaform would be auto detected

param
session The session to login to.
param
properties User specified properties for the persistence unit

        String toplinkPlatform = (String)properties.get(TopLinkProperties.TARGET_DATABASE);
        if (!session.isConnected()) {
            if (toplinkPlatform == null || toplinkPlatform.equals(TargetDatabase.Auto)) {
                // if user has not specified a database platform, try to detect
                session.loginAndDetectDatasource();
            } else {
                session.login();
            }
        }
    
public static java.util.MapmergeMaps(java.util.Map target, java.util.Map source)
Merge the properties from the source object into the target object. If the property exists in both objects, use the one from the target

param
target
param
source
return
the target object

        Map map = new HashMap();
        if (source != null){
            map.putAll(source);
        }

        if (target != null){
            map.putAll(target);
        }
        return map;
    
public static voidrunInSEMode(oracle.toplink.essentials.tools.schemaframework.SchemaManager mgr, boolean shouldDropFirst)

        String str = getConfigPropertyAsString(JAVASE_DB_INTERACTION, null ,"true");
        boolean interactWithDB = Boolean.valueOf(str.toLowerCase()).booleanValue();
        if (!interactWithDB){
            return;
        }
        createOrReplaceDefaultTables(mgr, shouldDropFirst);
    
public static voidtranslateOldProperties(java.util.Map m, oracle.toplink.essentials.internal.sessions.AbstractSession session)
This is a TEMPORARY method that will be removed. DON'T USE THIS METHOD - for internal use only.

param
Map m
param
AbstractSession session

        for(int i=0; i < oldPropertyNames.length; i++) {
            Object value = getConfigPropertyAsString(oldPropertyNames[i][1], m);
            if(value != null) {
                if(session != null){
                    session.log(SessionLog.INFO, SessionLog.TRANSACTION, "deprecated_property", oldPropertyNames[i]);
                }
                m.put(oldPropertyNames[i][0], value);
            }
        }
    
public static voidwarnOldProperties(java.util.Map m, oracle.toplink.essentials.internal.sessions.AbstractSession session)

        for(int i=0; i < oldPropertyNames.length; i++) {
            Object value = m.get(oldPropertyNames[i][1]);
            if(value != null) {
                session.log(SessionLog.INFO, SessionLog.TRANSACTION, "deprecated_property", oldPropertyNames[i]);
            }
        }
    
public static voidwriteDDLsToFiles(oracle.toplink.essentials.tools.schemaframework.SchemaManager mgr, java.lang.String appLocation, java.lang.String createDDLJdbc, java.lang.String dropDDLJdbc)

        // Ensure that the appLocation string ends with  File.seperator 
        appLocation = addFileSeperator(appLocation);
        if (null != createDDLJdbc) {
            String createJdbcFileName = appLocation + createDDLJdbc;
            mgr.outputCreateDDLToFile(createJdbcFileName);
        }

        if (null != dropDDLJdbc) {
            String dropJdbcFileName = appLocation + dropDDLJdbc;              
            mgr.outputDropDDLToFile(dropJdbcFileName);
        }

        mgr.setCreateSQLFiles(false);
        // When running in the application server environment always ensure that
        // we write out both the drop and create table files.
        createOrReplaceDefaultTables(mgr, true);
        mgr.closeDDLWriter();