Search in sources :

Example 56 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class EntityManagerSetupImpl method deploy.

/**
 * Deploy a persistence session and return an EntityManagerFactory.
 *
 * Deployment takes a session that was partially created in the predeploy call and makes it whole.
 *
 * This means doing any configuration that requires the real class definitions for the entities.  In
 * the predeploy phase we were in a stage where we were not let allowed to load the real classes.
 *
 * Deploy could be called several times - but only the first call does the actual deploying -
 * additional calls allow to update session properties (in case the session is not connected).
 *
 * Note that there is no need to synchronize deploy method - it doesn't alter factoryCount
 * and while deploy is executed no other method can alter the current state
 * (predeploy call would just increment factoryCount; undeploy call would not drop factoryCount to 0).
 * However precautions should be taken to handle concurrent calls to deploy, because those may
 * alter the current state or connect the session.
 *
 * @param realClassLoader The class loader that was used to load the entity classes. This loader
 *               will be maintained for the lifespan of the loaded classes.
 * @param additionalProperties added to persistence unit properties for updateServerSession overriding existing properties.
 *              In JSE case it allows to alter properties in main (as opposed to preMain where preDeploy is called).
 * @return An EntityManagerFactory to be used by the Container to obtain EntityManagers
 */
public AbstractSession deploy(ClassLoader realClassLoader, Map additionalProperties) {
    if (this.state != STATE_PREDEPLOYED && this.state != STATE_DEPLOYED && this.state != STATE_HALF_DEPLOYED) {
        if (mustBeCompositeMember()) {
            throw new PersistenceException(EntityManagerSetupException.compositeMemberCannotBeUsedStandalone(this.persistenceUnitInfo.getPersistenceUnitName()));
        }
        throw new PersistenceException(EntityManagerSetupException.cannotDeployWithoutPredeploy(this.persistenceUnitInfo.getPersistenceUnitName(), this.state, this.persistenceException));
    }
    // state is PREDEPLOYED or DEPLOYED
    this.session.log(SessionLog.FINEST, SessionLog.JPA, "deploy_begin", new Object[] { getPersistenceUnitInfo().getPersistenceUnitName(), this.session.getName(), this.state, this.factoryCount });
    ClassLoader classLoaderToUse = realClassLoader;
    if (additionalProperties.containsKey(PersistenceUnitProperties.CLASSLOADER)) {
        classLoaderToUse = (ClassLoader) additionalProperties.get(PersistenceUnitProperties.CLASSLOADER);
    } else if ((this.processor != null) && (this.processor.getProject() != null) && (this.processor.getProject().hasVirtualClasses()) && (this.state == STATE_PREDEPLOYED) && (!(classLoaderToUse instanceof DynamicClassLoader))) {
        classLoaderToUse = new DynamicClassLoader(classLoaderToUse);
    }
    // indicates whether session has failed to connect, determines whether HALF_DEPLOYED state should be kept in case of exception.
    boolean isLockAcquired = false;
    try {
        Map deployProperties = mergeMaps(additionalProperties, this.persistenceUnitInfo.getProperties());
        updateTunerPreDeploy(deployProperties, classLoaderToUse);
        translateOldProperties(deployProperties, this.session);
        if (isComposite()) {
            updateCompositeMembersProperties(deployProperties);
        }
        if (this.state == STATE_PREDEPLOYED) {
            this.deployLock.acquire();
            isLockAcquired = true;
            if (this.state == STATE_PREDEPLOYED) {
                if (this.shouldBuildProject && !this.isSessionLoadedFromSessionsXML) {
                    if (isComposite()) {
                        deployCompositeMembers(deployProperties, classLoaderToUse);
                    } else {
                        if (this.processor.getMetadataSource() != null) {
                            Map metadataProperties = this.processor.getMetadataSource().getPropertyOverrides(deployProperties, classLoaderToUse, this.session.getSessionLog());
                            if (metadataProperties != null && !metadataProperties.isEmpty()) {
                                translateOldProperties(metadataProperties, this.session);
                                deployProperties = mergeMaps(metadataProperties, deployProperties);
                            }
                        }
                        // listeners and queries require the real classes and are therefore built during deploy using the realClassLoader
                        this.processor.setClassLoader(classLoaderToUse);
                        this.processor.createDynamicClasses();
                        this.processor.addEntityListeners();
                        if (this.projectCacheAccessor != null) {
                            // cache the project:
                            this.projectCacheAccessor.storeProject(this.session.getProject(), deployProperties, this.session.getSessionLog());
                        }
                        // The project is initially created using class names rather than classes.  This call will make the conversion.
                        // If the session was loaded from sessions.xml this will also convert the descriptor classes to the correct class loader.
                        this.session.getProject().convertClassNamesToClasses(classLoaderToUse);
                        if (!isCompositeMember()) {
                            addBeanValidationListeners(deployProperties, classLoaderToUse);
                        }
                        // Process the customizers last.
                        this.processor.processCustomizers();
                    }
                    this.processor = null;
                } else {
                    // The project is initially created using class names rather than classes.  This call will make the conversion.
                    // If the session was loaded from sessions.xml this will also convert the descriptor classes to the correct class loader.
                    this.session.getProject().convertClassNamesToClasses(classLoaderToUse);
                    if (!this.shouldBuildProject) {
                        // process anything that might not have been serialized/cached in the project correctly:
                        if (!isCompositeMember()) {
                            addBeanValidationListeners(deployProperties, classLoaderToUse);
                        }
                        // process Descriptor customizers:
                        processDescriptorsFromCachedProject(classLoaderToUse);
                    }
                }
                finishProcessingDescriptorEvents(classLoaderToUse);
                this.structConverters = getStructConverters(classLoaderToUse);
                updateRemote(deployProperties, classLoaderToUse);
                initSession();
                if (this.session.getIntegrityChecker().hasErrors()) {
                    this.session.handleException(new IntegrityException(session.getIntegrityChecker()));
                }
                this.session.getDatasourcePlatform().getConversionManager().setLoader(classLoaderToUse);
                this.state = STATE_HALF_DEPLOYED;
            // keep deployLock
            } else {
                // state is HALF_DEPLOYED or DEPLOY_FAILED
                this.deployLock.release();
                isLockAcquired = false;
                if (this.state == STATE_DEPLOY_FAILED) {
                    // Rethrow the cache PersistenceException, which caused STATE_DEPLOYED_FAILED.
                    throw persistenceException;
                }
            }
        }
        // state is HALF_DEPLOYED or DEPLOYED
        if (!isCompositeMember()) {
            if (this.session.isDatabaseSession() && !((DatabaseSessionImpl) session).isLoggedIn()) {
                // If it's HALF_DEPLOYED then deployLock has been already acquired.
                if (!isLockAcquired) {
                    this.deployLock.acquire();
                    isLockAcquired = true;
                }
                if (!((DatabaseSessionImpl) this.session).isLoggedIn()) {
                    if (this.state == STATE_DEPLOY_FAILED) {
                        // Rethrow the cache PersistenceException, which caused STATE_DEPLOYED_FAILED.
                        throw persistenceException;
                    }
                    this.session.setProperties(deployProperties);
                    updateSession(deployProperties, classLoaderToUse);
                    if (isValidationOnly(deployProperties, false)) {
                        /**
                         * for 324213 we could add a session.loginAndDetectDatasource() call
                         * before calling initializeDescriptors when validation-only is True
                         * to avoid a native sequence exception on a generic DatabasePlatform
                         * by auto-detecting the correct DB platform.
                         * However, this would introduce a DB login when validation is on
                         * - in opposition to the functionality of the property (to only validate)
                         */
                        if (this.state == STATE_HALF_DEPLOYED) {
                            getDatabaseSession().initializeDescriptors();
                            this.state = STATE_DEPLOYED;
                        }
                    } else {
                        try {
                            updateTunerDeploy(deployProperties, classLoaderToUse);
                            updateFreeMemory(deployProperties);
                            if (this.isSessionLoadedFromSessionsXML) {
                                getDatabaseSession().login();
                            } else {
                                login(getDatabaseSession(), deployProperties, requiresConnection);
                            }
                            final Platform platform = getDatabaseSession().getDatasourcePlatform();
                            String dbProperties = getConfigPropertyAsStringLogDebug(PersistenceUnitProperties.TARGET_DATABASE_PROPERTIES, deployProperties, this.session);
                            PropertiesUtils.set(platform, PersistenceUnitProperties.TARGET_DATABASE_PROPERTIES, dbProperties);
                            // Make JTA integration throw JPA exceptions.
                            if (this.session.hasExternalTransactionController()) {
                                if (this.session.getExternalTransactionController().getExceptionHandler() == null) {
                                    this.session.getExternalTransactionController().setExceptionHandler(new ExceptionHandler() {

                                        @Override
                                        public Object handleException(RuntimeException exception) {
                                            if (exception instanceof org.eclipse.persistence.exceptions.OptimisticLockException) {
                                                throw new OptimisticLockException(exception);
                                            } else if (exception instanceof EclipseLinkException) {
                                                throw new PersistenceException(exception);
                                            } else {
                                                throw exception;
                                            }
                                        }
                                    });
                                }
                            }
                            this.state = STATE_DEPLOYED;
                        } catch (Throwable loginException) {
                            if (this.state == STATE_HALF_DEPLOYED) {
                                if (this.session.isConnected()) {
                                    // Cannot recover from that - the user has to fix the persistence unit and redeploy it.
                                    try {
                                        getDatabaseSession().logout();
                                    } catch (Throwable logoutException) {
                                    // Ignore
                                    }
                                    this.state = STATE_DEPLOY_FAILED;
                                }
                            }
                            throw loginException;
                        }
                        if (!this.isSessionLoadedFromSessionsXML) {
                            addStructConverters();
                        }
                        // Generate the DDL using the correct connection.
                        writeDDL(deployProperties, getDatabaseSession(deployProperties), classLoaderToUse);
                    }
                }
                // Initialize platform specific identity sequences.
                session.getDatasourcePlatform().initIdentitySequences(getDatabaseSession(), MetadataProject.DEFAULT_IDENTITY_GENERATOR);
                updateTunerPostDeploy(deployProperties, classLoaderToUse);
                this.deployLock.release();
                isLockAcquired = false;
            }
            // 266912: Initialize the Metamodel, a login should have already occurred.
            try {
                this.getMetamodel(classLoaderToUse);
            } catch (Exception e) {
                this.session.log(SessionLog.FINEST, SessionLog.METAMODEL, "metamodel_init_failed", new Object[] { e.getMessage() });
            }
        }
        // Clear the weaver's reference to meta-data information, as it is held by the class loader and will never gc.
        if (this.weaver != null) {
            this.weaver.clear();
            this.weaver = null;
        }
        return this.session;
    } catch (Throwable exception) {
        // before releasing deployLock switch to the correct state
        if (this.state == STATE_PREDEPLOYED) {
            this.state = STATE_DEPLOY_FAILED;
        }
        PersistenceException persistenceEx;
        if (this.state == STATE_DEPLOY_FAILED) {
            if (exception == persistenceException) {
                persistenceEx = new PersistenceException(EntityManagerSetupException.cannotDeployWithoutPredeploy(this.persistenceUnitInfo.getPersistenceUnitName(), this.state, this.persistenceException));
            } else {
                // before releasing deployLock cache the exception
                persistenceEx = createDeployFailedPersistenceException(exception);
            }
        } else {
            if (exception instanceof PersistenceException) {
                persistenceEx = (PersistenceException) exception;
            } else {
                persistenceEx = new PersistenceException(exception);
            }
        }
        if (isLockAcquired) {
            this.deployLock.release();
        }
        this.session.logThrowable(SessionLog.SEVERE, SessionLog.EJB, exception);
        throw persistenceEx;
    } finally {
        this.session.log(SessionLog.FINEST, SessionLog.JPA, "deploy_end", new Object[] { getPersistenceUnitInfo().getPersistenceUnitName(), this.session.getName(), this.state, this.factoryCount });
    }
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform) DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) EISPlatform(org.eclipse.persistence.eis.EISPlatform) CustomServerPlatform(org.eclipse.persistence.platform.server.CustomServerPlatform) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) OptimisticLockException(jakarta.persistence.OptimisticLockException) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) OptimisticLockException(jakarta.persistence.OptimisticLockException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) RemoteException(java.rmi.RemoteException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityManagerSetupException(org.eclipse.persistence.exceptions.EntityManagerSetupException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PersistenceException(jakarta.persistence.PersistenceException) MalformedURLException(java.net.MalformedURLException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) ExceptionHandler(org.eclipse.persistence.exceptions.ExceptionHandler) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) PersistenceException(jakarta.persistence.PersistenceException) DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap) IdentityMap(org.eclipse.persistence.internal.identitymaps.IdentityMap) HashMap(java.util.HashMap)

