Search in sources :

Example 1 with PersistenceUnitService

use of org.jboss.as.jpa.spi.PersistenceUnitService in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnitPhaseTwo.

/**
     * Second phase of starting the persistence unit
     *
     * @param phaseContext
     * @param deploymentUnit
     * @param eeModuleDescription
     * @param components
     * @param serviceTarget
     * @param classLoader
     * @param pu
     * @param provider
     * @param adaptor
     * @throws DeploymentUnitProcessingException
     */
private static void deployPersistenceUnitPhaseTwo(final DeploymentPhaseContext phaseContext, final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final Collection<ComponentDescription> components, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProvider provider, final PersistenceProviderAdaptor adaptor) throws DeploymentUnitProcessingException {
    TransactionManager transactionManager = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_MANAGER);
    TransactionSynchronizationRegistry transactionSynchronizationRegistry = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY);
    CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
    pu.setClassLoader(classLoader);
    try {
        ValidatorFactory validatorFactory = null;
        final HashMap<String, ValidatorFactory> properties = new HashMap<>();
        if (!ValidationMode.NONE.equals(pu.getValidationMode())) {
            // Get the CDI-enabled ValidatorFactory
            validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
        }
        BeanManagerAfterDeploymentValidation beanManagerAfterDeploymentValidation = registerJPAEntityListenerRegister(deploymentUnit);
        final PersistenceUnitServiceImpl service = new PersistenceUnitServiceImpl(classLoader, pu, adaptor, provider, PersistenceUnitRegistryImpl.INSTANCE, deploymentUnit.getServiceName(), validatorFactory, deploymentUnit.getAttachment(org.jboss.as.ee.naming.Attachments.JAVA_NAMESPACE_SETUP_ACTION), beanManagerAfterDeploymentValidation);
        final PersistenceAdaptorRemoval persistenceAdaptorRemoval = new PersistenceAdaptorRemoval(pu, adaptor);
        deploymentUnit.addToAttachmentList(REMOVAL_KEY, persistenceAdaptorRemoval);
        // add persistence provider specific properties
        adaptor.addProviderProperties(properties, pu);
        final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
        deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
        ServiceBuilder<PersistenceUnitService> builder = serviceTarget.addService(puServiceName, service);
        // the PU service has to depend on the JPAService which is responsible for setting up the necessary JPA infrastructure (like registering the cache EventListener(s))
        // @see https://issues.jboss.org/browse/WFLY-1531 for details
        builder.addDependency(JPAServiceNames.getJPAServiceName());
        // add dependency on first phase
        builder.addDependency(puServiceName.append(FIRST_PHASE), new CastingInjector<>(service.getPhaseOnePersistenceUnitServiceImplInjector(), PhaseOnePersistenceUnitServiceImpl.class));
        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 default datasource defined in the ee 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);
            }
        }
        // if the persistence unit is contained in a deployment that is a CDI bean archive (has beans.xml).
        if (WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
            builder.addDependency(beanManagerServiceName(deploymentUnit), new CastingInjector<BeanManager>(service.getBeanManagerInjector(), BeanManager.class));
        }
        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();
        }
        /**
             * handle extension that binds a transaction scoped entity manager to specified JNDI location
             */
        entityManagerBind(eeModuleDescription, serviceTarget, pu, puServiceName, transactionManager, transactionSynchronizationRegistry);
        /**
             * handle extension that binds an entity manager factory to specified JNDI location
             */
        entityManagerFactoryBind(eeModuleDescription, serviceTarget, pu, puServiceName);
        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 2 of 2) for '%s'.  PU is ready for injector action.", puServiceName);
        addManagementConsole(deploymentUnit, pu, adaptor, persistenceAdaptorRemoval);
    } catch (ServiceRegistryException e) {
        throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
    }
}
Also used : PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) 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) BeanManagerAfterDeploymentValidation(org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation) ServiceName(org.jboss.msc.service.ServiceName) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) PersistenceUnitService(org.jboss.as.jpa.spi.PersistenceUnitService) ProxyBeanManager(org.jboss.as.jpa.beanmanager.ProxyBeanManager) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 2 with PersistenceUnitService

use of org.jboss.as.jpa.spi.PersistenceUnitService in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnit.

/**
     * start the persistence unit in one phase
     *
     * @param phaseContext
     * @param deploymentUnit
     * @param eeModuleDescription
     * @param components
     * @param serviceTarget
     * @param classLoader
     * @param pu
     * @param startEarly
     * @param provider
     * @param adaptor
     * @param allowCdiBeanManagerAccess
     * @throws DeploymentUnitProcessingException
     */
