* @param context
* @return <code>true</code> if initialized properly, <code>false</code> otherwise
*/
boolean initialize(ServletContext context) {
WeldManager manager = (WeldManager) context.getAttribute(BEAN_MANAGER_ATTRIBUTE_NAME);
if (manager != null) {
isBootstrapNeeded = false;
}
if (isBootstrapNeeded) {
CDI11Deployment deployment = createDeployment(context, bootstrap);
if (deployment.getBeanDeploymentArchives().isEmpty()) {
// Skip initialization - there is no bean archive in the deployment
CommonLogger.LOG.initSkippedNoBeanArchiveFound();
return false;
}
ResourceInjectionServices resourceInjectionServices = new ServletResourceInjectionServices() {
};
try {
for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
archive.getServices().add(ResourceInjectionServices.class, resourceInjectionServices);
}
} catch (NoClassDefFoundError e) {
// Support GAE
WeldServletLogger.LOG.resourceInjectionNotAvailable();
}
String id = context.getInitParameter(CONTEXT_ID_KEY);
if (id != null) {
bootstrap.startContainer(id, Environments.SERVLET, deployment);
} else {
bootstrap.startContainer(Environments.SERVLET, deployment);
}
bootstrap.startInitialization();
/*
* This should work fine as all bean archives share the same classloader. The only difference this can make is per-BDA (CDI 1.0 style) enablement of
* alternatives, interceptors and decorators. Nothing we can do about that.
*/
manager = bootstrap.getManager(deployment.getBeanDeploymentArchives().iterator().next());
// Push the manager into the servlet context so we can access in JSF
context.setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);
}
ContainerContext containerContext = new ContainerContext(context, manager);
StringBuilder dump = new StringBuilder();
Container container = findContainer(containerContext, dump);
if (container == null) {
WeldServletLogger.LOG.noSupportedServletContainerDetected();
WeldServletLogger.LOG.debugv("Exception dump from Container lookup: {0}", dump);
} else {
container.initialize(containerContext);
this.container = container;
}
if (JspFactory.getDefaultFactory() != null) {
JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(context);
// Register the ELResolver with JSP
jspApplicationContext.addELResolver(manager.getELResolver());
// Register ELContextListener with JSP
try {
jspApplicationContext.addELContextListener(Reflections.<ELContextListener> newInstance("org.jboss.weld.el.WeldELContextListener"));
} catch (IllegalArgumentException e) {
throw WeldServletLogger.LOG.errorLoadingWeldELContextListener(e);
}
// Push the wrapped expression factory into the servlet context so that Tomcat or Jetty can hook it in using a container code
context.setAttribute(EXPRESSION_FACTORY_NAME, manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
}
if (isBootstrapNeeded) {
bootstrap.deployBeans().validateBeans().endInitialization();
}