final Map<String, Object> bindings = new HashMap<String, Object>();
bindings.putAll(appContext.getBindings());
bindings.putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader).buildBindings(JndiEncBuilder.JndiScope.comp));
final WebContext webContext = new WebContext(appContext);
webContext.setBindings(bindings);
webContext.getBindings().putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader).buildBindings(JndiEncBuilder.JndiScope.comp));
webContext.setJndiEnc(WebInitialContext.create(bindings, appContext.getGlobalJndiContext()));
webContext.setClassLoader(classLoader);
webContext.setId(webAppInfo.moduleId);
webContext.setContextRoot(webAppInfo.contextRoot);
webContext.getInjections().addAll(injections);
webContext.setInitialContext(new EmbeddedInitialContext(webContext.getJndiEnc(), webContext.getBindings()));
appContext.getWebContexts().add(webContext);
cs.addWebContext(webContext);
if (!appInfo.webAppAlone) {
final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
final List<BeanContext> beanContexts = assembler.initEjbs(classLoader, appInfo, appContext, injections, new ArrayList<BeanContext>(), webAppInfo.moduleId);
appContext.getBeanContexts().addAll(beanContexts);
new CdiBuilder().build(appInfo, appContext, appContext.getBeanContexts(), webContext);
assembler.startEjbs(true, beanContexts);
}
final ServletContextEvent sce = new MockServletContextEvent();
servletContextEvents.put(webAppInfo, sce);
// listeners
for (ListenerInfo listener : webAppInfo.listeners) {
final Class<?> clazz = webContext.getClassLoader().loadClass(listener.classname);
final Object instance = webContext.newInstance(clazz);
if (ServletContextListener.class.isInstance(instance)) {
((ServletContextListener) instance).contextInitialized(sce);
}
List<Object> list = listeners.get(webAppInfo);
if (list == null) {
list = new ArrayList<Object>();
listeners.put(webAppInfo, list);
}
list.add(instance);
}
final DeployedWebObjects deployedWebObjects = new DeployedWebObjects();
deployedWebObjects.webContext = webContext;
servletDeploymentInfo.put(webAppInfo, deployedWebObjects);
if (addServletMethod == null) { // can't manage filter/servlets
continue;
}
// register filters
for (FilterInfo info : webAppInfo.filters) {
for (String mapping : info.mappings) {
final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, info.initParams);
try {
addFilterMethod.invoke(null, info.classname, webContext, mapping, config);
deployedWebObjects.filterMappings.add(mapping);
} catch (Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
for (ClassListInfo info : webAppInfo.webAnnotatedClasses) {
final String url = info.name;
for (String filterPath : info.list) {
String classname = nameFromUrls(url, filterPath);
final Class<?> clazz = webContext.getClassLoader().loadClass(classname);
final WebFilter annotation = clazz.getAnnotation(WebFilter.class);
if (annotation != null) {
final Properties initParams = new Properties();
for (WebInitParam param : annotation.initParams()) {
initParams.put(param.name(), param.value());
}
final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, initParams);
for (String mapping: annotation.urlPatterns()) {
try {
addFilterMethod.invoke(null, classname, webContext, mapping, config);
deployedWebObjects.filterMappings.add(mapping);
} catch (Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
}
}
// register servlets
for (ServletInfo info : webAppInfo.servlets) {
if ("true".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
// skip jaxrs servlets
boolean skip = false;
for (ParamValueInfo pvi : info.initParams) {
if ("javax.ws.rs.Application".equals(pvi.name) || Application.class.getName().equals(pvi.name)) {
skip = true;
}
}
if (skip) {
continue;
}
if (info.servletClass == null) {
try {
if (Application.class.isAssignableFrom(classLoader.loadClass(info.servletName))) {
continue;
}
} catch (Exception e) {
// no-op
}
}
} // else let the user manage itself a rest servlet etc...
// deploy
for (String mapping : info.mappings) {
try {
addServletMethod.invoke(null, info.servletClass, webContext, mapping);
deployedWebObjects.mappings.add(mapping);
} catch (Exception e) {
LOGGER.warning(e.getMessage(), e);
}
}
}
for (ClassListInfo info : webAppInfo.webAnnotatedClasses) {
final String url = info.name;
for (String servletPath : info.list) {
String classname = nameFromUrls(url, servletPath);
final Class<?> clazz = webContext.getClassLoader().loadClass(classname);
final WebServlet annotation = clazz.getAnnotation(WebServlet.class);
if (annotation != null) {
for (String mapping: annotation.urlPatterns()) {
try {
addServletMethod.invoke(null, classname, webContext, mapping);