Search in sources :

Example 1 with WebappLifecycleListener

use of com.sun.faces.application.WebappLifecycleListener in project mojarra by eclipse-ee4j.

the class ConfigureListener method contextInitialized.

// ------------------------------------------ ServletContextListener Methods
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    Timer timer = Timer.getInstance();
    if (timer != null) {
        timer.startTiming();
    }
    ConfigManager configManager = ConfigManager.getInstance(servletContext);
    if (configManager == null) {
        configManager = ConfigManager.createInstance(servletContext);
    }
    if (configManager.hasBeenInitialized(servletContext)) {
        return;
    }
    InitFacesContext initFacesContext = new InitFacesContext(servletContext);
    LOGGER.log(FINE, () -> format("ConfigureListener.contextInitialized({0})", servletContext.getContextPath()));
    webConfig = WebConfiguration.getInstance(servletContext);
    // Check to see if the FacesServlet is present in the
    // web.xml. If it is, perform faces configuration as normal,
    // otherwise, simply return.
    // If found by FacesInitializer.
    Object facesServletRegistration = servletContext.getAttribute(FACES_SERVLET_REGISTRATION);
    WebXmlProcessor webXmlProcessor = new WebXmlProcessor(servletContext);
    if (facesServletRegistration == null) {
        if (!webXmlProcessor.isFacesServletPresent()) {
            if (!webConfig.isOptionEnabled(ForceLoadFacesConfigFiles)) {
                LOGGER.log(FINE, "No FacesServlet found in deployment descriptor - bypassing configuration");
                WebConfiguration.clear(servletContext);
                configManager.destroy(servletContext, initFacesContext);
                ConfigManager.removeInstance(servletContext);
                InitFacesContext.cleanupInitMaps(servletContext);
                return;
            }
        } else {
            LOGGER.log(FINE, "FacesServlet found in deployment descriptor - processing configuration.");
        }
    }
    if (webXmlProcessor.isDistributablePresent()) {
        webConfig.setOptionEnabled(WebConfiguration.BooleanWebContextInitParameter.EnableDistributable, true);
        servletContext.setAttribute(WebConfiguration.BooleanWebContextInitParameter.EnableDistributable.getQualifiedName(), TRUE);
    }
    // Bootstrap of faces required
    webAppListener = new WebappLifecycleListener(servletContext);
    webAppListener.contextInitialized(servletContextEvent);
    ReflectionUtils.initCache(Thread.currentThread().getContextClassLoader());
    Throwable caughtThrowable = null;
    try {
        if (LOGGER.isLoggable(INFO)) {
            LOGGER.log(INFO, "faces.config.listener.version", servletContext.getContextPath());
        }
        if (webConfig.isOptionEnabled(VerifyFacesConfigObjects)) {
            LOGGER.warning("JSF1059: WARNING!  The com.sun.faces.verifyObjects feature is to aid developers not using tools.  " + "It shouldn't be enabled if using an IDE, or if this application is being deployed for production as it " + "will impact application start times.");
            // If we're verifying, force bean validation to occur at startup as well
            webConfig.overrideContextInitParameter(EnableLazyBeanValidation, false);
            Verifier.setCurrentInstance(new Verifier());
        }
        configManager.initialize(servletContext, initFacesContext);
        if (shouldInitConfigMonitoring()) {
            initConfigMonitoring(servletContext);
        }
        // Step 7, verify that all the configured factories are available
        // and optionally that configured objects can be created.
        Verifier verifier = Verifier.getCurrentInstance();
        if (verifier != null && !verifier.isApplicationValid() && LOGGER.isLoggable(SEVERE)) {
            LOGGER.severe("faces.config.verifyobjects.failures_detected");
            StringBuilder sb = new StringBuilder(128);
            for (String msg : verifier.getMessages()) {
                sb.append(msg).append('\n');
            }
            LOGGER.severe(sb.toString());
        }
        ApplicationAssociate associate = ApplicationAssociate.getInstance(servletContext);
        if (associate != null) {
            associate.setExpressionFactory(ELManager.getExpressionFactory());
        }
        initFacesContext.setELContext(new ELContextImpl(initFacesContext));
        if (associate != null) {
            associate.setContextName(servletContext.getContextPath());
            boolean isErrorPagePresent = webXmlProcessor.isErrorPagePresent();
            associate.setErrorPagePresent(isErrorPagePresent);
            servletContext.setAttribute(ERROR_PAGE_PRESENT_KEY_NAME, isErrorPagePresent);
        }
        // Note: websocket channel filter is registered in FacesInitializer.
        if (webConfig.isOptionEnabled(EnableWebsocketEndpoint)) {
            ServerContainer serverContainer = (ServerContainer) servletContext.getAttribute(ServerContainer.class.getName());
            if (serverContainer == null) {
                throw new UnsupportedOperationException("Cannot enable f:websocket." + " The current websocket container implementation does not support programmatically registering a container-provided endpoint.");
            }
            serverContainer.addEndpoint(ServerEndpointConfig.Builder.create(WebsocketEndpoint.class, URI_TEMPLATE).build());
        }
        webConfig.doPostBringupActions();
        configManager.publishPostConfigEvent();
    } catch (Throwable t) {
        LOGGER.log(SEVERE, "Critical error during deployment: ", t);
        caughtThrowable = t;
    } finally {
        servletContextEvent.getServletContext().removeAttribute(ANNOTATED_CLASSES);
        servletContextEvent.getServletContext().removeAttribute(FACES_SERVLET_MAPPINGS);
        Verifier.setCurrentInstance(null);
        LOGGER.log(FINE, "faces.config.listener.version.complete");
        if (timer != null) {
            timer.stopTiming();
            timer.logResult("Initialization of context " + servletContext.getContextPath());
        }
        if (caughtThrowable != null) {
            throw new RuntimeException(caughtThrowable);
        }
        // Bug 20458755: The InitFacesContext was not being cleaned up, resulting in
        // a partially constructed FacesContext being made available
        // to other code that re-uses this Thread at init time.
        initFacesContext.releaseCurrentInstance();
    }
}
Also used : WebappLifecycleListener(com.sun.faces.application.WebappLifecycleListener) ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) Timer(com.sun.faces.util.Timer) ServletContext(jakarta.servlet.ServletContext) ELContextImpl(com.sun.faces.el.ELContextImpl) ServerContainer(jakarta.websocket.server.ServerContainer)

