class WebXmlInitializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
        LOGGER.log(FINE, () -> "Entering WebXmlInitializer.onStartup");

        try {
            WebXmlParser parser = new WebXmlParser();
            WebXmlManager manager = new WebXmlManager();
            servletContext.setAttribute(WebXmlManager.KEY, manager);

            WebApplication webApp = (WebApplication) servletContext;
            InputStream inputStream = servletContext.getResourceAsStream("WEB-INF/web.xml");
            if (inputStream != null) {
                WebXml webXml = parser.parse(servletContext.getResourceAsStream("WEB-INF/web.xml"));
                manager.setWebXml(webXml);
                manager.setInitialWebXml(webXml);
            }

            ArrayList<WebXml> webFragments = new ArrayList<>();
            List<URL> webFragmentUrls = Collections.list(servletContext.getClassLoader().getResources("META-INF/web-fragment.xml"));
            for (URL url : webFragmentUrls) {
                try (InputStream stream = url.openStream()) {
                    WebXml webFragment = parser.parse(stream);
                    webFragment.setFragment(true);
                    webFragments.add(webFragment);
                }
            }
            if (!webFragments.isEmpty()) {
                manager.setWebFragments(webFragments);
            }

            if (manager.getWebXml() == null) {
                manager.setWebXml(new WebXml());
            }

            if (manager.getWebXml() != null) {
                WebXml webXml = manager.getWebXml();
                WebXmlProcessor processor = new WebXmlProcessor();

                processor.process(webXml, webApp);

                if (webXml.getMetadataComplete()) {
                    return;
                }

                manager.getOrderedFragments().forEach(fragment -> processor.process(fragment, webApp));
            } else {
                LOGGER.fine("No web.xml found!");
            }
        } catch (IOException e) {
            LOGGER.log(WARNING, "Unable to parse web.xml", e);
        }

        LOGGER.log(FINE, () -> "Exiting WebXmlInitializer.onStartup");
    }

}