if (module == null) {
throw new DeploymentUnitProcessingException("failed to resolve module for deployment " + deploymentRoot);
}
final ClassLoader classLoader = module.getClassLoader();
final JBossWebMetaData metaData = warMetaData.getMergedJBossWebMetaData();
final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
// Create the context
final StandardContext webContext = new StandardContext();
final ContextConfig config = new JBossContextConfig(deploymentUnit);
List<ValveMetaData> valves = metaData.getValves();
if(valves == null) {
metaData.setValves(valves = new ArrayList<ValveMetaData>());
}
final ValveMetaData valve= new ValveMetaData();
valve.setModule("org.jboss.as.web");
valve.setValveClass(NamingValve.class.getName());
valve.setId(NamingValve.class.getName());
valves.add(valve);
//webContext.addInstanceListener(NamingValve.class.getName());
// Set the deployment root
try {
webContext.setDocBase(deploymentRoot.getPhysicalFile().getAbsolutePath());
} catch (IOException e) {
throw new DeploymentUnitProcessingException(e);
}
webContext.addLifecycleListener(config);
// Set the path name
final String deploymentName = deploymentUnit.getName();
String pathName = null;
if (metaData.getContextRoot() == null) {
pathName = "/" + deploymentUnit.getName().substring(0, deploymentUnit.getName().length() - 4);
} else {
pathName = metaData.getContextRoot();
if ("/".equals(pathName)) {
pathName = "";
} else if (pathName.length() > 0 && pathName.charAt(0) != '/') {
pathName = "/" + pathName;
}
}
webContext.setPath(pathName);
webContext.setIgnoreAnnotations(true);
webContext.setCrossContext(!metaData.isDisableCrossContext());
final WebInjectionContainer injectionContainer = new WebInjectionContainer(module.getClassLoader());
final Map<String, ComponentInstantiator> components = deploymentUnit
.getAttachment(WebAttachments.WEB_COMPONENT_INSTANTIATORS);
if (components != null) {
for (Map.Entry<String, ComponentInstantiator> entry : components.entrySet()) {
injectionContainer.addInstantiator(entry.getKey(), entry.getValue());
}
}
webContext.setInstanceManager(injectionContainer);
final Loader loader = new WebCtxLoader(classLoader);
webContext.setLoader(loader);
// Set the session cookies flag according to metadata
switch (metaData.getSessionCookies()) {
case JBossWebMetaData.SESSION_COOKIES_ENABLED:
webContext.setCookies(true);
break;
case JBossWebMetaData.SESSION_COOKIES_DISABLED:
webContext.setCookies(false);
break;
}
String metaDataSecurityDomain = metaData.getSecurityDomain();
if (metaDataSecurityDomain != null) {
metaDataSecurityDomain = metaDataSecurityDomain.trim();
}
String securityDomain = metaDataSecurityDomain == null ? SecurityConstants.DEFAULT_APPLICATION_POLICY : SecurityUtil
.unprefixSecurityDomain(metaDataSecurityDomain);
Map<String, Set<String>> principalVersusRolesMap = metaData.getSecurityRoles().getPrincipalVersusRolesMap();
// Setup an deployer configured ServletContext attributes
final List<ServletContextAttribute> attributes = deploymentUnit.getAttachment(ServletContextAttribute.ATTACHMENT_KEY);
if (attributes != null) {
final ServletContext context = webContext.getServletContext();
for (ServletContextAttribute attribute : attributes) {
context.setAttribute(attribute.getName(), attribute.getValue());
}
}
try {
JBossWebRealmService realmService = new JBossWebRealmService(principalVersusRolesMap);
ServiceBuilder<?> builder = serviceTarget.addService(WebSubsystemServices.JBOSS_WEB_REALM.append(deploymentName),
realmService);
builder.addDependency(DependencyType.REQUIRED, SecurityDomainService.SERVICE_NAME.append(securityDomain),
SecurityDomainContext.class, realmService.getSecurityDomainContextInjector()).setInitialMode(Mode.ACTIVE)
.install();
WebDeploymentService webDeploymentService = new WebDeploymentService(webContext, injectionContainer);
if(moduleDescription != null ) {
webDeploymentService.getNamespaceSelector().setValue(new ImmediateValue<NamespaceContextSelector>(moduleDescription.getNamespaceContextSelector()));
}
builder = serviceTarget.addService(WebSubsystemServices.JBOSS_WEB.append(deploymentName), webDeploymentService);
builder.addDependency(WebSubsystemServices.JBOSS_WEB_HOST.append(hostName), VirtualHost.class,
new WebContextInjector(webContext)).addDependencies(injectionContainer.getServiceNames());