Search in sources :

Example 1 with JPAInitializer

use of org.eclipse.persistence.internal.jpa.deployment.JPAInitializer in project eclipselink by eclipse-ee4j.

the class DDLTestSuite method testContainerGenerateSchema.

/**
 * Test the container PersistenceProvider.generateSchema(PersistenceUnitInfo
 * info, Map map) method from the Persistence API.
 */
public void testContainerGenerateSchema() {
    String CONTAINER_GENERATE_SCHEMA_DROP_TARGET = "jpa21-container-generate-schema-drop.jdbc";
    String CONTAINER_GENERATE_SCHEMA_CREATE_TARGET = "jpa21-container-generate-schema-create.jdbc";
    String CONTAINER_GENERATE_SCHEMA_SESSION_NAME = "container-generate-schema-session";
    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
    properties.put(PersistenceUnitProperties.SESSION_NAME, CONTAINER_GENERATE_SCHEMA_SESSION_NAME);
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, "true");
    properties.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION, PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
    properties.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET, CONTAINER_GENERATE_SCHEMA_DROP_TARGET);
    properties.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET, CONTAINER_GENERATE_SCHEMA_CREATE_TARGET);
    // When a container calls PersistenceProvider.generateSchema(PersistenceUnitInfo info, Map map),
    // the container passes its own PUInfo Implementation. To avoid having to implement our own PUInfo,
    // a proxy object is created that invokes SEPersistenceUnitInfo's methods in the background.
    PersistenceProvider provider = new PersistenceProvider();
    JPAInitializer initializer = provider.getInitializer(puName, properties);
    SEPersistenceUnitInfo sePUImpl = initializer.findPersistenceUnitInfo(puName, properties);
    PersistenceUnitInfo puinfo = (PersistenceUnitInfo) Proxy.newProxyInstance(SEPersistenceUnitInfo.class.getClassLoader(), new Class<?>[] { PersistenceUnitInfo.class }, new PUInfoInvocationHandler(sePUImpl));
    provider.generateSchema(puinfo, properties);
    // Now create an entity manager and build some objects for this PU using
    // the same session name. Create the schema on the database with the
    // target scripts built previously.
    testPersistenceGenerateSchemaOnDatabase(CONTAINER_GENERATE_SCHEMA_CREATE_TARGET, CONTAINER_GENERATE_SCHEMA_DROP_TARGET, CONTAINER_GENERATE_SCHEMA_SESSION_NAME);
}
Also used : HashMap(java.util.HashMap) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) HashMap(java.util.HashMap) Map(java.util.Map) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) PUInfoInvocationHandler(org.eclipse.persistence.testing.models.jpa21.advanced.xml.ddl.PUInfoInvocationHandler)

Example 2 with JPAInitializer

use of org.eclipse.persistence.internal.jpa.deployment.JPAInitializer in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testEMFTargetServerEnforcing.

public void testEMFTargetServerEnforcing() {
    // the test requires passing properties to createEMF or createContainerEMF method.
    if (isOnServer()) {
        return;
    }
    EntityManagerFactory emf = null;
    try {
        System.setProperty(SystemProperties.ENFORCE_TARGET_SERVER, "true");
        Map<String, String> properties = new HashMap<>(JUnitTestCaseHelper.getDatabaseProperties());
        properties.put(PersistenceUnitProperties.TARGET_SERVER, Platform.class.getName());
        properties.put(PersistenceUnitProperties.SESSION_NAME, "dummy-default-session");
        PersistenceProvider provider = new PersistenceProvider();
        JPAInitializer initializer = provider.getInitializer(getPersistenceUnitName(), properties);
        SEPersistenceUnitInfo sePUImpl = initializer.findPersistenceUnitInfo(getPersistenceUnitName(), properties);
        emf = provider.createContainerEntityManagerFactory(sePUImpl, properties);
        ServerPlatform server = ((JpaEntityManagerFactory) emf).getServerSession().getServerPlatform();
        assertNotNull(server);
        assertFalse("server should be overridden", server instanceof Platform);
    } finally {
        System.clearProperty(SystemProperties.ENFORCE_TARGET_SERVER);
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : WebLogic_12_Platform(org.eclipse.persistence.platform.server.wls.WebLogic_12_Platform) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform) WebSphere_7_Platform(org.eclipse.persistence.platform.server.was.WebSphere_7_Platform) HashMap(java.util.HashMap) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform)

Example 3 with JPAInitializer

use of org.eclipse.persistence.internal.jpa.deployment.JPAInitializer in project eclipselink by eclipse-ee4j.

the class PersistenceProvider method generateSchema.