private static void deployPersistenceUnit(final DeploymentPhaseContext phaseContext, final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final Collection<ComponentDescription> components, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final boolean startEarly, final PersistenceProvider provider, final PersistenceProviderAdaptor adaptor, final boolean allowCdiBeanManagerAccess) throws DeploymentUnitProcessingException {
    pu.setClassLoader(classLoader);
    TransactionManager transactionManager = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_MANAGER);
    TransactionSynchronizationRegistry transactionSynchronizationRegistry = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY);
    CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
    try {
        ValidatorFactory validatorFactory = null;
        final HashMap<String, ValidatorFactory> properties = new HashMap<>();
        if (!ValidationMode.NONE.equals(pu.getValidationMode())) {
            // Get the CDI-enabled ValidatorFactory
            validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
        }
        BeanManagerAfterDeploymentValidation beanManagerAfterDeploymentValidation = registerJPAEntityListenerRegister(deploymentUnit);
        final PersistenceUnitServiceImpl service = new PersistenceUnitServiceImpl(classLoader, pu, adaptor, provider, PersistenceUnitRegistryImpl.INSTANCE, deploymentUnit.getServiceName(), validatorFactory, deploymentUnit.getAttachment(org.jboss.as.ee.naming.Attachments.JAVA_NAMESPACE_SETUP_ACTION), beanManagerAfterDeploymentValidation);
        final PersistenceAdaptorRemoval persistenceAdaptorRemoval = new PersistenceAdaptorRemoval(pu, adaptor);
        deploymentUnit.addToAttachmentList(REMOVAL_KEY, persistenceAdaptorRemoval);
        // add persistence provider specific properties
        adaptor.addProviderProperties(properties, pu);
        final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
        deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
        ServiceBuilder<PersistenceUnitService> 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 default datasource defined in the ee 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);
            }
        }
        // if the persistence unit is contained in a deployment that is a CDI bean archive (has beans.xml).
        if (allowCdiBeanManagerAccess && WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
            builder.addDependency(beanManagerServiceName(deploymentUnit), new CastingInjector<BeanManager>(service.getBeanManagerInjector(), BeanManager.class));
        }
        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();
        }
        /**
             * handle extension that binds a transaction scoped entity manager to specified JNDI location
             */
        entityManagerBind(eeModuleDescription, serviceTarget, pu, puServiceName, transactionManager, transactionSynchronizationRegistry);
        /**
             * handle extension that binds an entity manager factory to specified JNDI location
             */
        entityManagerFactoryBind(eeModuleDescription, serviceTarget, pu, puServiceName);
        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 for '%s'.  PU is ready for injector action.", puServiceName);
        addManagementConsole(deploymentUnit, pu, adaptor, persistenceAdaptorRemoval);
    } catch (ServiceRegistryException e) {
        throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
    }
}
Also used : PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) ServiceRegistryException(org.jboss.msc.service.ServiceRegistryException) CapabilityServiceSupport(org.jboss.as.controller.capability.CapabilityServiceSupport) BeanManagerAfterDeploymentValidation(org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation) ServiceName(org.jboss.msc.service.ServiceName) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) PersistenceUnitService(org.jboss.as.jpa.spi.PersistenceUnitService) ProxyBeanManager(org.jboss.as.jpa.beanmanager.ProxyBeanManager) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 3 with PersistenceUnitService

use of org.jboss.as.jpa.spi.PersistenceUnitService in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnitPhaseTwo.

/**
 * Second phase of starting the persistence unit
 *
 * @param deploymentUnit
 * @param eeModuleDescription
 * @param serviceTarget
 * @param classLoader
 * @param pu
 * @param provider
 * @param adaptor
 * @throws DeploymentUnitProcessingException
 */
