From 9ca151b098ea9ae4fd1884a1105d5e742f232ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 15 Nov 2018 16:46:06 +0100 Subject: [PATCH] [WFLY-11381] Refactoring CdiValidatorFactoryService to use non deprecated ServiceBuilder methods. --- .../as/weld/CdiValidatorFactoryService.java | 29 ++++++------------- .../CdiBeanValidationFactoryProcessor.java | 14 ++++----- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/weld/bean-validation/src/main/java/org/jboss/as/weld/CdiValidatorFactoryService.java b/weld/bean-validation/src/main/java/org/jboss/as/weld/CdiValidatorFactoryService.java index 1e8fa25158e3..930132623fcf 100644 --- a/weld/bean-validation/src/main/java/org/jboss/as/weld/CdiValidatorFactoryService.java +++ b/weld/bean-validation/src/main/java/org/jboss/as/weld/CdiValidatorFactoryService.java @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.Set; +import java.util.function.Supplier; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.Default; @@ -36,13 +37,10 @@ import org.jboss.as.server.deployment.Attachments; import org.jboss.as.server.deployment.DeploymentUnit; import org.jboss.modules.Module; -import org.jboss.msc.inject.Injector; -import org.jboss.msc.service.Service; +import org.jboss.msc.Service; import org.jboss.msc.service.ServiceName; import org.jboss.msc.service.StartContext; -import org.jboss.msc.service.StartException; import org.jboss.msc.service.StopContext; -import org.jboss.msc.value.InjectedValue; import org.wildfly.security.manager.WildFlySecurityManager; /** @@ -50,12 +48,13 @@ * ValidatorFactory. * * @author Farah Juma + * @author Richard Opalka */ -public class CdiValidatorFactoryService implements Service { +public class CdiValidatorFactoryService implements Service { public static final ServiceName SERVICE_NAME = ServiceName.of("CdiValidatorFactoryService"); - private final InjectedValue beanManagerInjector = new InjectedValue<>(); + private final Supplier beanManagerSupplier; private final ClassLoader classLoader; @@ -66,21 +65,21 @@ public class CdiValidatorFactoryService implements Service beanManagerSupplier) { this.deploymentUnit = deploymentUnit; final Module module = this.deploymentUnit.getAttachment(Attachments.MODULE); this.classLoader = module.getClassLoader(); + this.beanManagerSupplier = beanManagerSupplier; } @Override - public void start(final StartContext context) throws StartException { + public void start(final StartContext context) { final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); try { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader); - BeanManager beanManager = beanManagerInjector.getValue(); // Get the CDI-enabled ValidatorFactory - ValidatorFactory validatorFactory = getReference(ValidatorFactory.class, beanManager); + ValidatorFactory validatorFactory = getReference(ValidatorFactory.class, beanManagerSupplier.get()); // Replace the delegate of LazyValidatorFactory LazyValidatorFactory lazyValidatorFactory = (LazyValidatorFactory)(deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY)); @@ -104,12 +103,6 @@ public void stop(final StopContext context) { } } - @Override - public CdiValidatorFactoryService getValue() - throws IllegalStateException, IllegalArgumentException { - return this; - } - private T getReference(Class clazz, BeanManager beanManager) { Set> beans = beanManager.getBeans(clazz, new AnnotationLiteral() {}); Iterator> i = beans.iterator(); @@ -122,8 +115,4 @@ private T getReference(Class clazz, BeanManager beanManager) { return (T) beanManager.getReference(bean, clazz, context); } - public Injector getBeanManagerInjector() { - return beanManagerInjector; - } - } diff --git a/weld/bean-validation/src/main/java/org/jboss/as/weld/deployment/processor/CdiBeanValidationFactoryProcessor.java b/weld/bean-validation/src/main/java/org/jboss/as/weld/deployment/processor/CdiBeanValidationFactoryProcessor.java index 166c2b993e9c..5922696d1e8c 100644 --- a/weld/bean-validation/src/main/java/org/jboss/as/weld/deployment/processor/CdiBeanValidationFactoryProcessor.java +++ b/weld/bean-validation/src/main/java/org/jboss/as/weld/deployment/processor/CdiBeanValidationFactoryProcessor.java @@ -27,25 +27,26 @@ import org.jboss.as.ee.weld.WeldDeploymentMarker; import org.jboss.as.server.deployment.DeploymentPhaseContext; import org.jboss.as.server.deployment.DeploymentUnit; -import org.jboss.as.server.deployment.DeploymentUnitProcessingException; import org.jboss.as.server.deployment.DeploymentUnitProcessor; import org.jboss.as.weld.CdiValidatorFactoryService; import org.jboss.as.weld.ServiceNames; -import org.jboss.msc.inject.CastingInjector; import org.jboss.msc.service.ServiceBuilder; import org.jboss.msc.service.ServiceName; import org.jboss.msc.service.ServiceTarget; +import java.util.function.Supplier; + /** * Deployment processor that replaces the delegate of LazyValidatorFactory with a CDI-enabled ValidatorFactory. * * @author Farah Juma * @author Martin Kouba + * @author Richard Opalka */ public class CdiBeanValidationFactoryProcessor implements DeploymentUnitProcessor { @Override - public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException { + public void deploy(DeploymentPhaseContext phaseContext) { final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit(); final DeploymentUnit topLevelDeployment = deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent(); final ServiceName weldStartService = topLevelDeployment.getServiceName().append(ServiceNames.WELD_START_SERVICE_NAME); @@ -57,11 +58,10 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro } final ServiceTarget serviceTarget = phaseContext.getServiceTarget(); final ServiceName serviceName = deploymentUnit.getServiceName().append(CdiValidatorFactoryService.SERVICE_NAME); - final CdiValidatorFactoryService cdiValidatorFactoryService = new CdiValidatorFactoryService(deploymentUnit); - final ServiceBuilder sb = serviceTarget.addService(serviceName, cdiValidatorFactoryService); - sb.addDependency(ServiceNames.beanManagerServiceName(deploymentUnit), Object.class, - new CastingInjector(cdiValidatorFactoryService.getBeanManagerInjector(), BeanManager.class)); + final ServiceBuilder sb = serviceTarget.addService(serviceName); + final Supplier beanManagerSupplier = sb.requires(ServiceNames.beanManagerServiceName(deploymentUnit)); sb.requires(weldStartService); + sb.setInstance(new CdiValidatorFactoryService(deploymentUnit, beanManagerSupplier)); sb.install(); }