/**
 * Create database schemas and/or tables and/or create DDL scripts as
 * determined by the supplied properties.
 * <p>
 * Called by the Persistence class when schema generation is to occur as a
 * separate phase from creation of the entity manager factory.
 *
 * @param persistenceUnitName the name of the persistence unit
 * @param properties properties for schema generation; these may also
 *        contain provider-specific properties. The value of these
 *        properties override any values that may have been configured
 *        elsewhere.
 * @throws PersistenceException if insufficient or inconsistent
 *         configuration information is provided of if schema generation
 *         otherwise fails
 *
 * @since Java Persistence 2.1
 */
@Override
public boolean generateSchema(String persistenceUnitName, Map properties) {
    String puName = (persistenceUnitName == null) ? "" : persistenceUnitName;
    Map nonNullProperties = (properties == null) ? new HashMap<>() : properties;
    // If not EclipseLink, do nothing.
    if (checkForProviderProperty(nonNullProperties)) {
        JPAInitializer initializer = getInitializer(puName, nonNullProperties);
        SEPersistenceUnitInfo puInfo = initializer.findPersistenceUnitInfo(puName, nonNullProperties);
        if (puInfo != null && checkForProviderProperty(properties)) {
            // Will cause a login if necessary, generate the DDL and then close.
            // The false indicates that we do not require a connection if
            // generating only to script. Since the user may have connected with
            // specific database credentials for DDL generation or even provided
            // a specific connection, close the emf once we're done.
            createEntityManagerFactoryImpl(puInfo, properties, false).close();
            return true;
        }
    }
    return false;
}
Also used : JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with JPAInitializer

use of org.eclipse.persistence.internal.jpa.deployment.JPAInitializer in project eclipselink by eclipse-ee4j.

the class PersistenceProvider method createEntityManagerFactoryImpl.

/**
 * Internal method to return the entity manager factory.
 */
protected EntityManagerFactoryImpl createEntityManagerFactoryImpl(PersistenceUnitInfo puInfo, Map properties, boolean requiresConnection) {
    if (puInfo != null) {
        boolean isNew = false;
        // the name the uniquely defines the pu
        String uniqueName = null;
        String sessionName = null;
        EntityManagerSetupImpl emSetupImpl = null;
        String puName = puInfo.getPersistenceUnitName();
        JPAInitializer initializer = getInitializer(puInfo.getPersistenceUnitName(), properties);
        try {
            if (EntityManagerSetupImpl.mustBeCompositeMember(puInfo)) {
                // Persistence unit cannot be used standalone (only as a composite member).
                // Still the factory will be created but attempt to createEntityManager would cause an exception.
                emSetupImpl = new EntityManagerSetupImpl(puName, puName);
                // Predeploy assigns puInfo and does not do anything else.
                // The session is not created, no need to add emSetupImpl to the global map.
                emSetupImpl.predeploy(puInfo, properties);
                isNew = true;
            } else {
                if (initializer.isPersistenceUnitUniquelyDefinedByName()) {
                    uniqueName = puName;
                } else {
                    uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
                }
                sessionName = EntityManagerSetupImpl.getOrBuildSessionName(properties, puInfo, uniqueName);
                synchronized (EntityManagerFactoryProvider.emSetupImpls) {
                    emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
                    if (emSetupImpl == null) {
                        // there may be initial emSetupImpl cached in Initializer - remove it and use.
                        emSetupImpl = initializer.extractInitialEmSetupImpl(puName);
                        if (emSetupImpl != null) {
                            // change the name
                            emSetupImpl.changeSessionName(sessionName);
                        } else {
                            // create and predeploy a new emSetupImpl
                            emSetupImpl = initializer.callPredeploy((SEPersistenceUnitInfo) puInfo, properties, uniqueName, sessionName);
                        }
                        // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                        emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
                        EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
                        isNew = true;
                    }
                }
            }
        } catch (Exception e) {
            throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
        }
        if (!isNew) {
            if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
                throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
            }
            // synchronized to prevent undeploying by other threads.
            boolean undeployed = false;
            synchronized (emSetupImpl) {
                if (emSetupImpl.isUndeployed()) {
                    undeployed = true;
                } else {
                    // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                    emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
                }
            }
            if (undeployed) {
                // it has been undeployed by factory.close() in another thread - start all over again.
                return (EntityManagerFactoryImpl) createEntityManagerFactory(puName, properties);
            }
        }
        EntityManagerFactoryImpl factory = null;
        try {
            factory = new EntityManagerFactoryImpl(emSetupImpl, properties);
            emSetupImpl.setRequiresConnection(requiresConnection);
            emSetupImpl.preInitializeCanonicalMetamodel(factory);
            if (emSetupImpl.shouldGetSessionOnCreateFactory(properties)) {
                factory.getDatabaseSession();
            }
            return factory;
        } catch (RuntimeException ex) {
            if (factory != null) {
                factory.close();
            } else {
                emSetupImpl.undeploy();
            }
            throw ex;
        }
    }
    return null;
}
Also used : JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) PersistenceException(jakarta.persistence.PersistenceException)