private static void deployPersistenceUnitPhaseTwo(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProvider provider, final PersistenceProviderAdaptor adaptor) throws DeploymentUnitProcessingException {
    TransactionManager transactionManager = ContextTransactionManager.getInstance();
    TransactionSynchronizationRegistry transactionSynchronizationRegistry = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY);
    CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
    pu.setClassLoader(classLoader);
    try {
        ValidatorFactory validatorFactory = null;
        final HashMap<String, ValidatorFactory> properties = new HashMap<>();
        if (!ValidationMode.NONE.equals(pu.getValidationMode()) && capabilitySupport.hasCapability("org.wildfly.bean-validation")) {
            // Get the Jakarta Contexts and Dependency Injection enabled ValidatorFactory
            validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
        }
        BeanManagerAfterDeploymentValidation beanManagerAfterDeploymentValidation = registerJPAEntityListenerRegister(deploymentUnit, capabilitySupport);
        final PersistenceAdaptorRemoval persistenceAdaptorRemoval = new PersistenceAdaptorRemoval(pu, adaptor);
        deploymentUnit.addToAttachmentList(REMOVAL_KEY, persistenceAdaptorRemoval);
        // add persistence provider specific properties
        adaptor.addProviderProperties(properties, pu);
        final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
        deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
        final PersistenceUnitServiceImpl service = new PersistenceUnitServiceImpl(properties, classLoader, pu, adaptor, provider, PersistenceUnitRegistryImpl.INSTANCE, deploymentUnit.getServiceName(), validatorFactory, deploymentUnit.getAttachment(org.jboss.as.ee.naming.Attachments.JAVA_NAMESPACE_SETUP_ACTION), beanManagerAfterDeploymentValidation);
        ServiceBuilder<PersistenceUnitService> builder = serviceTarget.addService(puServiceName, service);
        // the PU service has to depend on the JPAService which is responsible for setting up the necessary JPA infrastructure (like registering the cache EventListener(s))
        // @see https://issues.jboss.org/browse/WFLY-1531 for details
        builder.requires(JPAServiceNames.getJPAServiceName());
        // add dependency on first phase
        builder.addDependency(puServiceName.append(FIRST_PHASE), Object.class, new CastingInjector<>(service.getPhaseOnePersistenceUnitServiceImplInjector(), PhaseOnePersistenceUnitServiceImpl.class));
        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 Jakarta Transactions datasource
        if (useDefaultDataSource) {
            // try the default datasource defined in the ee subsystem
            String defaultJtaDataSource = null;
            if (eeModuleDescription != null) {
                defaultJtaDataSource = eeModuleDescription.getDefaultResourceJndiNames().getDataSource();
            }
            if (defaultJtaDataSource == null || defaultJtaDataSource.isEmpty()) {
                // try the datasource defined in the Jakarta Persistence 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);
            }
        }
        // JPA 2.1 sections 3.5.1 + 9.1 require the Jakarta Contexts and Dependency Injection bean manager to be passed to the persistence provider
        // if the persistence unit is contained in a deployment that is a Jakarta Contexts and Dependency Injection bean archive (has beans.xml).
        final CapabilityServiceSupport support = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
        if (support.hasCapability(WELD_CAPABILITY_NAME)) {
            support.getOptionalCapabilityRuntimeAPI(WELD_CAPABILITY_NAME, WeldCapability.class).get().addBeanManagerService(deploymentUnit, builder, service.getBeanManagerInjector());
        }
        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();
        }
        /**
         * handle extension that binds a transaction scoped entity manager to specified JNDI location
         */
        entityManagerBind(eeModuleDescription, serviceTarget, pu, puServiceName, transactionManager, transactionSynchronizationRegistry);
        /**
         * handle extension that binds an entity manager factory to specified JNDI location
         */
        entityManagerFactoryBind(eeModuleDescription, serviceTarget, pu, puServiceName);
        // get async executor from Services.addServerExecutorDependency
        addServerExecutorDependency(builder, service.getExecutorInjector());
        builder.install();
        ROOT_LOGGER.tracef("added PersistenceUnitService (phase 2 of 2) for '%s'.  PU is ready for injector action.", puServiceName);
        addManagementConsole(deploymentUnit, pu, adaptor, persistenceAdaptorRemoval);
    } catch (ServiceRegistryException e) {
        throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
    }
}
Also used : PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) 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) BeanManagerAfterDeploymentValidation(org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation) ServiceName(org.jboss.msc.service.ServiceName) ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) PersistenceUnitService(org.jboss.as.jpa.spi.PersistenceUnitService)

Example 4 with PersistenceUnitService

use of org.jboss.as.jpa.spi.PersistenceUnitService in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnit.

/**
 * start the persistence unit in one phase
 *
 * @param deploymentUnit
 * @param eeModuleDescription
 * @param serviceTarget
 * @param classLoader
 * @param pu
 * @param provider
 * @param adaptor
 * @param allowCdiBeanManagerAccess
 * @throws DeploymentUnitProcessingException
 */
