Search in sources :

Example 1 with ThreadContext

use of com.sun.faces.spi.ThreadContext in project mojarra by eclipse-ee4j.

the class ConfigManager method initialize.

// ---------------------------------------------------------- Public instance Methods
/**
 * <p>
 * This method bootstraps Faces based on the parsed configuration resources.
 * </p>
 *
 * @param servletContext the <code>ServletContext</code> for the application that requires initialization
 * @param facesContext the involved initialization faces context
 */
public void initialize(ServletContext servletContext, InitFacesContext facesContext) {
    if (!hasBeenInitialized(servletContext)) {
        initializedContexts.add(servletContext);
        initializeConfigProcessers(servletContext, facesContext);
        ExecutorService executor = null;
        try {
            WebConfiguration webConfig = WebConfiguration.getInstance(servletContext);
            boolean validating = webConfig.isOptionEnabled(ValidateFacesConfigFiles);
            if (useThreads(servletContext)) {
                executor = createExecutorService();
            }
            // Obtain and merge the XML and Programmatic documents
            DocumentInfo[] facesDocuments = mergeDocuments(getXMLDocuments(servletContext, getFacesConfigResourceProviders(), executor, validating), getProgrammaticDocuments(getConfigPopulators()));
            FacesConfigInfo lastFacesConfigInfo = new FacesConfigInfo(facesDocuments[facesDocuments.length - 1]);
            facesDocuments = sortDocuments(facesDocuments, lastFacesConfigInfo);
            InjectionProvider containerConnector = InjectionProviderFactory.createInstance(facesContext.getExternalContext());
            facesContext.getAttributes().put(INJECTION_PROVIDER_KEY, containerConnector);
            if (!lastFacesConfigInfo.isWebInfFacesConfig() || !lastFacesConfigInfo.isMetadataComplete()) {
                findAnnotations(facesDocuments, containerConnector, servletContext, facesContext, executor);
            }
            // See if the app is running in a HA enabled env
            if (containerConnector instanceof HighAvailabilityEnabler) {
                ((HighAvailabilityEnabler) containerConnector).enableHighAvailability(servletContext);
            }
            // Process the ordered and merged documents
            // This invokes a chain or processors where each processor grabs its own elements of interest
            // from each document.
            DocumentInfo[] facesDocuments2 = facesDocuments;
            configProcessors.subList(0, 3).stream().forEach(e -> {
                try {
                    e.process(servletContext, facesContext, facesDocuments2);
                } catch (Exception e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            });
            long parentThreadId = Thread.currentThread().getId();
            ClassLoader parentContextClassLoader = Thread.currentThread().getContextClassLoader();
            ThreadContext threadContext = getThreadContext(containerConnector);
            Object parentWebContext = threadContext != null ? threadContext.getParentWebContext() : null;
            configProcessors.subList(3, configProcessors.size()).stream().forEach(e -> {
                long currentThreadId = Thread.currentThread().getId();
                InitFacesContext initFacesContext = null;
                if (currentThreadId != parentThreadId) {
                    Thread.currentThread().setContextClassLoader(parentContextClassLoader);
                    initFacesContext = InitFacesContext.getInstance(servletContext);
                    if (parentWebContext != null) {
                        threadContext.propagateWebContextToChild(parentWebContext);
                    }
                } else {
                    initFacesContext = facesContext;
                }
                try {
                    e.process(servletContext, initFacesContext, facesDocuments2);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } finally {
                    if (currentThreadId != parentThreadId) {
                        Thread.currentThread().setContextClassLoader(null);
                        if (parentWebContext != null) {
                            threadContext.clearChildContext();
                        }
                    }
                }
            });
            faceletTaglibConfigProcessor.process(servletContext, facesContext, getXMLDocuments(servletContext, getFaceletConfigResourceProviders(), executor, validating));
        } catch (Exception e) {
            // Clear out any configured factories
            releaseFactories();
            Throwable t = e;
            if (!(e instanceof ConfigurationException)) {
                t = new ConfigurationException("CONFIGURATION FAILED! " + t.getMessage(), t);
            }
            throw (ConfigurationException) t;
        } finally {
            if (executor != null) {
                executor.shutdown();
            }
            servletContext.removeAttribute(ANNOTATIONS_SCAN_TASK_KEY);
        }
    }
    DbfFactory.removeSchemaMap(servletContext);
}
Also used : FacesConfigInfo(com.sun.faces.config.manager.FacesConfigInfo) InjectionProvider(com.sun.faces.spi.InjectionProvider) ThreadContext(com.sun.faces.spi.ThreadContext) HighAvailabilityEnabler(com.sun.faces.spi.HighAvailabilityEnabler) NamingException(javax.naming.NamingException) FacesException(jakarta.faces.FacesException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) DocumentInfo(com.sun.faces.config.manager.documents.DocumentInfo)

Aggregations

FacesConfigInfo (com.sun.faces.config.manager.FacesConfigInfo)1 DocumentInfo (com.sun.faces.config.manager.documents.DocumentInfo)1 HighAvailabilityEnabler (com.sun.faces.spi.HighAvailabilityEnabler)1 InjectionProvider (com.sun.faces.spi.InjectionProvider)1 ThreadContext (com.sun.faces.spi.ThreadContext)1 FacesException (jakarta.faces.FacesException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 NamingException (javax.naming.NamingException)1