protected void processDeployment(final String hostName, final WarMetaData warMetaData, final DeploymentUnit deploymentUnit,
final ServiceTarget serviceTarget) throws DeploymentUnitProcessingException {
final VirtualFile deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
if (module == null) {
throw new DeploymentUnitProcessingException("failed to resolve module for deployment " + deploymentRoot);
}
final ClassLoader classLoader = module.getClassLoader();
final JBossWebMetaData metaData = warMetaData.getMergedJBossWebMetaData();
final List<SetupAction> setupActions = deploymentUnit
.getAttachmentList(org.jboss.as.ee.component.Attachments.EE_SETUP_ACTIONS);
// Create the context
final StandardContext webContext = new StandardContext();
final ContextConfig config = new JBossContextConfig(deploymentUnit);
// add SecurityAssociationValve right at the beginning
webContext.addValve(new SecurityContextAssociationValve(deploymentUnit));
List<ValveMetaData> valves = metaData.getValves();
if (valves == null) {
metaData.setValves(valves = new ArrayList<ValveMetaData>());
}
// Set the deployment root
try {
webContext.setDocBase(deploymentRoot.getPhysicalFile().getAbsolutePath());
} catch (IOException e) {
throw new DeploymentUnitProcessingException(e);
}
webContext.addLifecycleListener(config);
String pathName;
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());
//see AS7-2077
//basically we want to ignore components that have failed for whatever reason
//if they are important they will be picked up when the web deployment actually starts
final Map<String, ComponentInstantiator> components = deploymentUnit.getAttachment(WebAttachments.WEB_COMPONENT_INSTANTIATORS);
if (components != null) {
final Set<ServiceName> failed = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.FAILED_COMPONENTS);
for (Map.Entry<String, ComponentInstantiator> entry : components.entrySet()) {
boolean skip = false;
for (final ServiceName serviceName : entry.getValue().getServiceNames()) {
if (failed.contains(serviceName)) {
skip = true;
break;
}
}
if (!skip) {
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);
// 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 {
final ServiceName deploymentServiceName = WebSubsystemServices.deploymentServiceName(hostName, pathName);
final ServiceName realmServiceName = deploymentServiceName.append("realm");
final JBossWebRealmService realmService = new JBossWebRealmService(deploymentUnit);
ServiceBuilder<?> builder = serviceTarget.addService(realmServiceName, realmService);
builder.addDependency(DependencyType.REQUIRED, SecurityDomainService.SERVICE_NAME.append(securityDomain),
SecurityDomainContext.class, realmService.getSecurityDomainContextInjector()).setInitialMode(Mode.ACTIVE)
.install();
final WebDeploymentService webDeploymentService = new WebDeploymentService(webContext, injectionContainer, setupActions);
builder = serviceTarget
.addService(deploymentServiceName, webDeploymentService)
.addDependency(WebSubsystemServices.JBOSS_WEB_HOST.append(hostName), VirtualHost.class,
new WebContextInjector(webContext)).addDependencies(injectionContainer.getServiceNames())
.addDependency(realmServiceName, Realm.class, webDeploymentService.getRealm())
.addDependencies(deploymentUnit.getAttachmentList(Attachments.WEB_DEPENDENCIES))
.addDependency(JndiNamingDependencyProcessor.serviceName(deploymentUnit));
//add any dependencies required by the setup action
for(final SetupAction action : setupActions) {
builder.addDependencies(action.dependencies());
}
if (metaData.getDistributable() != null) {
final DistributedCacheManagerFactory factory = DistributableSessionManager.getDistributedCacheManagerFactory();
if (factory != null) {
builder.addDependencies(DependencyType.OPTIONAL, factory.getDependencies(metaData));
}
}
builder.install();
// adding JACC service
AbstractSecurityDeployer<?> deployer = new WarSecurityDeployer();
JaccService<?> service = deployer.deploy(deploymentUnit);
if (service != null) {
((WarJaccService) service).setContext(webContext);
final ServiceName jaccServiceName = JaccService.SERVICE_NAME.append(deploymentUnit.getName());
builder = serviceTarget.addService(jaccServiceName, service);
if (deploymentUnit.getParent() != null) {
// add dependency to parent policy
final DeploymentUnit parentDU = deploymentUnit.getParent();
builder.addDependency(JaccService.SERVICE_NAME.append(parentDU.getName()), PolicyConfiguration.class,
service.getParentPolicyInjector());
}
// add dependency to web deployment service
builder.addDependency(deploymentServiceName);
builder.setInitialMode(Mode.ACTIVE).install();
}
} catch (ServiceRegistryException e) {
throw new DeploymentUnitProcessingException("Failed to add JBoss web deployment service", e);
}
// Process the web related mgmt information
final ModelNode node = deploymentUnit.getDeploymentSubsystemModel("web");
node.get("context-root").set("".equals(pathName) ? "/" : pathName);