private void processDeployment(
      final WarMetaData warMetaData,
      final DeploymentUnit deploymentUnit,
      final ServiceTarget serviceTarget,
      String hostName)
      throws DeploymentUnitProcessingException {
    final VirtualFile deploymentRoot =
        deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
    final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
    if (module == null) {
      throw new DeploymentUnitProcessingException(MESSAGES.failedToResolveModule(deploymentUnit));
    }
    final DeploymentClassIndex deploymentClassIndex =
        deploymentUnit.getAttachment(Attachments.CLASS_INDEX);
    final JBossWebMetaData metaData = warMetaData.getMergedJBossWebMetaData();
    final List<SetupAction> setupActions =
        deploymentUnit.getAttachmentList(org.jboss.as.ee.component.Attachments.WEB_SETUP_ACTIONS);

    ScisMetaData scisMetaData = deploymentUnit.getAttachment(ScisMetaData.ATTACHMENT_KEY);

    final Set<ServiceName> dependentComponents = new HashSet<>();
    // 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 List<ServiceName> components =
        deploymentUnit.getAttachmentList(WebComponentDescription.WEB_COMPONENTS);
    final Set<ServiceName> failed =
        deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.FAILED_COMPONENTS);
    for (final ServiceName component : components) {
      boolean skip = false;
      if (!failed.contains(component)) {
        dependentComponents.add(component);
      }
    }

    ComponentRegistry componentRegistry =
        deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.COMPONENT_REGISTRY);
    if (componentRegistry == null) {
      // we do this to avoid lots of other null checks
      // this will only happen if the EE subsystem is not installed
      componentRegistry = new ComponentRegistry(null);
    }

    final WebInjectionContainer injectionContainer =
        new WebInjectionContainer(module.getClassLoader(), componentRegistry);

    DeploymentInfo deploymentInfo =
        createServletConfig(
            metaData,
            deploymentUnit,
            module,
            deploymentClassIndex,
            injectionContainer,
            componentRegistry,
            scisMetaData,
            deploymentRoot);

    final String pathName = pathNameOfDeployment(deploymentUnit, metaData);
    deploymentInfo.setContextPath(pathName);

    String metaDataSecurityDomain = metaData.getSecurityDomain();
    if (metaDataSecurityDomain == null) {
      metaDataSecurityDomain = getJBossAppSecurityDomain(deploymentUnit);
    }
    if (metaDataSecurityDomain != null) {
      metaDataSecurityDomain = metaDataSecurityDomain.trim();
    }

    String securityDomain =
        metaDataSecurityDomain == null
            ? SecurityConstants.DEFAULT_APPLICATION_POLICY
            : SecurityUtil.unprefixSecurityDomain(metaDataSecurityDomain);

    final ServiceName deploymentServiceName =
        UndertowService.deploymentServiceName(hostName, deploymentInfo.getContextPath());
    final ServiceName hostServiceName = UndertowService.virtualHostName(defaultServer, hostName);
    final UndertowDeploymentService service =
        new UndertowDeploymentService(
            deploymentInfo, injectionContainer, module, warMetaData.getMergedJBossWebMetaData());
    final ServiceBuilder<UndertowDeploymentService> builder =
        serviceTarget
            .addService(deploymentServiceName, service)
            .addDependencies(dependentComponents)
            .addDependency(
                UndertowService.SERVLET_CONTAINER.append(defaultContainer),
                ServletContainerService.class,
                service.getContainer())
            .addDependency(hostServiceName, Host.class, service.getHost())
            .addDependency(
                SecurityDomainService.SERVICE_NAME.append(securityDomain),
                SecurityDomainContext.class,
                service.getSecurityDomainContextValue())
            .addDependency(
                UndertowService.UNDERTOW, UndertowService.class, service.getUndertowService());

    deploymentUnit.addToAttachmentList(
        Attachments.DEPLOYMENT_COMPLETE_SERVICES, deploymentServiceName);

    // add any dependencies required by the setup action
    for (final SetupAction action : setupActions) {
      builder.addDependencies(action.dependencies());
      deploymentInfo.addThreadSetupAction(
          new ThreadSetupAction() {

            @Override
            public Handle setup(final HttpServerExchange exchange) {
              action.setup(Collections.<String, Object>emptyMap());
              return new Handle() {
                @Override
                public void tearDown() {
                  action.teardown(Collections.<String, Object>emptyMap());
                }
              };
            }
          });
    }

    if (metaData.getDistributable() != null) {
      DistributedCacheManagerFactoryService factoryService =
          new DistributedCacheManagerFactoryService();
      DistributedCacheManagerFactory factory = factoryService.getValue();
      if (factory != null) {
        ServiceName factoryServiceName = deploymentServiceName.append("session");
        builder.addDependency(
            ServiceBuilder.DependencyType.OPTIONAL,
            factoryServiceName,
            DistributedCacheManagerFactory.class,
            service.getDistributedCacheManagerFactoryInjectedValue());

        ServiceBuilder<DistributedCacheManagerFactory> factoryBuilder =
            serviceTarget.addService(factoryServiceName, factoryService);
        boolean enabled =
            factory.addDeploymentDependencies(
                deploymentServiceName,
                deploymentUnit.getServiceRegistry(),
                serviceTarget,
                factoryBuilder,
                metaData);
        factoryBuilder.setInitialMode(enabled ? Mode.ON_DEMAND : Mode.NEVER).install();
      }
    }

    // OSGi web applications are activated in {@link WebContextActivationProcessor} according to
    // bundle lifecycle changes
    if (deploymentUnit.hasAttachment(Attachments.OSGI_MANIFEST)) {
      builder.setInitialMode(Mode.NEVER);
      UndertowDeploymentService.ContextActivatorImpl activator =
          new UndertowDeploymentService.ContextActivatorImpl(builder.install());
      deploymentUnit.putAttachment(ContextActivator.ATTACHMENT_KEY, activator);
    } else {
      builder.setInitialMode(Mode.ACTIVE);
      builder.install();
    }

    // Process the web related mgmt information
    final ModelNode node =
        deploymentUnit.getDeploymentSubsystemModel(UndertowExtension.SUBSYSTEM_NAME);
    node.get(DeploymentDefinition.CONTEXT_ROOT.getName()).set("".equals(pathName) ? "/" : pathName);
    node.get(DeploymentDefinition.VIRTUAL_HOST.getName()).set(hostName);
    processManagement(deploymentUnit, metaData);
  }
