use of org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices in project core by weld.
the class WeldServletLifecycle method initialize.
/**
* @param context
* @return <code>true</code> if initialized properly, <code>false</code> otherwise
*/
boolean initialize(ServletContext context) {
isDevModeEnabled = Boolean.valueOf(context.getInitParameter(CONTEXT_PARAM_DEV_MODE));
WeldManager manager = (WeldManager) context.getAttribute(BEAN_MANAGER_ATTRIBUTE_NAME);
if (manager != null) {
isBootstrapNeeded = false;
String contextId = BeanManagerProxy.unwrap(manager).getContextId();
context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, contextId);
} else {
Object container = context.getAttribute(Listener.CONTAINER_ATTRIBUTE_NAME);
if (container instanceof ContainerInstanceFactory) {
ContainerInstanceFactory factory = (ContainerInstanceFactory) container;
// start the container
ContainerInstance containerInstance = factory.initialize();
container = containerInstance;
// we are in charge of shutdown also
this.shutdownAction = () -> containerInstance.shutdown();
}
if (container instanceof ContainerInstance) {
// the container instance was either passed to us directly or was created in the block above
ContainerInstance containerInstance = (ContainerInstance) container;
manager = BeanManagerProxy.unwrap(containerInstance.getBeanManager());
context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, containerInstance.getId());
isBootstrapNeeded = false;
}
}
final CDI11Bootstrap bootstrap = new WeldBootstrap();
if (isBootstrapNeeded) {
final CDI11Deployment deployment = createDeployment(context, bootstrap);
deployment.getServices().add(ExternalConfiguration.class, new ExternalConfigurationBuilder().add(BEAN_IDENTIFIER_INDEX_OPTIMIZATION.get(), Boolean.FALSE.toString()).build());
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(org.jboss.weld.Container.CONTEXT_ID_KEY);
if (id != null) {
bootstrap.startContainer(id, Environments.SERVLET, deployment);
} else {
bootstrap.startContainer(Environments.SERVLET, deployment);
}
bootstrap.startInitialization();
/*
* Determine the BeanManager used for example for EL resolution - 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.
*
* First try to find the bean archive for WEB-INF/classes. If not found, take the first one available.
*/
for (BeanDeploymentArchive bda : deployment.getBeanDeploymentArchives()) {
if (bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES_FILE_PATH) || bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES)) {
manager = bootstrap.getManager(bda);
break;
}
}
if (manager == null) {
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 (Reflections.isClassLoadable(WeldClassLoaderResourceLoader.INSTANCE, JSP_FACTORY_CLASS_NAME) && 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(new WeldELContextListener());
} catch (Exception 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();
if (isDevModeEnabled) {
FilterRegistration.Dynamic filterDynamic = context.addFilter("Weld Probe Filter", DevelopmentMode.PROBE_FILTER_CLASS_NAME);
filterDynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
}
this.shutdownAction = () -> bootstrap.shutdown();
}
return true;
}
Aggregations