Search in sources :

Example 1 with PhaseOnePersistenceUnitServiceImpl

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

Example 2 with PhaseOnePersistenceUnitServiceImpl

use of org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method deployPersistenceUnitPhaseOne.

/**
 * first phase of starting the persistence unit
 *
 * @param deploymentUnit
 * @param eeModuleDescription
 * @param serviceTarget
 * @param classLoader
 * @param pu
 * @param adaptor
 * @throws DeploymentUnitProcessingException
 */
private static void deployPersistenceUnitPhaseOne(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, 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;
        // 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);
        boolean partOfWeldDeployment = false;
        if (support.hasCapability(WELD_CAPABILITY_NAME)) {
            partOfWeldDeployment = support.getOptionalCapabilityRuntimeAPI(WELD_CAPABILITY_NAME, WeldCapability.class).get().isPartOfWeldDeployment(deploymentUnit);
        }
        if (partOfWeldDeployment) {
            proxyBeanManager = new ProxyBeanManager();
            // register Jakarta Contexts and Dependency Injection extension before WeldDeploymentProcessor, which is important for
            registerJPAEntityListenerRegister(deploymentUnit, support);
        // EAR deployments that contain a WAR that has persistence units defined.
        }
        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);
        final PhaseOnePersistenceUnitServiceImpl service = new PhaseOnePersistenceUnitServiceImpl(classLoader, pu, adaptor, deploymentUnit.getServiceName(), proxyBeanManager);
        service.getPropertiesInjector().inject(properties);
        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 Jakarta Transactions datasource
        if (useDefaultDataSource) {
            // try the one defined in the Jakarta Persistence 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();
        }
        // get async executor from Services.addServerExecutorDependency
        addServerExecutorDependency(builder, service.getExecutorInjector());
        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) WeldCapability(org.jboss.as.weld.WeldCapability) 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)

Example 3 with PhaseOnePersistenceUnitServiceImpl

use of org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl in project fakereplace by fakereplace.

the class WildflyHibernate5ClassChangeAware method afterChange.

@Override
public void afterChange(final List<ChangedClass> changed, final List<NewClassData> added) {
    boolean replace = false;
    for (ChangedClass changedClass : changed) {
        if (changedClass.getChangedClass().isAnnotationPresent(Entity.class) || !changedClass.getChangedAnnotationsByType(Entity.class).isEmpty()) {
            replace = true;
        }
    }
    if (!replace) {
        return;
    }
    final Set<PersistenceUnitServiceImpl> puServices = (Set<PersistenceUnitServiceImpl>) InstanceTracker.get(WildflyHibernate5Extension.PERSISTENCE_UNIT_SERVICE);
    final Set<PhaseOnePersistenceUnitServiceImpl> phaseOneServices = (Set<PhaseOnePersistenceUnitServiceImpl>) InstanceTracker.get(WildflyHibernate5Extension.PERSISTENCE_PHASE_ONE_SERVICE);
    // we need to update this index
    try {
        WritableServiceBasedNamingStore.pushOwner(CurrentServiceContainer.getServiceContainer());
        try {
            for (PersistenceUnitServiceImpl puService : puServices) {
                try {
                    // make sure the service is started before stopping
                    if (puService.getExecutorInjector().getOptionalValue() != null) {
                        doServiceStop(puService);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            for (PhaseOnePersistenceUnitServiceImpl puService : phaseOneServices) {
                try {
                    if (puService.getExecutorInjector().getOptionalValue() != null) {
                        doServiceStop(puService);
                        doServiceStart(puService);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            for (PersistenceUnitServiceImpl puService : puServices) {
                try {
                    // make sure the service is started before stopping
                    if (puService.getExecutorInjector().getOptionalValue() != null) {
                        doServiceStart(puService);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } finally {
            WritableServiceBasedNamingStore.popOwner();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Entity(javax.persistence.Entity) PersistenceUnitServiceImpl(org.jboss.as.jpa.service.PersistenceUnitServiceImpl) PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) Set(java.util.Set) ChangedClass(org.fakereplace.api.ChangedClass) PhaseOnePersistenceUnitServiceImpl(org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl) StartException(org.jboss.msc.service.StartException)

Aggregations

PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3 HashMap (java.util.HashMap)2 ValidatorFactory (javax.validation.ValidatorFactory)2 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)2 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)2 ServiceName (org.jboss.msc.service.ServiceName)2 ServiceRegistryException (org.jboss.msc.service.ServiceRegistryException)2 Set (java.util.Set)1 Entity (javax.persistence.Entity)1 ChangedClass (org.fakereplace.api.ChangedClass)1 PersistenceUnitServiceImpl (org.jboss.as.jpa.service.PersistenceUnitServiceImpl)1 WeldCapability (org.jboss.as.weld.WeldCapability)1 StartException (org.jboss.msc.service.StartException)1