Example 2 with WebappLifecycleListener

use of com.sun.faces.application.WebappLifecycleListener in project mojarra by eclipse-ee4j.

the class ConfigureListener method reload.

/**
 * This method will be invoked {@link WebConfigResourceMonitor} when changes to any of the faces-config.xml files
 * included in WEB-INF are modified.
 */
private void reload(ServletContext servletContext) {
    LOGGER.log(INFO, () -> format("Reloading Faces configuration for context {0}", servletContext.getContextPath()));
    // tear down the application
    try {
        // this will only be true in the automated test usage scenario
        if (webAppListener != null) {
            List<HttpSession> sessions = webAppListener.getActiveSessions();
            if (sessions != null) {
                for (HttpSession session : sessions) {
                    if (LOGGER.isLoggable(INFO)) {
                        LOGGER.log(INFO, "Invalidating Session {0}", session.getId());
                    }
                    session.invalidate();
                }
            }
        }
        // Release any allocated application resources
        FactoryFinder.releaseFactories();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        FacesContext initContext = new InitFacesContext(servletContext);
        ApplicationAssociate.clearInstance(initContext.getExternalContext());
        ApplicationAssociate.setCurrentInstance(null);
        // Release the initialization mark on this web application
        ConfigManager configManager = ConfigManager.getInstance(servletContext);
        if (configManager != null) {
            configManager.destroy(servletContext, initContext);
            ConfigManager.removeInstance(servletContext);
        } else {
            LOGGER.log(SEVERE, "Unexpected state during reload: no ConfigManager instance in current ServletContext but one is expected to exist.");
        }
        initContext.release();
        ReflectionUtils.clearCache(Thread.currentThread().getContextClassLoader());
        WebConfiguration.clear(servletContext);
    }
    // Bring the application back up.
    // No verification will be performed either to make this light weight.
    // init a new WebAppLifecycleListener so that the cached ApplicationAssociate
    // is removed.
    webAppListener = new WebappLifecycleListener(servletContext);
    InitFacesContext initContext = new InitFacesContext(servletContext);
    ReflectionUtils.initCache(Thread.currentThread().getContextClassLoader());
    try {
        ConfigManager configManager = ConfigManager.createInstance(servletContext);
        if (configManager != null) {
            configManager.initialize(servletContext, initContext);
        } else {
            LOGGER.log(SEVERE, "Unexpected state during reload: no ConfigManager instance in current ServletContext but one is expected to exist.");
        }
        ApplicationAssociate associate = ApplicationAssociate.getInstance(servletContext);
        if (associate != null) {
            Boolean errorPagePresent = (Boolean) servletContext.getAttribute(ERROR_PAGE_PRESENT_KEY_NAME);
            if (errorPagePresent != null) {
                associate.setErrorPagePresent(errorPagePresent);
                associate.setContextName(servletContext.getContextPath());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        initContext.release();
    }
    LOGGER.log(INFO, () -> format("Reload complete. ({0})", servletContext.getContextPath()));
}
Also used : FacesContext(jakarta.faces.context.FacesContext) WebappLifecycleListener(com.sun.faces.application.WebappLifecycleListener) ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) HttpSession(jakarta.servlet.http.HttpSession) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ApplicationAssociate (com.sun.faces.application.ApplicationAssociate)2 WebappLifecycleListener (com.sun.faces.application.WebappLifecycleListener)2 ELContextImpl (com.sun.faces.el.ELContextImpl)1 Timer (com.sun.faces.util.Timer)1 FacesContext (jakarta.faces.context.FacesContext)1 ServletContext (jakarta.servlet.ServletContext)1 HttpSession (jakarta.servlet.http.HttpSession)1 ServerContainer (jakarta.websocket.server.ServerContainer)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXException (org.xml.sax.SAXException)1