Exemplo n.º 1
0
  /** @see WebXmlParser#parse(InputStream) */
  public WebApp parse(final Bundle bundle, final InputStream inputStream) {
    final WebApp webApp = new WebApp(); // changed to final because of inner
    // class.
    try {
      final Element rootElement = getRootElement(inputStream);
      if (rootElement != null) {
        // webApp = new WebApp();
        // web-app attributes
        String version = getAttribute(rootElement, "version");
        Integer majorVersion = null;
        if (version != null && !version.isEmpty() && version.length() > 2) {
          LOG.debug("version found in web.xml - " + version);
          try {
            majorVersion = Integer.parseInt(version.split("\\.")[0]);
          } catch (NumberFormatException nfe) {
            // munch do nothing here stay with null therefore
            // annotation scanning is disabled.
          }
        } else if (version != null && !version.isEmpty() && version.length() > 0) {
          try {
            majorVersion = Integer.parseInt(version);
          } catch (NumberFormatException e) {
            // munch do nothing here stay with null....
          }
        }
        Boolean metaDataComplete =
            Boolean.parseBoolean(getAttribute(rootElement, "metadata-complete", "false"));
        webApp.setMetaDataComplete(metaDataComplete);
        LOG.debug("metadata-complete is: " + metaDataComplete);
        // web-app elements
        webApp.setDisplayName(getTextContent(getChild(rootElement, "display-name")));
        parseContextParams(rootElement, webApp);
        parseSessionConfig(rootElement, webApp);
        parseServlets(rootElement, webApp);
        parseFilters(rootElement, webApp);
        parseListeners(rootElement, webApp);
        parseErrorPages(rootElement, webApp);
        parseWelcomeFiles(rootElement, webApp);
        parseMimeMappings(rootElement, webApp);
        parseSecurity(rootElement, webApp);

        LOG.debug("scanninf for ServletContainerInitializers");

        ServiceLoader<ServletContainerInitializer> serviceLoader =
            ServiceLoader.load(
                ServletContainerInitializer.class, bundle.getClass().getClassLoader());
        if (serviceLoader != null) {
          LOG.debug("ServletContainerInitializers found");
          while (serviceLoader.iterator().hasNext()) {
            // for (ServletContainerInitializer service :
            // serviceLoader) {
            ServletContainerInitializer service = null;
            try {
              bundle.loadClass(ServletContainerInitializer.class.getName());
              Object obj = serviceLoader.iterator().next();
              if (obj instanceof ServletContainerInitializer)
                service = (ServletContainerInitializer) obj;
              else continue;
            } catch (ServiceConfigurationError e) {
              LOG.error("ServiceConfigurationError loading ServletContainerInitializer", e);
              continue;
            } catch (ClassNotFoundException e) {
              LOG.error("ServiceConfigurationError loading ServletContainerInitializer", e);
              continue;
            }
            WebAppServletContainerInitializer webAppServletContainerInitializer =
                new WebAppServletContainerInitializer();
            webAppServletContainerInitializer.setServletContainerInitializer(service);
            if (!webApp.getMetaDataComplete() && majorVersion != null && majorVersion >= 3) {
              HandlesTypes annotation = service.getClass().getAnnotation(HandlesTypes.class);
              Class[] classes;
              if (annotation != null) {
                // add annotated classes to service
                classes = annotation.value();
                webAppServletContainerInitializer.setClasses(classes);
              }
            }
            webApp.addServletContainerInitializer(webAppServletContainerInitializer);
          }
        }

        if (!webApp.getMetaDataComplete() && majorVersion != null && majorVersion >= 3) {
          LOG.debug("metadata-complete is either false or not set");

          LOG.debug("scanning for annotated classes");
          Enumeration<?> clazzes = bundle.findEntries("/", "*.class", true);

          for (; clazzes.hasMoreElements(); ) {
            URL clazzUrl = (URL) clazzes.nextElement();
            Class<?> clazz;
            String clazzFile = clazzUrl.getFile();
            LOG.debug("Class file found at :" + clazzFile);
            if (clazzFile.startsWith("/WEB-INF/classes"))
              clazzFile = clazzFile.replaceFirst("/WEB-INF/classes", "");
            else if (clazzFile.startsWith("/WEB-INF/lib"))
              clazzFile = clazzFile.replaceFirst("/WEB-INF/lib", "");
            String clazzName =
                clazzFile.replaceAll("/", ".").replaceAll(".class", "").replaceFirst(".", "");
            try {
              clazz = bundle.loadClass(clazzName);
            } catch (ClassNotFoundException e) {
              LOG.debug("Class {} not found", clazzName);
              continue;
            }
            if (clazz.isAnnotationPresent(WebServlet.class)) {
              LOG.debug("found WebServlet annotation on class: " + clazz);
              WebServletAnnotationScanner annonScanner =
                  new WebServletAnnotationScanner(bundle, clazz.getCanonicalName());
              annonScanner.scan(webApp);
            } else if (clazz.isAnnotationPresent(WebFilter.class)) {
              LOG.debug("found WebFilter annotation on class: " + clazz);
              WebFilterAnnotationScanner filterScanner =
                  new WebFilterAnnotationScanner(bundle, clazz.getCanonicalName());
              filterScanner.scan(webApp);
            } else if (clazz.isAnnotationPresent(WebListener.class)) {
              LOG.debug("found WebListener annotation on class: " + clazz);
              addWebListener(webApp, clazz.getSimpleName());
            }
          }
          LOG.debug("class scanning done");
        }

        // special handling for finding JSF Context listeners wrapped in
        // *.tld files
        Enumeration tldEntries = bundle.getResources("*.tld");
        // Enumeration tldEntries = bundle.findEntries("/", "*.tld",
        // true);
        while (tldEntries != null && tldEntries.hasMoreElements()) {
          URL url = (URL) tldEntries.nextElement();
          Element rootTld = getRootElement(url.openStream());
          if (rootTld != null) {
            parseListeners(rootTld, webApp);
          }
        }

      } else {
        LOG.warn("The parsed web.xml does not have a root element");
        return null;
      }
    } catch (ParserConfigurationException ignore) {
      LOG.error("Cannot parse web.xml", ignore);
    } catch (IOException ignore) {
      LOG.error("Cannot parse web.xml", ignore);
    } catch (SAXException ignore) {
      LOG.error("Cannot parse web.xml", ignore);
    }
    return webApp;
  }