Skip to content

Commit

Permalink
[WFLY-11381] Refactoring CdiValidatorFactoryService to use non deprec…
Browse files Browse the repository at this point in the history
…ated ServiceBuilder methods.
  • Loading branch information
ropalka committed Nov 20, 2018
1 parent c847ec1 commit 9ca151b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
Expand Up @@ -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;
Expand All @@ -36,26 +37,24 @@
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;

/**
* Service that replaces the delegate of LazyValidatorFactory with a CDI-enabled
* ValidatorFactory.
*
* @author Farah Juma
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class CdiValidatorFactoryService implements Service<CdiValidatorFactoryService> {
public class CdiValidatorFactoryService implements Service {

public static final ServiceName SERVICE_NAME = ServiceName.of("CdiValidatorFactoryService");

private final InjectedValue<BeanManager> beanManagerInjector = new InjectedValue<>();
private final Supplier<BeanManager> beanManagerSupplier;

private final ClassLoader classLoader;

Expand All @@ -66,21 +65,21 @@ public class CdiValidatorFactoryService implements Service<CdiValidatorFactorySe
*
* @param deploymentUnit the deployment unit
*/
public CdiValidatorFactoryService(DeploymentUnit deploymentUnit) {
public CdiValidatorFactoryService(final DeploymentUnit deploymentUnit, final Supplier<BeanManager> 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));
Expand All @@ -104,12 +103,6 @@ public void stop(final StopContext context) {
}
}

@Override
public CdiValidatorFactoryService getValue()
throws IllegalStateException, IllegalArgumentException {
return this;
}

private <T> T getReference(Class<T> clazz, BeanManager beanManager) {
Set<Bean<?>> beans = beanManager.getBeans(clazz, new AnnotationLiteral<Default>() {});
Iterator<Bean<?>> i = beans.iterator();
Expand All @@ -122,8 +115,4 @@ private <T> T getReference(Class<T> clazz, BeanManager beanManager) {
return (T) beanManager.getReference(bean, clazz, context);
}

public Injector<BeanManager> getBeanManagerInjector() {
return beanManagerInjector;
}

}
Expand Up @@ -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 <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
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);
Expand All @@ -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<BeanManager>(cdiValidatorFactoryService.getBeanManagerInjector(), BeanManager.class));
final ServiceBuilder<?> sb = serviceTarget.addService(serviceName);
final Supplier<BeanManager> beanManagerSupplier = sb.requires(ServiceNames.beanManagerServiceName(deploymentUnit));
sb.requires(weldStartService);
sb.setInstance(new CdiValidatorFactoryService(deploymentUnit, beanManagerSupplier));
sb.install();
}

Expand Down

0 comments on commit 9ca151b

Please sign in to comment.