Example 5 with JPAInitializer

use of org.eclipse.persistence.internal.jpa.deployment.JPAInitializer in project eclipselink by eclipse-ee4j.

the class RuntimeFactory method createEntityManagerFactory.

public EntityManagerFactory createEntityManagerFactory(PersistenceUnit pu) {
    EntityManagerSetupImpl emSetupImpl = null;
    boolean isNew = false;
    // the name that uniquely defines persistence unit
    String name = pu.getName();
    pu.setProperty(PersistenceUnitProperties.METADATA_SOURCE, new MetadataSource(pu));
    SEPersistenceUnitInfo puInfo = (SEPersistenceUnitInfo) pu.getPersistenceUnitInfo();
    JPAInitializer initializer = new PersistenceProvider().getInitializer(name, null);
    Map<String, Object> props = new HashMap<String, Object>();
    String uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
    String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(props, puInfo, uniqueName);
    try {
        synchronized (EntityManagerFactoryProvider.emSetupImpls) {
            emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
            if (emSetupImpl != null) {
                if (puInfo.getClassLoader() != emSetupImpl.getPersistenceUnitInfo().getClassLoader()) {
                    emSetupImpl.undeploy();
                    EntityManagerFactoryProvider.emSetupImpls.remove(sessionName);
                    SessionManager manager = SessionManager.getManager();
                    if (manager.getSessions().containsKey(sessionName)) {
                        manager.destroySession(sessionName);
                    }
                    manager.destroy();
                    emSetupImpl = null;
                }
            }
            if (emSetupImpl == null) {
                // there may be initial emSetupImpl cached in Initializer - remove it and use.
                emSetupImpl = initializer.extractInitialEmSetupImpl(name);
                if (emSetupImpl == null) {
                    // create and predeploy a new emSetupImpl
                    emSetupImpl = initializer.callPredeploy(puInfo, props, uniqueName, sessionName);
                } else {
                    // change the name
                    emSetupImpl.changeSessionName(sessionName);
                }
                // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
                EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
                isNew = true;
            }
        }
    } catch (Exception e) {
        throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
    }
    if (!isNew) {
        if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
            throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
        }
        // synchronized to prevent undeploying by other threads.
        boolean undeployed = false;
        synchronized (emSetupImpl) {
            if (emSetupImpl.isUndeployed()) {
                undeployed = true;
            }
            // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
            emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
        }
        if (undeployed) {
            // start all over again.
            return createEntityManagerFactory(pu);
        }
    }
    EntityManagerFactoryImpl factory = null;
    try {
        factory = new EntityManagerFactoryImpl(emSetupImpl, props);
        // actually calling createEntityManager
        if (emSetupImpl.shouldGetSessionOnCreateFactory(props)) {
            factory.getDatabaseSession();
        }
    } catch (RuntimeException ex) {
        if (factory != null) {
            factory.close();
        } else {
            emSetupImpl.undeploy();
        }
        throw ex;
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) SessionManager(org.eclipse.persistence.sessions.factories.SessionManager) JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) MetadataSource(org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)

Aggregations

JPAInitializer (org.eclipse.persistence.internal.jpa.deployment.JPAInitializer)7 HashMap (java.util.HashMap)6 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)6 Map (java.util.Map)4 PersistenceProvider (org.eclipse.persistence.jpa.PersistenceProvider)4 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)2 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)2 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)2 EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 PersistenceException (jakarta.persistence.PersistenceException)1 MetadataSource (org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource)1 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)1 ServerPlatform (org.eclipse.persistence.platform.server.ServerPlatform)1 WebSphere_7_Platform (org.eclipse.persistence.platform.server.was.WebSphere_7_Platform)1 WebLogic_12_Platform (org.eclipse.persistence.platform.server.wls.WebLogic_12_Platform)1 SessionManager (org.eclipse.persistence.sessions.factories.SessionManager)1 PUInfoInvocationHandler (org.eclipse.persistence.testing.models.jpa21.advanced.xml.ddl.PUInfoInvocationHandler)1 PUInfoInvocationHandler (org.eclipse.persistence.testing.models.jpa22.advanced.xml.ddl.PUInfoInvocationHandler)1