private static void deployPersistenceUnit(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProvider provider, final PersistenceProviderAdaptor adaptor, final boolean allowCdiBeanManagerAccess) throws DeploymentUnitProcessingException {
    pu.setClassLoader(classLoader);
    TransactionManager transactionManager = ContextTransactionManager.getInstance();
    TransactionSynchronizationRegistry transactionSynchronizationRegistry = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY);
    CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
    try {
        ValidatorFactory validatorFactory = null;
        final HashMap<String, ValidatorFactory> properties = new HashMap<>();
        CapabilityServiceSupport css = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
        if (!ValidationMode.NONE.equals(pu.getValidationMode()) && css.hasCapability("org.wildfly.bean-validation")) {
            // Get the Jakarta Contexts and Dependency Injection enabled ValidatorFactory
            validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
        }
        BeanManagerAfterDeploymentValidation beanManagerAfterDeploymentValidation = registerJPAEntityListenerRegister(deploymentUnit, capabilitySupport);
        final PersistenceAdaptorRemoval persistenceAdaptorRemoval = new PersistenceAdaptorRemoval(pu, adaptor);
        deploymentUnit.addToAttachmentList(REMOVAL_KEY, persistenceAdaptorRemoval);
        // add persistence provider specific properties
        adaptor.addProviderProperties(properties, pu);
        final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
        deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
        deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
        final PersistenceUnitServiceImpl service = new PersistenceUnitServiceImpl(properties, classLoader, pu, adaptor, provider, PersistenceUnitRegistryImpl.INSTANCE, deploymentUnit.getServiceName(), validatorFactory, deploymentUnit.getAttachment(org.jboss.as.ee.naming.Attachments.JAVA_NAMESPACE_SETUP_ACTION), beanManagerAfterDeploymentValidation);
        ServiceBuilder<PersistenceUnitService> 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 Jakarta Transactions datasource
        if (useDefaultDataSource) {
            // try the default datasource defined in the ee subsystem
            String defaultJtaDataSource = null;
            if (eeModuleDescription != null) {
                defaultJtaDataSource = eeModuleDescription.getDefaultResourceJndiNames().getDataSource();
            }
            if (defaultJtaDataSource == null || defaultJtaDataSource.isEmpty()) {
                // try the datasource defined in the Jakarta Persistence 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);
            }
        }
        // JPA 2.1 sections 3.5.1 + 9.1 require the Jakarta Contexts and Dependency Injection bean manager to be passed to the peristence provider
        // if the persistence unit is contained in a deployment that is a Jakarta Contexts and Dependency Injection bean archive (has beans.xml).
        final CapabilityServiceSupport support = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
        if (support.hasCapability(WELD_CAPABILITY_NAME) && allowCdiBeanManagerAccess) {
            support.getOptionalCapabilityRuntimeAPI(WELD_CAPABILITY_NAME, WeldCapability.class).get().addBeanManagerService(deploymentUnit, builder, service.getBeanManagerInjector());
        }
        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();
        }
        /**
         * handle extension that binds a transaction scoped entity manager to specified JNDI location
         */
        entityManagerBind(eeModuleDescription, serviceTarget, pu, puServiceName, transactionManager, transactionSynchronizationRegistry);
        /**
         * handle extension that binds an entity manager factory to specified JNDI location
         */
        entityManagerFactoryBind(eeModuleDescription, serviceTarget, pu, puServiceName);
        // get async executor from Services.addServerExecutorDependency
        addServerExecutorDependency(builder, service.getExecutorInjector());
        builder.install();
        ROOT_LOGGER.tracef("added PersistenceUnitService for '%s'.  PU is ready for injector action.", puServiceName);
        addManagementConsole(deploymentUnit, pu, adaptor, persistenceAdaptorRemoval);
    } catch (ServiceRegistryException e) {
        throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
    }
}
Also used : PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) ServiceRegistryException(org.jboss.msc.service.ServiceRegistryException) CapabilityServiceSupport(org.jboss.as.controller.capability.CapabilityServiceSupport) BeanManagerAfterDeploymentValidation(org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation) ServiceName(org.jboss.msc.service.ServiceName) ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) PersistenceUnitService(org.jboss.as.jpa.spi.PersistenceUnitService)

Aggregations

HashMap (java.util.HashMap)4 TransactionManager (javax.transaction.TransactionManager)4 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)4 ValidatorFactory (javax.validation.ValidatorFactory)4 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)4 BeanManagerAfterDeploymentValidation (org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation)4 PersistenceUnitServiceImpl (org.jboss.as.jpa.service.PersistenceUnitServiceImpl)4 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)4 PersistenceUnitService (org.jboss.as.jpa.spi.PersistenceUnitService)4 ServiceName (org.jboss.msc.service.ServiceName)4 ServiceRegistryException (org.jboss.msc.service.ServiceRegistryException)4 BeanManager (javax.enterprise.inject.spi.BeanManager)2 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)2 ContextTransactionManager (org.wildfly.transaction.client.ContextTransactionManager)2