Search in sources :

Example 1 with ProxyBeanManager

use of org.jboss.as.jpa.beanmanager.ProxyBeanManager 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)

Example 2 with ProxyBeanManager

use of org.jboss.as.jpa.beanmanager.ProxyBeanManager in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnitPhaseOne.

/**
     * first phase of starting the persistence unit
     *
     * @param phaseContext
     * @param deploymentUnit
     * @param eeModuleDescription
     * @param components
     * @param serviceTarget
     * @param classLoader
     * @param pu
     * @param adaptor
     * @throws DeploymentUnitProcessingException
     */
private static void deployPersistenceUnitPhaseOne(final DeploymentPhaseContext phaseContext, final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final Collection<ComponentDescription> components, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProviderAdaptor adaptor) throws DeploymentUnitProcessingException {
    CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
    pu.setClassLoader(classLoader);
    try {
        final HashMap<String, ValidatorFactory> properties = new HashMap<>();
        ProxyBeanManager proxyBeanManager = null;
        // if the persistence unit is contained in a deployment that is a CDI bean archive (has beans.xml).
        if (WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
            proxyBeanManager = new ProxyBeanManager();
        }
        final PhaseOnePersistenceUnitServiceImpl service = new PhaseOnePersistenceUnitServiceImpl(classLoader, pu, adaptor, deploymentUnit.getServiceName(), proxyBeanManager);
        deploymentUnit.addToAttachmentList(REMOVAL_KEY, new PersistenceAdaptorRemoval(pu, adaptor));
        // add persistence provider specific properties
        adaptor.addProviderProperties(properties, pu);
        final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu).append(FIRST_PHASE);
        deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
        ServiceBuilder<PhaseOnePersistenceUnitServiceImpl> builder = serviceTarget.addService(puServiceName, service);
        boolean useDefaultDataSource = Configuration.allowDefaultDataSourceUse(pu);
        final String jtaDataSource = adjustJndi(pu.getJtaDataSourceName());
        final String nonJtaDataSource = adjustJndi(pu.getNonJtaDataSourceName());
        if (jtaDataSource != null && jtaDataSource.length() > 0) {
            if (jtaDataSource.equals(EE_DEFAULT_DATASOURCE)) {
                // explicit use of default datasource
                useDefaultDataSource = true;
            } else {
                builder.addDependency(ContextNames.bindInfoForEnvEntry(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), false, jtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getJtaDataSourceInjector()));
                useDefaultDataSource = false;
            }
        }
        if (nonJtaDataSource != null && nonJtaDataSource.length() > 0) {
            builder.addDependency(ContextNames.bindInfoForEnvEntry(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), false, nonJtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getNonJtaDataSourceInjector()));
            useDefaultDataSource = false;
        }
        // JPA 2.0 8.2.1.5, container provides default JTA datasource
        if (useDefaultDataSource) {
            // try the one defined in the jpa subsystem
            String defaultJtaDataSource = null;
            if (eeModuleDescription != null) {
                defaultJtaDataSource = eeModuleDescription.getDefaultResourceJndiNames().getDataSource();
            }
            if (defaultJtaDataSource == null || defaultJtaDataSource.isEmpty()) {
                // try the datasource defined in the JPA subsystem
                defaultJtaDataSource = adjustJndi(JPAService.getDefaultDataSourceName());
            }
            if (defaultJtaDataSource != null && !defaultJtaDataSource.isEmpty()) {
                builder.addDependency(ContextNames.bindInfoFor(defaultJtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getJtaDataSourceInjector()));
                ROOT_LOGGER.tracef("%s is using the default data source '%s'", puServiceName, defaultJtaDataSource);
            }
        }
        try {
            // save a thread local reference to the builder for setting up the second level cache dependencies
            CacheDeploymentListener.setInternalDeploymentSupport(builder, capabilitySupport);
            adaptor.addProviderDependencies(pu);
        } finally {
            CacheDeploymentListener.clearInternalDeploymentSupport();
        }
        builder.setInitialMode(ServiceController.Mode.ACTIVE).addInjection(service.getPropertiesInjector(), properties);
        // get async executor from Services.addServerExecutorDependency
        addServerExecutorDependency(builder, service.getExecutorInjector(), false);
        builder.install();
        ROOT_LOGGER.tracef("added PersistenceUnitService (phase 1 of 2) for '%s'.  PU is ready for injector action.", puServiceName);
    } catch (ServiceRegistryException e) {
        throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) ServiceRegistryException(org.jboss.msc.service.ServiceRegistryException) CapabilityServiceSupport(org.jboss.as.controller.capability.CapabilityServiceSupport) ServiceName(org.jboss.msc.service.ServiceName) ProxyBeanManager(org.jboss.as.jpa.beanmanager.ProxyBeanManager)

Aggregations

ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)2 AccessControlContext (java.security.AccessControlContext)1 PrivilegedAction (java.security.PrivilegedAction)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ValidatorFactory (javax.validation.ValidatorFactory)1 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)1 TempClassLoaderFactoryImpl (org.jboss.as.jpa.classloader.TempClassLoaderFactoryImpl)1 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)1 ServiceName (org.jboss.msc.service.ServiceName)1 ServiceRegistryException (org.jboss.msc.service.ServiceRegistryException)1 StartException (org.jboss.msc.service.StartException)1 EntityManagerFactoryBuilder (org.jipijapa.plugin.spi.EntityManagerFactoryBuilder)1