/**
   * Parse the web.xml and publish the corresponding web app. The received list is expected to
   * contain one URL of an web.xml (only first is used. The web.xml will be parsed and resulting web
   * application structure will be registered with the http service.
   *
   * @throws NullArgumentException if bundle or list of web xmls is null
   * @throws PreConditionException if the list of web xmls is empty or more then one xml
   * @see BundleObserver#addingEntries(Bundle,List)
   */
  public void addingEntries(final Bundle bundle, final List<URL> entries) {
    NullArgumentException.validateNotNull(bundle, "Bundle");
    NullArgumentException.validateNotNull(entries, "List of *.xml's");

    // Context name is also needed for some of the pre-condition checks, therefore it is retrieved
    // here.
    String contextName = extractContextName(bundle);
    LOG.info(String.format("Using [%s] as web application context name", contextName));

    List<String> virtualHostList = extractVirtualHostList(bundle);
    LOG.info(String.format("[%d] virtual hosts defined in bundle header", virtualHostList.size()));

    List<String> connectorList = extractConnectorList(bundle);
    LOG.info(String.format("[%d] connectors defined in bundle header", connectorList.size()));

    // try-catch only to inform framework and listeners of an event.
    try {
      //	        PreConditionException.validateLesserThan( entries.size(), 3, "Number of xml's" );
      PreConditionException.validateEqualTo(
          "WEB-INF".compareToIgnoreCase(Path.getDirectParent(entries.get(0))),
          0,
          "Direct parent of web.xml");
    } catch (PreConditionException pce) {
      LOG.error(pce.getMessage(), pce);
      eventDispatcher.webEvent(
          new WebEvent(WebEvent.FAILED, "/" + contextName, bundle, bundleContext.getBundle(), pce));
      throw pce;
    }

    if (webApps.containsKey(bundle.getBundleId())) {
      LOG.debug(
          String.format("Already found a web application in bundle %d", bundle.getBundleId()));
      return;
    }

    URL webXmlURL = null; // = entries.get( 0 );
    URL jettyWebXmlURL = null;

    for (URL url : entries) {
      if (isJettyWebXml(url)) {
        // it's the jetty-web.xml
        jettyWebXmlURL = url;
      } else if (isWebXml(url)) {
        // it's the web.xml
        webXmlURL = url;
      } else {
        // just another one
      }
    }
    if (webXmlURL == null) {
      PreConditionException pce =
          new PreConditionException("no web.xml configured in web-application");
      LOG.error(pce.getMessage(), pce);
      eventDispatcher.webEvent(
          new WebEvent(WebEvent.FAILED, "/" + contextName, bundle, bundleContext.getBundle(), pce));
      throw pce;
    }

    LOG.debug("Parsing a web application from [" + webXmlURL + "]");

    String rootPath = extractRootPath(bundle);
    LOG.info(String.format("Using [%s] as web application root path", rootPath));

    InputStream is = null;
    try {
      is = webXmlURL.openStream();
      final WebApp webApp = m_parser.parse(bundle, is);
      if (webApp != null) {
        LOG.debug("Parsed web app [" + webApp + "]");

        webApp.setWebXmlURL(webXmlURL);
        webApp.setJettyWebXmlURL(jettyWebXmlURL);
        webApp.setVirtualHostList(virtualHostList);
        webApp.setConnectorList(connectorList);
        webApp.setBundle(bundle);
        webApp.setContextName(contextName);
        webApp.setRootPath(rootPath);
        webApp.setDeploymentState(WebApp.UNDEPLOYED_STATE);

        webApps.put(bundle.getBundleId(), webApp);

        // The Webapp-Deploy header controls if the app is deployed on
        // startup.
        if ("true".equals(opt(getHeader(bundle, "Webapp-Deploy"), "true"))) {
          deploy(webApp);
        } else {
          eventDispatcher.webEvent(
              new WebEvent(
                  WebEvent.UNDEPLOYED,
                  "/" + webApp.getContextName(),
                  webApp.getBundle(),
                  bundleContext.getBundle()));
        }
      }
    } catch (Exception ignore) {
      LOG.error("Could not parse web.xml", ignore);
      eventDispatcher.webEvent(
          new WebEvent(
              WebEvent.FAILED, "/" + contextName, bundle, bundleContext.getBundle(), ignore));
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ignore) {
          // just ignore
        }
      }
    }
  }