Skip to content

Commit

Permalink
WELD-2256 Allow CDI.getBeanManager() after BeforeBeanDiscovery is fired
Browse files Browse the repository at this point in the history
- generate unique container id if not set
- also init BeanManager lazily
  • Loading branch information
mkouba committed Nov 11, 2016
1 parent 2b0435d commit 8f88f99
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 125 deletions.
Expand Up @@ -40,6 +40,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

Expand Down Expand Up @@ -144,10 +145,10 @@
*
* <pre>
* Weld builder = new Weld()
* .disableDiscovery()
* .packages(Main.class, Utils.class)
* .interceptors(TransactionalInterceptor.class)
* .property("org.jboss.weld.construction.relaxed", true);
* .disableDiscovery()
* .packages(Main.class, Utils.class)
* .interceptors(TransactionalInterceptor.class)
* .property("org.jboss.weld.construction.relaxed", true);
* WeldContainer container = builder.initialize();
* </pre>
*
Expand Down Expand Up @@ -230,7 +231,7 @@ public class Weld implements ContainerInstanceFactory {
private ResourceLoader resourceLoader;

public Weld() {
this(RegistrySingletonProvider.STATIC_INSTANCE);
this(null);
}

/**
Expand Down Expand Up @@ -370,8 +371,8 @@ public Weld addExtension(Extension extension) {
/**
* Enable interceptors for the synthetic bean archive, all previous values are removed.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param interceptorClasses
* @return self
Expand All @@ -387,8 +388,8 @@ public Weld interceptors(Class<?>... interceptorClasses) {
/**
* Add an interceptor class to the list of enabled interceptors for the synthetic bean archive.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param interceptorClass
* @return self
Expand All @@ -397,11 +398,12 @@ public Weld addInterceptor(Class<?> interceptorClass) {
enabledInterceptors.add(syntheticMetadata(interceptorClass));
return this;
}

/**
* Enable decorators for the synthetic bean archive, all previous values are removed.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param decoratorClasses
* @return self
Expand All @@ -417,8 +419,8 @@ public Weld decorators(Class<?>... decoratorClasses) {
/**
* Add a decorator class to the list of enabled decorators for the synthetic bean archive.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param decoratorClass
* @return self
Expand All @@ -431,8 +433,8 @@ public Weld addDecorator(Class<?> decoratorClass) {
/**
* Select alternatives for the synthetic bean archive, all previous values are removed.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param alternativeClasses
* @return self
Expand All @@ -448,8 +450,8 @@ public Weld alternatives(Class<?>... alternativeClasses) {
/**
* Add an alternative class to the list of selected alternatives for a synthetic bean archive.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param alternativeClass
* @return self
Expand All @@ -462,8 +464,8 @@ public Weld addAlternative(Class<?> alternativeClass) {
/**
* Select alternative stereotypes for the synthetic bean archive, all previous values are removed.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param alternativeStereotypeClasses
* @return self
Expand All @@ -480,8 +482,8 @@ public final Weld alternativeStereotypes(Class<? extends Annotation>... alternat
/**
* Add an alternative stereotype class to the list of selected alternative stereotypes for a synthetic bean archive.
* <p>
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the
* absence of the <code>beans.xml</code> descriptor.
* This method does not add any class to the set of bean classes for the synthetic bean archive. It's purpose is solely to compensate the absence of the
* <code>beans.xml</code> descriptor.
*
* @param alternativeStereotypeClass
* @return self
Expand Down Expand Up @@ -533,7 +535,7 @@ public Weld resetAll() {
reset();
properties.clear();
enableDiscovery();
containerId(RegistrySingletonProvider.STATIC_INSTANCE);
containerId(null);
return this;
}

Expand Down Expand Up @@ -567,7 +569,7 @@ public boolean isDiscoveryEnabled() {
}

/**
* Bootstraps a new Weld SE container with the current {@link #containerId}.
* Bootstraps a new Weld SE container with the current container id (generated value if not set through {@link #containerId(String)}).
* <p/>
* The container must be shut down properly when an application is stopped. Applications are encouraged to use the try-with-resources statement or invoke
* {@link WeldContainer#shutdown()} explicitly.
Expand Down Expand Up @@ -602,38 +604,24 @@ public WeldContainer initialize() {
}
deployment.getServices().add(ExternalConfiguration.class, configurationBuilder.build());

final String containerId = this.containerId != null ? this.containerId : UUID.randomUUID().toString();
bootstrap.startContainer(containerId, Environments.SE, deployment);
bootstrap.startInitialization();
bootstrap.deployBeans();
bootstrap.validateBeans();
bootstrap.endInitialization();

final WeldContainer weldContainer = WeldContainer.initialize(containerId, bootstrap.getManager(getDeterminingBeanDeploymentArchive(deployment)),
bootstrap, isEnabled(SHUTDOWN_HOOK_SYSTEM_PROPERTY, true));
final WeldContainer weldContainer = WeldContainer.startInitialization(containerId, deployment, bootstrap);

initializedContainers.put(containerId, weldContainer);
return weldContainer;
}

private BeanDeploymentArchive getDeterminingBeanDeploymentArchive(Deployment deployment) {
Collection<BeanDeploymentArchive> beanDeploymentArchives = deployment.getBeanDeploymentArchives();
if (beanDeploymentArchives.size() == 1) {
// Only one bean archive or isolation is disabled
return beanDeploymentArchives.iterator().next();
try {
bootstrap.startInitialization();
bootstrap.deployBeans();
bootstrap.validateBeans();
bootstrap.endInitialization();
WeldContainer.endInitialization(weldContainer, isEnabled(SHUTDOWN_HOOK_SYSTEM_PROPERTY, true));
initializedContainers.put(containerId, weldContainer);
} catch (Throwable e) {
// Discard the container if a bootstrap problem occurs, e.g. validation error
WeldContainer.discard(weldContainer.getId());
throw e;
}
for (BeanDeploymentArchive beanDeploymentArchive : beanDeploymentArchives) {
if (WeldDeployment.SYNTHETIC_BDA_ID.equals(beanDeploymentArchive.getId())) {
// Synthetic bean archive takes precedence
return beanDeploymentArchive;
}
}
for (BeanDeploymentArchive beanDeploymentArchive : beanDeploymentArchives) {
if (!WeldDeployment.ADDITIONAL_BDA_ID.equals(beanDeploymentArchive.getId())) {
// Get the first non-additional bean deployment archive
return beanDeploymentArchive;
}
}
return deployment.loadBeanDeploymentArchive(WeldContainer.class);
return weldContainer;
}

/**
Expand Down

0 comments on commit 8f88f99

Please sign in to comment.