Package org.jpox.jpa

Source Code of org.jpox.jpa.EntityManagerFactoryImpl

/**********************************************************************
Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
2006 Andy Jefferson - javadocs, reading of persistence.xml, overriding props
    ...
**********************************************************************/
package org.jpox.jpa;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContextType;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;

import org.jpox.OMFContext;
import org.jpox.ObjectManagerFactoryImpl;
import org.jpox.PersistenceConfiguration;
import org.jpox.jpa.exceptions.NotProviderException;
import org.jpox.metadata.MetaDataManager;
import org.jpox.metadata.PersistenceFileMetaData;
import org.jpox.metadata.PersistenceUnitMetaData;
import org.jpox.metadata.TransactionType;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;

/**
* EntityManagerFactory implementation.
* Caches the "persistence-unit" MetaData information when encountered (in J2SE mode).
*
* @version $Revision: 1.1 $
*/
public class EntityManagerFactoryImpl implements EntityManagerFactory
{
    /** Localiser for messages. */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.jpa.Localisation",
        JPOXJPAHelper.class.getClassLoader());

    /** Underlying PersistenceManagerFactory that provides the persistence functionality. */
    private PersistenceManagerFactory pmf;

    /** Cache of persistence-unit information for J2SE. */
    static private Map<String, PersistenceUnitMetaData> unitMetaDataCache = null;

    /** Persistence-Unit metadata that we are using in this EMF. */
    private PersistenceUnitMetaData unitMetaData = null;

    /** Flag for whether the factory is closed. */
    private boolean closed = false;

    /** Flag for whether this EMF is managed by a container. */
    private boolean containerManaged = false;

    /**
     * Constructor.
     */
    public EntityManagerFactoryImpl()
    {
    }

    /**
     * Constructor when working in a J2EE environment.
     * @param unitInfo The "persistent-unit" info
     * @param overridingProps factory properties overriding those in the "persistence-unit"
     */
    @SuppressWarnings("unchecked")
    public EntityManagerFactoryImpl(PersistenceUnitInfo unitInfo, Map overridingProps)
    {
        containerManaged = true;

        // Strictly speaking this is only required for the other constructor since the J2EE container should check
        // before calling us but we check anyway
        boolean validProvider = false;
        if (unitInfo.getPersistenceProviderClassName() == null ||
            unitInfo.getPersistenceProviderClassName().equals(PersistenceProviderImpl.class.getName()) ||
            (overridingProps != null && PersistenceProviderImpl.class.getName().equals(overridingProps.get("javax.persistence.provider"))))
        {
            validProvider = true;
        }
        if (!validProvider)
        {
            // Not a valid provider
            throw new NotProviderException(LOCALISER.msg("EMF.NotProviderForPersistenceUnit",
                unitInfo.getPersistenceUnitName()));
        }

        // Create a PersistenceUnitMetaData
        if (unitInfo.getTransactionType() == PersistenceUnitTransactionType.JTA)
        {
            unitMetaData = new PersistenceUnitMetaData(null, unitInfo.getPersistenceUnitName(),
                TransactionType.JTA.toString());
        }
        else if (unitInfo.getTransactionType() == PersistenceUnitTransactionType.RESOURCE_LOCAL)
        {
            unitMetaData = new PersistenceUnitMetaData(null, unitInfo.getPersistenceUnitName(),
                TransactionType.RESOURCE_LOCAL.toString());
        }
       
        // Classes
        List<String> classNames = unitInfo.getManagedClassNames();
        Iterator<String> classIter = classNames.iterator();
        while (classIter.hasNext())
        {
            unitMetaData.addClassName(classIter.next());
        }

        // Mapping files
        List<String> mappingFileNames = unitInfo.getMappingFileNames();
        Iterator<String> mappingFileIter = mappingFileNames.iterator();
        while (mappingFileIter.hasNext())
        {
            unitMetaData.addMappingFile(mappingFileIter.next());
        }

        // Jars
        List<URL> jarUrls = unitInfo.getJarFileUrls();
        Iterator<URL> jarUrlIter = jarUrls.iterator();
        while (jarUrlIter.hasNext())
        {
            unitMetaData.addJarFile(jarUrlIter.next());
        }

        // Properties
        Properties props = unitInfo.getProperties();
        for (Enumeration e = props.propertyNames(); e.hasMoreElements();)
        {
            String prop = (String) e.nextElement();
            unitMetaData.addProperty(prop, props.getProperty(prop));
        }

        // Exclude unlisted classes
        if (unitInfo.excludeUnlistedClasses())
        {
            unitMetaData.setExcludeUnlistedClasses();
        }

        // Provider
        unitMetaData.setProvider(unitInfo.getPersistenceProviderClassName());

        if (overridingProps == null)
        {
            overridingProps = new HashMap();
        }

        // unit info will give us a javax.sql.DataSource instance, so we give that to PMF
        if (unitInfo.getJtaDataSource() != null)
        {
            overridingProps.put("org.jpox.ConnectionFactory", unitInfo.getJtaDataSource());
        }
        if (unitInfo.getNonJtaDataSource() != null)
        {
            overridingProps.put("org.jpox.ConnectionFactory2", unitInfo.getNonJtaDataSource());
        }
        if (unitInfo.getClassLoader() != null)
        {
            overridingProps.put("org.jpox.primaryClassLoader", unitInfo.getClassLoader());
        }

        unitInfo.addTransformer(new JPAClassTransformer());
       
        // Initialise the PMF
        pmf = initialisePMF(unitMetaData, overridingProps);
    }

    /**
     * Constructor when working in a J2SE environment.
     * @param unitName Name of the "persistent-unit" to use
     * @param overridingProps factory properties overriding those in the "persistence-unit"
     */
    public EntityManagerFactoryImpl(String unitName, Map overridingProps)
    {
        if (unitMetaDataCache == null)
        {
            // Create our cache so we save on lookups
            unitMetaDataCache = new HashMap<String, PersistenceUnitMetaData>();
        }

        // Find the "persistence-unit"
        unitMetaData = EntityManagerFactoryImpl.unitMetaDataCache.get(unitName);
        if (unitMetaData == null)
        {
            // Find all "META-INF/persistence.xml" files in the current thread loader CLASSPATH and parse them
            // Create a temporary PMFContext so we have a parser
            OMFContext pmfCtxt = new OMFContext(new PersistenceConfiguration(){});
            pmfCtxt.setApi("JPA");
            MetaDataManager metadataMgr = pmfCtxt.getMetaDataManager();
            PersistenceFileMetaData[] files = metadataMgr.parsePersistenceFiles();
            if (files == null)
            {
                // No "persistence.xml" files found
                JPOXLogger.JPA.warn(LOCALISER.msg("EMF.NoPersistenceXML"));
                //throw new NoPersistenceXmlException(LOCALISER.msg("EMF.NoPersistenceXML"));
            }
            else
            {
                for (int i=0;i<files.length;i++)
                {
                    PersistenceUnitMetaData[] unitmds = files[i].getPersistenceUnits();
                    for (int j=0;j<unitmds.length;j++)
                    {
                        // Cache the "persistence-unit" for future reference
                        EntityManagerFactoryImpl.unitMetaDataCache.put(unitmds[j].getName(), unitmds[j]);
                        if (unitmds[j].getName().equals(unitName))
                        {
                            unitMetaData = unitmds[j];
                            unitMetaData.clearJarFiles(); // Jar files not applicable to J2SE [JPA 6.3]
                        }
                    }
                }
            }

            if (unitMetaData == null)
            {
                // No "persistence-unit" of the same name as requested so nothing to manage the persistence of
                JPOXLogger.JPA.warn(LOCALISER.msg("EMF.PersistenceUnitNotFound", unitName));
            }
            else
            {
                EntityManagerFactoryImpl.unitMetaDataCache.put(unitMetaData.getName(), unitMetaData);
            }
        }

        // Check the provider is ok for our use
        boolean validProvider = false;
        if (unitMetaData != null)
        {
            if (unitMetaData.getProvider() == null ||
                unitMetaData.getProvider().equals(PersistenceProviderImpl.class.getName()))
            {
                validProvider = true;
            }
        }
        if (overridingProps != null &&
            PersistenceProviderImpl.class.getName().equals(overridingProps.get("javax.persistence.provider")))
        {
            validProvider = true;
        }
        if (!validProvider)
        {
            // Not a valid provider
            throw new NotProviderException(LOCALISER.msg("EMF.NotProviderForPersistenceUnit",
                unitName));
        }

        // Initialise the PMF (even if unitMetaData is null)
        pmf = initialisePMF(unitMetaData, overridingProps);
    }

    /**
     * Accessor for whether the EMF is managed by a container.
     * @return Whether managed by a container
     */
    public boolean isContainerManaged()
    {
        return containerManaged;
    }

    /**
     * Method to close the factory.
     */
    public void close()
    {
        closed = true;
    }

    /**
     * Accessor for whether the factory is open
     * @return Whether it is open
     */
    public boolean isOpen()
    {
        return !closed;
    }

    /**
     * Method to create an entity manager.
     * @return The Entity Manager
     */
    public EntityManager createEntityManager()
    {
        // TODO Pass in the PersistenceContextType from metadata (if any)
        return new EntityManagerImpl(this, pmf, PersistenceContextType.EXTENDED);
    }

    /**
     * Method to create an entity manager with the specified properties.
     * This creates a new underlying PersistenceManagerFactory since each PMF is locked
     * when created to stop config changes.
     * @param overridingProps Properties to use for this manager
     * @return The Entity Manager
     */
    @SuppressWarnings("unchecked")
    public EntityManager createEntityManager(Map overridingProps)
    {
        // Create a PMF to do the actual persistence, using the original persistence-unit, plus these properties
        PersistenceManagerFactory thePMF = initialisePMF(unitMetaData, overridingProps);

        // TODO Pass in the PersistenceContextType from metadata (if any)
        return new EntityManagerImpl(this, thePMF, PersistenceContextType.EXTENDED);
    }

    /**
     * Method to initialise a PersistenceManagerFactory that will control the persistence.
     * If the unitMetaData is null will simply create a default PMF without initialising any MetaData etc.
     * If there is a unitMetaData then all metadata for that unit will be loaded/initialised.
     * @param unitMetaData The "persistence-unit" metadata (if any)
     * @param overridingProps Properties to override all others
     * @return The PersistenceManagerFactory
     */
    @SuppressWarnings("unchecked")
    protected PersistenceManagerFactory initialisePMF(PersistenceUnitMetaData unitMetaData, Map overridingProps)
    {
        // Create a PMF to do the actual persistence
        Map props = new HashMap();
        props.put("javax.jdo.PersistenceManagerFactoryClass", "org.jpox.jdo.JDOPersistenceManagerFactory");
        props.put("org.jpox.persistenceApiName", "JPA"); // PMF in "JPA mode"

        if (unitMetaData.getJtaDataSource() != null)
        {
            props.put("org.jpox.ConnectionFactoryName", unitMetaData.getJtaDataSource());
        }
        if (unitMetaData.getNonJtaDataSource() != null)
        {
            props.put("org.jpox.ConnectionFactory2Name", unitMetaData.getNonJtaDataSource());
        }
        if (unitMetaData != null)
        {
            if (unitMetaData.getTransactionType() != null)
            {
                props.put("org.jpox.TransactionType",
                    unitMetaData.getTransactionType().toString());
            }
            Properties unitProps = unitMetaData.getProperties();
            if (unitProps != null)
            {
                // Props for this "persistence-unit"
                props.putAll(unitProps);
            }
        }
        if (overridingProps != null)
        {
            // Apply the overriding properties
            props.putAll(overridingProps);
        }
        props.put("org.jpox.autoStartMechanism", "None"); // Dont allow autostart with JPA
        props.remove("org.jpox.PersistenceUnitName"); // Don't specify the persistence-unit

        PersistenceManagerFactory thePMF = JDOHelper.getPersistenceManagerFactory(props);

        if (unitMetaData != null)
        {
            // Load up the MetaData implied by this "persistence-unit"
            ObjectManagerFactoryImpl omf = (ObjectManagerFactoryImpl)thePMF;
            omf.getOMFContext().getMetaDataManager().initialise(unitMetaData, omf.getOMFContext().getClassLoaderResolver(null));
        }

        return thePMF;
    }
}
TOP

Related Classes of org.jpox.jpa.EntityManagerFactoryImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.