Search in sources :

Example 1 with EntityManagerFactoryBuilder

use of org.jipijapa.plugin.spi.EntityManagerFactoryBuilder in project wildfly by wildfly.

the class PersistenceUnitServiceImpl method start.

@Override
public void start(final StartContext context) throws StartException {
    final ExecutorService executor = executorInjector.getValue();
    final AccessControlContext accessControlContext = AccessController.doPrivileged(GetAccessControlContextAction.getInstance());
    final Runnable task = new Runnable() {

        // run async in a background thread
        @Override
        public void run() {
            PrivilegedAction<Void> privilegedAction = new PrivilegedAction<Void>() {

                // run as security privileged action
                @Override
                public Void run() {
                    ClassLoader old = Thread.currentThread().getContextClassLoader();
                    Thread.currentThread().setContextClassLoader(classLoader);
                    if (javaNamespaceSetup != null) {
                        javaNamespaceSetup.setup(Collections.<String, Object>emptyMap());
                    }
                    try {
                        PhaseOnePersistenceUnitServiceImpl phaseOnePersistenceUnitService = phaseOnePersistenceUnitServiceInjectedValue.getOptionalValue();
                        WritableServiceBasedNamingStore.pushOwner(deploymentUnitServiceName);
                        Object wrapperBeanManagerLifeCycle = null;
                        // creating container EntityManagerFactory
                        if (validatorFactory != null) {
                            properties.getValue().put(VALIDATOR_FACTORY, validatorFactory);
                        }
                        // handle phase 2 of 2 of bootstrapping the persistence unit
                        if (phaseOnePersistenceUnitService != null) {
                            ROOT_LOGGER.startingPersistenceUnitService(2, pu.getScopedPersistenceUnitName());
                            // indicate that the second phase of bootstrapping the persistence unit has started
                            phaseOnePersistenceUnitService.setSecondPhaseStarted(true);
                            if (beanManagerInjector.getOptionalValue() != null) {
                                wrapperBeanManagerLifeCycle = phaseOnePersistenceUnitService.getBeanManagerLifeCycle();
                                // update the bean manager proxy to the actual CDI bean manager
                                proxyBeanManager = phaseOnePersistenceUnitService.getBeanManager();
                                proxyBeanManager.setDelegate(beanManagerInjector.getOptionalValue());
                            }
                            EntityManagerFactoryBuilder emfBuilder = phaseOnePersistenceUnitService.getEntityManagerFactoryBuilder();
                            // persistence unit bootstrap.
                            if (validatorFactory != null) {
                                emfBuilder.withValidatorFactory(validatorFactory);
                            }
                            // get the EntityManagerFactory from the second phase of the persistence unit bootstrap
                            entityManagerFactory = emfBuilder.build();
                        } else {
                            ROOT_LOGGER.startingService("Persistence Unit", pu.getScopedPersistenceUnitName());
                            // start the persistence unit in one pass (1 of 1)
                            pu.setTempClassLoaderFactory(new TempClassLoaderFactoryImpl(classLoader));
                            pu.setJtaDataSource(jtaDataSource.getOptionalValue());
                            pu.setNonJtaDataSource(nonJtaDataSource.getOptionalValue());
                            if (beanManagerInjector.getOptionalValue() != null) {
                                proxyBeanManager = new ProxyBeanManager();
                                proxyBeanManager.setDelegate(beanManagerInjector.getOptionalValue());
                                wrapperBeanManagerLifeCycle = persistenceProviderAdaptor.beanManagerLifeCycle(proxyBeanManager);
                                if (wrapperBeanManagerLifeCycle != null) {
                                    // pass the wrapper object representing the bean manager life cycle object
                                    properties.getValue().put(CDI_BEAN_MANAGER, wrapperBeanManagerLifeCycle);
                                } else {
                                    properties.getValue().put(CDI_BEAN_MANAGER, proxyBeanManager);
                                }
                            }
                            entityManagerFactory = createContainerEntityManagerFactory();
                        }
                        persistenceUnitRegistry.add(getScopedPersistenceUnitName(), getValue());
                        if (wrapperBeanManagerLifeCycle != null) {
                            beanManagerAfterDeploymentValidation.register(persistenceProviderAdaptor, wrapperBeanManagerLifeCycle);
                        }
                        context.complete();
                    } catch (Throwable t) {
                        context.failed(new StartException(t));
                    } finally {
                        Thread.currentThread().setContextClassLoader(old);
                        // release the temp classloader factory (only needed when creating the EMF)
                        pu.setTempClassLoaderFactory(null);
                        WritableServiceBasedNamingStore.popOwner();
                        if (javaNamespaceSetup != null) {
                            javaNamespaceSetup.teardown(Collections.<String, Object>emptyMap());
                        }
                    }
                    return null;
                }
            };
            WildFlySecurityManager.doChecked(privilegedAction, accessControlContext);
        }
    };
    try {
        executor.execute(task);
    } catch (RejectedExecutionException e) {
        task.run();
    } finally {
        context.asynchronous();
    }
}
Also used : EntityManagerFactoryBuilder(org.jipijapa.plugin.spi.EntityManagerFactoryBuilder) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) ExecutorService(java.util.concurrent.ExecutorService) TempClassLoaderFactoryImpl(org.jboss.as.jpa.classloader.TempClassLoaderFactoryImpl) ProxyBeanManager(org.jboss.as.jpa.beanmanager.ProxyBeanManager) StartException(org.jboss.msc.service.StartException)

Aggregations

AccessControlContext (java.security.AccessControlContext)1 PrivilegedAction (java.security.PrivilegedAction)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)1 TempClassLoaderFactoryImpl (org.jboss.as.jpa.classloader.TempClassLoaderFactoryImpl)1 StartException (org.jboss.msc.service.StartException)1 EntityManagerFactoryBuilder (org.jipijapa.plugin.spi.EntityManagerFactoryBuilder)1