Skip to content

Commit

Permalink
[WFLY-11381] Refactoring WeldClassIntrospector to use non deprecated …
Browse files Browse the repository at this point in the history
…ServiceBuilder methods.
  • Loading branch information
ropalka committed Nov 20, 2018
1 parent f987774 commit d0734dc
Showing 1 changed file with 22 additions and 22 deletions.
Expand Up @@ -5,6 +5,8 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
Expand All @@ -20,33 +22,39 @@
import org.jboss.as.weld.injection.InjectionTargets;
import org.jboss.as.weld.services.BeanManagerService;
import org.jboss.as.weld.util.Utils;
import org.jboss.msc.service.Service;
import org.jboss.msc.Service;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceTarget;
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.jboss.weld.bean.builtin.BeanManagerProxy;
import org.jboss.weld.manager.BeanManagerImpl;

/**
* @author Stuart Douglas
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class WeldClassIntrospector implements EEClassIntrospector, Service<EEClassIntrospector> {
public class WeldClassIntrospector implements EEClassIntrospector, Service {

private static final ServiceName SERVICE_NAME = ServiceName.of("weld", "weldClassIntrospector");

private final InjectedValue<BeanManager> beanManager = new InjectedValue<>();

private final Consumer<EEClassIntrospector> eeClassIntrospectorConsumer;
private final Supplier<BeanManager> beanManagerSupplier;
private final ConcurrentMap<Class<?>, InjectionTarget<?>> injectionTargets = new ConcurrentHashMap<>();

private WeldClassIntrospector(final Consumer<EEClassIntrospector> eeClassIntrospectorConsumer, final Supplier<BeanManager> beanManagerSupplier) {
this.eeClassIntrospectorConsumer = eeClassIntrospectorConsumer;
this.beanManagerSupplier = beanManagerSupplier;
}

public static void install(final DeploymentUnit deploymentUnit, final ServiceTarget serviceTarget) {
final WeldClassIntrospector introspector = new WeldClassIntrospector();
final ServiceBuilder sb = serviceTarget.addService(serviceName(deploymentUnit), introspector);
sb.addDependency(BeanManagerService.serviceName(deploymentUnit), BeanManager.class, introspector.beanManager);
final ServiceName sn = serviceName(deploymentUnit);
final ServiceBuilder<?> sb = serviceTarget.addService(sn);
final Consumer<EEClassIntrospector> eeClassIntrospectorConsumer = sb.provides(sn);
final Supplier<BeanManager> beanManagerSupplier = sb.requires(BeanManagerService.serviceName(deploymentUnit));
sb.requires(Utils.getRootDeploymentUnit(deploymentUnit).getServiceName().append(WeldStartService.SERVICE_NAME));
sb.setInstance(new WeldClassIntrospector(eeClassIntrospectorConsumer, beanManagerSupplier));
sb.install();
}

Expand All @@ -57,7 +65,7 @@ public static ServiceName serviceName(DeploymentUnit deploymentUnit) {
@Override
public ManagedReferenceFactory createFactory(Class<?> clazz) {

final BeanManager beanManager = this.beanManager.getValue();
final BeanManager beanManager = this.beanManagerSupplier.get();
final InjectionTarget injectionTarget = getInjectionTarget(clazz);
return new ManagedReferenceFactory() {
@Override
Expand All @@ -76,7 +84,7 @@ private InjectionTarget getInjectionTarget(Class<?> clazz) {
if (target != null) {
return target;
}
final BeanManagerImpl beanManager = BeanManagerProxy.unwrap(this.beanManager.getValue());
final BeanManagerImpl beanManager = BeanManagerProxy.unwrap(beanManagerSupplier.get());
Bean<?> bean = null;
Set<Bean<?>> beans = new HashSet<>(beanManager.getBeans(clazz, Any.Literal.INSTANCE));
Iterator<Bean<?>> it = beans.iterator();
Expand All @@ -102,32 +110,24 @@ private InjectionTarget getInjectionTarget(Class<?> clazz) {

@Override
public ManagedReference createInstance(final Object instance) {
final BeanManager beanManager = this.beanManager.getValue();
final BeanManager beanManager = beanManagerSupplier.get();
final InjectionTarget injectionTarget = getInjectionTarget(instance.getClass());
final CreationalContext context = beanManager.createCreationalContext(null);
injectionTarget.inject(instance, context);
injectionTarget.postConstruct(instance);
return new WeldManagedReference(injectionTarget, context, instance);
}

public InjectedValue<BeanManager> getBeanManager() {
return beanManager;
}

@Override
public void start(StartContext startContext) throws StartException {
public void start(final StartContext startContext) throws StartException {
eeClassIntrospectorConsumer.accept(this);
}

@Override
public void stop(StopContext stopContext) {
injectionTargets.clear();
}

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

private static class WeldManagedReference implements ManagedReference {

private final InjectionTarget injectionTarget;
Expand Down

0 comments on commit d0734dc

Please sign in to comment.