*
@@ -230,7 +231,7 @@ public class Weld implements ContainerInstanceFactory {
private ResourceLoader resourceLoader;
public Weld() {
- this(RegistrySingletonProvider.STATIC_INSTANCE);
+ this(null);
}
/**
@@ -370,8 +371,8 @@ public Weld addExtension(Extension extension) {
/**
* Enable interceptors for the synthetic bean archive, all previous values are removed.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param interceptorClasses
* @return self
@@ -387,8 +388,8 @@ public Weld interceptors(Class>... interceptorClasses) {
/**
* Add an interceptor class to the list of enabled interceptors for the synthetic bean archive.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param interceptorClass
* @return self
@@ -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.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param decoratorClasses
* @return self
@@ -417,8 +419,8 @@ public Weld decorators(Class>... decoratorClasses) {
/**
* Add a decorator class to the list of enabled decorators for the synthetic bean archive.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param decoratorClass
* @return self
@@ -431,8 +433,8 @@ public Weld addDecorator(Class> decoratorClass) {
/**
* Select alternatives for the synthetic bean archive, all previous values are removed.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param alternativeClasses
* @return self
@@ -448,8 +450,8 @@ public Weld alternatives(Class>... alternativeClasses) {
/**
* Add an alternative class to the list of selected alternatives for a synthetic bean archive.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param alternativeClass
* @return self
@@ -462,8 +464,8 @@ public Weld addAlternative(Class> alternativeClass) {
/**
* Select alternative stereotypes for the synthetic bean archive, all previous values are removed.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param alternativeStereotypeClasses
* @return self
@@ -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.
*
- * 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 beans.xml 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
+ * beans.xml descriptor.
*
* @param alternativeStereotypeClass
* @return self
@@ -533,7 +535,7 @@ public Weld resetAll() {
reset();
properties.clear();
enableDiscovery();
- containerId(RegistrySingletonProvider.STATIC_INSTANCE);
+ containerId(null);
return this;
}
@@ -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)}).
*
* 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.
@@ -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 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;
}
/**
diff --git a/environments/se/core/src/main/java/org/jboss/weld/environment/se/WeldContainer.java b/environments/se/core/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
index 512952f9d3a..a96811e06bb 100644
--- a/environments/se/core/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
+++ b/environments/se/core/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
@@ -16,6 +16,7 @@
*/
package org.jboss.weld.environment.se;
+import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -26,10 +27,14 @@
import javax.enterprise.inject.spi.BeanManager;
import org.jboss.weld.AbstractCDI;
+import org.jboss.weld.bean.builtin.BeanManagerProxy;
import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.bootstrap.api.Singleton;
import org.jboss.weld.bootstrap.api.SingletonProvider;
+import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
+import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.environment.ContainerInstance;
+import org.jboss.weld.environment.deployment.WeldDeployment;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.se.events.ContainerShutdown;
import org.jboss.weld.environment.se.logging.WeldSELogger;
@@ -37,7 +42,6 @@
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.literal.InitializedLiteral;
import org.jboss.weld.manager.BeanManagerImpl;
-import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.util.collections.ImmutableList;
/**
@@ -125,21 +129,31 @@ public static List getRunningContainerIds() {
}
/**
+ * Start the initialization.
*
* @param id
* @param manager
* @param bootstrap
* @return the initialized Weld container
*/
- static WeldContainer initialize(String id, WeldManager manager, Bootstrap bootstrap, boolean isShutdownHookEnabled) {
+ static WeldContainer startInitialization(String id, Deployment deployment, Bootstrap bootstrap) {
if (SINGLETON.isSet(id)) {
throw WeldSELogger.LOG.weldContainerAlreadyRunning(id);
}
- WeldContainer weldContainer = new WeldContainer(id, manager, bootstrap);
+ WeldContainer weldContainer = new WeldContainer(id, deployment, bootstrap);
SINGLETON.set(id, weldContainer);
RUNNING_CONTAINER_IDS.add(id);
- WeldSELogger.LOG.weldContainerInitialized(id);
- manager.fireEvent(new ContainerInitialized(id), InitializedLiteral.APPLICATION);
+ return weldContainer;
+ }
+
+ /**
+ * Finish the initialization.
+ *
+ * @param container
+ * @param isShutdownHookEnabled
+ */
+ static void endInitialization(WeldContainer container, boolean isShutdownHookEnabled) {
+ container.complete();
// If needed, register one shutdown hook for all containers
if (shutdownHook == null && isShutdownHookEnabled) {
synchronized (LOCK) {
@@ -149,37 +163,51 @@ static WeldContainer initialize(String id, WeldManager manager, Bootstrap bootst
}
}
}
- return weldContainer;
+ }
+
+ /**
+ *
+ * @param containerId
+ */
+ static void discard(String containerId) {
+ SINGLETON.clear(containerId);
+ RUNNING_CONTAINER_IDS.remove(containerId);
}
// This replicates org.jboss.weld.Container.contextId
private final String id;
- private final WeldManager manager;
+ private final Deployment deployment;
private final Bootstrap bootstrap;
- private final WeldInstance