Exemplo n.º 2
0
  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(MESSAGES.failedToResolveModule(deploymentRoot));
    }
    final ClassLoader classLoader = module.getClassLoader();
    final JBossWebMetaData metaData = warMetaData.getMergedJBossWebMetaData();
    final List<SetupAction> setupActions =
        deploymentUnit.getAttachmentList(org.jboss.as.ee.component.Attachments.WEB_SETUP_ACTIONS);

    // Create the context
    final StandardContext webContext = new StandardContext();
    final JBossContextConfig config = new JBossContextConfig(deploymentUnit);

    // Add SecurityAssociationValve right at the beginning
    webContext.addValve(new SecurityContextAssociationValve(deploymentUnit));

    // Set the deployment root
    try {
      webContext.setDocBase(deploymentRoot.getPhysicalFile().getAbsolutePath());
    } catch (IOException e) {
      throw new DeploymentUnitProcessingException(e);
    }
    webContext.addLifecycleListener(config);

    final String pathName = pathNameOfDeployment(deploymentUnit, metaData);
    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());
        }
      }
    }

    final Loader loader = new WebCtxLoader(classLoader);
    webContext.setLoader(loader);

    // Valves
    List<ValveMetaData> valves = metaData.getValves();
    if (valves == null) {
      metaData.setValves(valves = new ArrayList<ValveMetaData>());
    }
    for (ValveMetaData valve : valves) {
      Valve valveInstance =
          (Valve) getInstance(module, valve.getModule(), valve.getValveClass(), valve.getParams());
      webContext.getPipeline().addValve(valveInstance);
    }

    // Container listeners
    List<ContainerListenerMetaData> listeners = metaData.getContainerListeners();
    if (listeners != null) {
      for (ContainerListenerMetaData listener : listeners) {
        switch (listener.getListenerType()) {
          case CONTAINER:
            ContainerListener containerListener =
                (ContainerListener)
                    getInstance(
                        module,
                        listener.getModule(),
                        listener.getListenerClass(),
                        listener.getParams());
            webContext.addContainerListener(containerListener);
            break;
          case LIFECYCLE:
            LifecycleListener lifecycleListener =
                (LifecycleListener)
                    getInstance(
                        module,
                        listener.getModule(),
                        listener.getListenerClass(),
                        listener.getParams());
            if (webContext instanceof Lifecycle) {
              ((Lifecycle) webContext).addLifecycleListener(lifecycleListener);
            }
            break;
          case SERVLET_INSTANCE:
            webContext.addInstanceListener(listener.getListenerClass());
            break;
          case SERVLET_CONTAINER:
            webContext.addWrapperListener(listener.getListenerClass());
            break;
          case SERVLET_LIFECYCLE:
            webContext.addWrapperLifecycle(listener.getListenerClass());
            break;
        }
      }
    }

    // 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);

    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, attributes);
      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) {
        DistributedCacheManagerFactoryService factoryService =
            new DistributedCacheManagerFactoryService();
        DistributedCacheManagerFactory factory = factoryService.getValue();
        if (factory != null) {
          ServiceName factoryServiceName = deploymentServiceName.append("session");
          builder.addDependency(
              DependencyType.OPTIONAL,
              factoryServiceName,
              DistributedCacheManagerFactory.class,
              config.getDistributedCacheManagerFactoryInjector());

          ServiceBuilder<DistributedCacheManagerFactory> factoryBuilder =
              serviceTarget.addService(factoryServiceName, factoryService);
          boolean enabled = factory.addDependencies(serviceTarget, factoryBuilder, metaData);
          factoryBuilder
              .setInitialMode(
                  enabled ? ServiceController.Mode.ON_DEMAND : ServiceController.Mode.NEVER)
              .install();
        }
      }

      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(MESSAGES.failedToAddWebDeployment(), e);
    }

    // Process the web related mgmt information
    final ModelNode node = deploymentUnit.getDeploymentSubsystemModel("web");
    node.get("context-root").set("".equals(pathName) ? "/" : pathName);
    node.get("virtual-host").set(hostName);
    processManagement(deploymentUnit, metaData);
  }