Skip to content

Commit

Permalink
[WFLY-11381] Refactoring WeldBootstrapService to use non deprecated S…
Browse files Browse the repository at this point in the history
…erviceBuilder methods.
  • Loading branch information
ropalka committed Nov 20, 2018
1 parent 75d1584 commit f1e7237
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
Expand Up @@ -28,18 +28,19 @@
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.enterprise.inject.spi.BeanManager;

import org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl;
import org.jboss.as.weld.deployment.WeldDeployment;
import org.jboss.as.weld.logging.WeldLogger;
import org.jboss.as.weld.services.ModuleGroupSingletonProvider;
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.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.weld.Container;
import org.jboss.weld.ContainerState;
import org.jboss.weld.bootstrap.WeldBootstrap;
Expand All @@ -56,8 +57,9 @@
* the point that the bean manager is available.
*
* @author Stuart Douglas
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class WeldBootstrapService implements Service<WeldBootstrapService> {
public class WeldBootstrapService implements Service {

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

Expand All @@ -68,18 +70,29 @@ public class WeldBootstrapService implements Service<WeldBootstrapService> {
private final BeanDeploymentArchiveImpl rootBeanDeploymentArchive;

private final String deploymentName;

private final InjectedValue<SecurityServices> securityServices = new InjectedValue<SecurityServices>();
private final InjectedValue<TransactionServices> weldTransactionServices = new InjectedValue<TransactionServices>();
private final InjectedValue<ExecutorServices> executorServices = new InjectedValue<ExecutorServices>();
private final InjectedValue<ExecutorService> serverExecutor = new InjectedValue<ExecutorService>();
private final Consumer<WeldBootstrapService> weldBootstrapServiceConsumer;
private final Supplier<ExecutorServices> executorServicesSupplier;
private final Supplier<ExecutorService> serverExecutorSupplier;
private final Supplier<SecurityServices> securityServicesSupplier;
private final Supplier<TransactionServices> weldTransactionServicesSupplier;

private volatile boolean started;

public WeldBootstrapService(WeldDeployment deployment, Environment environment, final String deploymentName) {
public WeldBootstrapService(final WeldDeployment deployment, final Environment environment, final String deploymentName,
final Consumer<WeldBootstrapService> weldBootstrapServiceConsumer,
final Supplier<ExecutorServices> executorServicesSupplier,
final Supplier<ExecutorService> serverExecutorSupplier,
final Supplier<SecurityServices> securityServicesSupplier,
final Supplier<TransactionServices> weldTransactionServicesSupplier
) {
this.deployment = deployment;
this.environment = environment;
this.deploymentName = deploymentName;
this.weldBootstrapServiceConsumer = weldBootstrapServiceConsumer;
this.executorServicesSupplier = executorServicesSupplier;
this.serverExecutorSupplier = serverExecutorSupplier;
this.securityServicesSupplier = securityServicesSupplier;
this.weldTransactionServicesSupplier = weldTransactionServicesSupplier;
this.bootstrap = new WeldBootstrap();
Map<String, BeanDeploymentArchive> bdas = new HashMap<String, BeanDeploymentArchive>();
BeanDeploymentArchiveImpl rootBeanDeploymentArchive = null;
Expand Down Expand Up @@ -109,15 +122,15 @@ public synchronized void start(final StartContext context) {

WeldLogger.DEPLOYMENT_LOGGER.startingWeldService(deploymentName);
// set up injected services
addWeldService(SecurityServices.class, securityServices.getValue());
addWeldService(SecurityServices.class, securityServicesSupplier.get());

TransactionServices transactionServices = weldTransactionServices.getOptionalValue();
TransactionServices transactionServices = weldTransactionServicesSupplier.get();
if (transactionServices != null) {
addWeldService(TransactionServices.class, transactionServices);
}

if (!deployment.getServices().contains(ExecutorServices.class)) {
addWeldService(ExecutorServices.class, executorServices.getValue());
addWeldService(ExecutorServices.class, executorServicesSupplier.get());
}

ModuleGroupSingletonProvider.addClassLoaders(deployment.getModule().getClassLoader(),
Expand All @@ -131,19 +144,20 @@ public synchronized void start(final StartContext context) {
} finally {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
}

weldBootstrapServiceConsumer.accept(this);
}

/**
* This is a no-op if {@link WeldStartService#start(StartContext)} completes normally and the shutdown is performed in
* {@link WeldStartService#stop(org.jboss.msc.service.StopContext)}.
*/
public synchronized void stop(final StopContext context) {
weldBootstrapServiceConsumer.accept(null);
if (started) {
// WeldStartService#stop() not completed - attempt to perform the container cleanup
final Container container = Container.instance(deploymentName);
if (container != null && !ContainerState.SHUTDOWN.equals(container.getState())) {
final ExecutorService executorService = serverExecutor.getValue();
final ExecutorService executorService = serverExecutorSupplier.get();
final Runnable task = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -238,24 +252,4 @@ WeldBootstrap getBootstrap() {
return bootstrap;
}

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

public InjectedValue<SecurityServices> getSecurityServices() {
return securityServices;
}

public InjectedValue<TransactionServices> getWeldTransactionServices() {
return weldTransactionServices;
}

public InjectedValue<ExecutorServices> getExecutorServices() {
return executorServices;
}

public InjectedValue<ExecutorService> getServerExecutor() {
return serverExecutor;
}
}
Expand Up @@ -33,6 +33,8 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.enterprise.inject.spi.Extension;

Expand Down Expand Up @@ -94,6 +96,7 @@
*
* @author Stuart Douglas
* @author <a href="mailto:tadamski@redhat.com">Tomasz Adamski</a>
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class WeldDeploymentProcessor implements DeploymentUnitProcessor {

Expand Down Expand Up @@ -225,21 +228,16 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro

final WeldDeployment deployment = new WeldDeployment(beanDeploymentArchives, extensions, module, subDeploymentLoaders, deploymentUnit, rootBeanDeploymentModule, eeModuleDescriptors);

final WeldBootstrapService weldBootstrapService = new WeldBootstrapService(deployment, WildFlyWeldEnvironment.INSTANCE, deploymentUnit.getName());

installBootstrapConfigurationService(deployment, parent);

// Add root module services to WeldDeployment
for (Entry<Class<? extends Service>, Service> entry : rootModuleServices.entrySet()) {
weldBootstrapService.addWeldService(entry.getKey(), Reflections.cast(entry.getValue()));
}

// add the weld service
final ServiceBuilder<WeldBootstrapService> weldBootstrapServiceBuilder = serviceTarget.addService(weldBootstrapServiceName, weldBootstrapService);

final ServiceBuilder<?> weldBootstrapServiceBuilder = serviceTarget.addService(weldBootstrapServiceName);
final Consumer<WeldBootstrapService> weldBootstrapServiceConsumer = weldBootstrapServiceBuilder.provides(weldBootstrapServiceName);
weldBootstrapServiceBuilder.requires(TCCLSingletonService.SERVICE_NAME);
weldBootstrapServiceBuilder.addDependency(WeldExecutorServices.SERVICE_NAME, ExecutorServices.class, weldBootstrapService.getExecutorServices());
weldBootstrapServiceBuilder.addDependency(Services.JBOSS_SERVER_EXECUTOR, ExecutorService.class, weldBootstrapService.getServerExecutor());
final Supplier<ExecutorServices> executorServicesSupplier = weldBootstrapServiceBuilder.requires(WeldExecutorServices.SERVICE_NAME);
final Supplier<ExecutorService> serverExecutorSupplier = weldBootstrapServiceBuilder.requires(Services.JBOSS_SERVER_EXECUTOR);
Supplier<SecurityServices> securityServicesSupplier = null;
Supplier<TransactionServices> weldTransactionServicesSupplier = null;

// Install additional services
final ServiceLoader<BootstrapDependencyInstaller> installers = ServiceLoader.load(BootstrapDependencyInstaller.class,
Expand All @@ -248,12 +246,18 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
ServiceName serviceName = installer.install(serviceTarget, deploymentUnit, jtsEnabled);
// Add dependency for recognized services
if (ServiceNames.WELD_SECURITY_SERVICES_SERVICE_NAME.getSimpleName().equals(serviceName.getSimpleName())) {
weldBootstrapServiceBuilder.addDependency(serviceName, SecurityServices.class, weldBootstrapService.getSecurityServices());
securityServicesSupplier = weldBootstrapServiceBuilder.requires(serviceName);
} else if (ServiceNames.WELD_TRANSACTION_SERVICES_SERVICE_NAME.getSimpleName().equals(serviceName.getSimpleName())) {
weldBootstrapServiceBuilder.addDependency(serviceName, TransactionServices.class, weldBootstrapService.getWeldTransactionServices());
weldTransactionServicesSupplier = weldBootstrapServiceBuilder.requires(serviceName);
}
}

final WeldBootstrapService weldBootstrapService = new WeldBootstrapService(deployment, WildFlyWeldEnvironment.INSTANCE, deploymentUnit.getName(),
weldBootstrapServiceConsumer, executorServicesSupplier, serverExecutorSupplier, securityServicesSupplier, weldTransactionServicesSupplier);
// Add root module services to WeldDeployment
for (Entry<Class<? extends Service>, Service> entry : rootModuleServices.entrySet()) {
weldBootstrapService.addWeldService(entry.getKey(), Reflections.cast(entry.getValue()));
}
weldBootstrapServiceBuilder.setInstance(weldBootstrapService);
weldBootstrapServiceBuilder.install();

final List<SetupAction> setupActions = new ArrayList<SetupAction>();
Expand Down

0 comments on commit f1e7237

Please sign in to comment.