Example 57 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class MetadataProject method createDynamicClasses.

/**
 * INTERNAL:
 * Create the dynamic class using JPA metadata processed descriptors. Called
 * at deploy time after all metadata processing has completed.
 */
public void createDynamicClasses(ClassLoader loader) {
    if (!m_virtualClasses.isEmpty()) {
        if (DynamicClassLoader.class.isAssignableFrom(loader.getClass())) {
            DynamicClassLoader dcl = (DynamicClassLoader) loader;
            // Create the dynamic classes.
            Map<String, MetadataDescriptor> dynamicClasses = new HashMap<String, MetadataDescriptor>();
            for (ClassAccessor accessor : m_virtualClasses.values()) {
                createDynamicClass(accessor.getDescriptor(), dynamicClasses, dcl);
            }
            // Create the dynamic types.
            Map<String, DynamicType> dynamicTypes = new HashMap<String, DynamicType>();
            for (MetadataDescriptor descriptor : dynamicClasses.values()) {
                createDynamicType(descriptor, dynamicTypes, dcl);
            }
        } else {
            // have a dynamic class loader throw an exception.
            throw ValidationException.invalidClassLoaderForDynamicPersistence();
        }
    }
    createRestInterfaces(loader);
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ClassAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor) DynamicType(org.eclipse.persistence.dynamic.DynamicType)

