Search in sources :

Example 6 with ApplicationAssociate

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

the class ConfigureListener method contextDestroyed.

@Override
public void contextDestroyed(ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();
    ConfigManager configManager = ConfigManager.getInstance(context);
    // The additional check for a WebConfiguration instance was added at the request of JBoss
    if (configManager == null && WebConfiguration.getInstanceWithoutCreating(context) != null) {
        LOGGER.log(WARNING, "Unexpected state during contextDestroyed: no ConfigManager instance in current ServletContext but one is expected to exist.");
    }
    InitFacesContext initContext = null;
    try {
        initContext = getInitFacesContext(context);
        if (initContext == null) {
            initContext = new InitFacesContext(context);
        } else {
            InitFacesContext.getThreadInitContextMap().put(Thread.currentThread(), initContext);
        }
        if (webAppListener != null) {
            webAppListener.contextDestroyed(sce);
            webAppListener = null;
        }
        if (webResourcePool != null) {
            webResourcePool.shutdownNow();
        }
        if (LOGGER.isLoggable(FINE)) {
            LOGGER.log(FINE, "ConfigureListener.contextDestroyed({0})", context.getServletContextName());
        }
        if (configManager == null || !configManager.hasBeenInitialized(context)) {
            return;
        }
        ApplicationAssociate associate = ApplicationAssociate.getInstance(context);
        if (associate != null) {
            associate.setExpressionFactory(ELManager.getExpressionFactory());
        }
        initContext.setELContext(new ELContextImpl(initContext));
        Application application = initContext.getApplication();
        application.publishEvent(initContext, PreDestroyApplicationEvent.class, Application.class, application);
    } catch (Exception e) {
        LOGGER.log(SEVERE, "Unexpected exception when attempting to tear down the Mojarra runtime", e);
    } finally {
        ApplicationAssociate.clearInstance(context);
        ApplicationAssociate.setCurrentInstance(null);
        // Release the initialization mark on this web application
        if (configManager != null) {
            configManager.destroy(context, initContext);
            ConfigManager.removeInstance(context);
        } else if (WebConfiguration.getInstanceWithoutCreating(context) != null && LOGGER.isLoggable(WARNING)) {
            LOGGER.log(WARNING, "Unexpected state during contextDestroyed: no ConfigManager instance in current ServletContext but one is expected to exist.");
        }
        FactoryFinder.releaseFactories();
        ReflectionUtils.clearCache(Thread.currentThread().getContextClassLoader());
        WebConfiguration.clear(context);
        InitFacesContext.cleanupInitMaps(context);
    }
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) ServletContext(jakarta.servlet.ServletContext) ELContextImpl(com.sun.faces.el.ELContextImpl) Application(jakarta.faces.application.Application) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with ApplicationAssociate

use of com.sun.faces.application.ApplicationAssociate 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 8 with ApplicationAssociate

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

the class FaceletWebappResourceHelper method findResourceUrlConsideringFlows.

private URL findResourceUrlConsideringFlows(String resourceName, boolean[] outDoNotCache) throws IOException {
    URL url = null;
    ClassLoader cl = Util.getCurrentLoader(this);
    Enumeration<URL> matches = cl.getResources(FLOW_IN_JAR_PREFIX + resourceName);
    try {
        url = matches.nextElement();
    } catch (NoSuchElementException nsee) {
        url = null;
    }
    if (url != null && matches.hasMoreElements()) {
        boolean keepGoing = true;
        FacesContext context = FacesContext.getCurrentInstance();
        Flow currentFlow = context.getApplication().getFlowHandler().getCurrentFlow(context);
        do {
            if (currentFlow != null && 0 < currentFlow.getDefiningDocumentId().length()) {
                String definingDocumentId = currentFlow.getDefiningDocumentId();
                ExternalContext extContext = context.getExternalContext();
                ApplicationAssociate associate = ApplicationAssociate.getInstance(extContext);
                if (associate.urlIsRelatedToDefiningDocumentInJar(url, definingDocumentId)) {
                    keepGoing = false;
                    outDoNotCache[0] = true;
                } else {
                    if (matches.hasMoreElements()) {
                        url = matches.nextElement();
                    } else {
                        keepGoing = false;
                    }
                }
            } else {
                keepGoing = false;
            }
        } while (keepGoing);
    }
    return url;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) ExternalContext(jakarta.faces.context.ExternalContext) URL(java.net.URL) NoSuchElementException(java.util.NoSuchElementException) Flow(jakarta.faces.flow.Flow)

Example 9 with ApplicationAssociate

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

the class ViewScopeManager method destroyBeans.

/**
 * Destroy the managed beans from the given view map.
 *
 * @param facesContext the Faces Context.
 * @param viewMap the view map.
 */
public void destroyBeans(FacesContext facesContext, Map<String, Object> viewMap) {
    LOGGER.log(FINEST, "Destroying @ViewScoped beans from view map: {0}", viewMap);
    ApplicationAssociate applicationAssociate = ApplicationAssociate.getInstance(facesContext.getExternalContext());
    if (applicationAssociate != null) {
        destroyBeans(applicationAssociate, viewMap);
    }
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate)

Example 10 with ApplicationAssociate

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

the class ViewScopeManager method sessionDestroyed.

/**
 * Destroy the associated data in the session.
 *
 * @param httpSessionEvent the HTTP session event.
 */
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    LOGGER.log(FINEST, "Cleaning up session for @ViewScoped beans");
    if (contextManager != null) {
        contextManager.sessionDestroyed(httpSessionEvent);
    }
    HttpSession session = httpSessionEvent.getSession();
    @SuppressWarnings("unchecked") Map<String, Object> activeViewMaps = (Map<String, Object>) session.getAttribute(ACTIVE_VIEW_MAPS);
    if (activeViewMaps != null) {
        Iterator<Object> activeViewMapsIterator = activeViewMaps.values().iterator();
        ApplicationAssociate applicationAssociate = ApplicationAssociate.getInstance(httpSessionEvent.getSession().getServletContext());
        while (activeViewMapsIterator.hasNext()) {
            @SuppressWarnings("unchecked") Map<String, Object> viewMap = (Map<String, Object>) activeViewMapsIterator.next();
            destroyBeans(applicationAssociate, viewMap);
        }
        activeViewMaps.clear();
        session.removeAttribute(ACTIVE_VIEW_MAPS);
        session.removeAttribute(ACTIVE_VIEW_MAPS_SIZE);
    }
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) HttpSession(jakarta.servlet.http.HttpSession) LRUMap(com.sun.faces.util.LRUMap) Map(java.util.Map)

Aggregations

ApplicationAssociate (com.sun.faces.application.ApplicationAssociate)22 FacesContext (jakarta.faces.context.FacesContext)5 NodeList (org.w3c.dom.NodeList)5 Application (jakarta.faces.application.Application)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3 WebappLifecycleListener (com.sun.faces.application.WebappLifecycleListener)2 ELContextImpl (com.sun.faces.el.ELContextImpl)2 PostConstruct (jakarta.annotation.PostConstruct)2 ExternalContext (jakarta.faces.context.ExternalContext)2 ServletContext (jakarta.servlet.ServletContext)2 HttpSession (jakarta.servlet.http.HttpSession)2 URL (java.net.URL)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Element (org.w3c.dom.Element)2