Example 58 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class MetadataProject method createRestInterfaces.

private void createRestInterfaces(ClassLoader loader) {
    if (DynamicClassLoader.class.isAssignableFrom(loader.getClass())) {
        DynamicClassLoader dcl = (DynamicClassLoader) loader;
        for (EntityAccessor accessor : getEntityAccessors()) {
            String className = accessor.getParentClassName();
            if (className == null || getEntityAccessor(className) == null) {
                dcl.createDynamicAdapter(accessor.getJavaClassName());
            }
        }
        for (ClassAccessor classAccessor : getAllAccessors()) {
            String className = classAccessor.getParentClassName();
            if (className == null || getEntityAccessor(className) == null) {
                dcl.createDynamicCollectionAdapter(classAccessor.getJavaClassName());
                dcl.createDynamicReferenceAdapter(classAccessor.getJavaClassName());
            }
        }
    }
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) EntityAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor) ClassAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor)

Aggregations

DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)58 BeforeClass (org.junit.BeforeClass)27 DynamicTypeBuilder (org.eclipse.persistence.dynamic.DynamicTypeBuilder)26 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)17 Test (org.junit.Test)17 JPADynamicHelper (org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)15 JPADynamicTypeBuilder (org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder)13 HashMap (java.util.HashMap)7 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)6 DynamicType (org.eclipse.persistence.dynamic.DynamicType)5 IOException (java.io.IOException)4 TableSequence (org.eclipse.persistence.sequencing.TableSequence)4 EntityManager (jakarta.persistence.EntityManager)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 Project (org.eclipse.persistence.sessions.Project)3 EclipseLinkClassWriter (org.eclipse.persistence.dynamic.EclipseLinkClassWriter)2 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)2