Search in sources :

Example 1 with FacesConfigInfo

use of com.sun.faces.config.manager.FacesConfigInfo 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)

Example 2 with FacesConfigInfo

use of com.sun.faces.config.manager.FacesConfigInfo in project mojarra by eclipse-ee4j.

the class ProvideMetadataToAnnotationScanTask method initializeIvars.

private void initializeIvars(Set<Class<?>> annotatedSet) {
    if (uris != null || jarNames != null) {
        return;
    }
    uris = new HashSet<>(documentInfos.length);
    jarNames = new HashSet<>(documentInfos.length);
    for (DocumentInfo docInfo : documentInfos) {
        URI sourceURI = docInfo.getSourceURI();
        Matcher jarMatcher = JAR_PATTERN.matcher(sourceURI == null ? "" : sourceURI.toString());
        if (jarMatcher.matches()) {
            String jarName = jarMatcher.group(2);
            if (!jarNames.contains(jarName)) {
                FacesConfigInfo configInfo = new FacesConfigInfo(docInfo);
                if (!configInfo.isMetadataComplete()) {
                    uris.add(sourceURI);
                    jarNames.add(jarName);
                } else {
                    /*
                         * Because the container annotation scanning does not know anything about faces-config.xml metadata-complete the
                         * annotatedSet of classes will include classes that are not supposed to be included.
                         *
                         * The code below looks at the CodeSource of the class and determines whether or not it should be removed from the
                         * annotatedSet because the faces-config.xml that owns it has metadata-complete="true".
                         */
                    ArrayList<Class<?>> toRemove = new ArrayList<>(1);
                    String sourceURIString = sourceURI.toString();
                    if (annotatedSet != null) {
                        for (Class<?> clazz : annotatedSet) {
                            if (sourceURIString.contains(clazz.getProtectionDomain().getCodeSource().getLocation().toString())) {
                                toRemove.add(clazz);
                            }
                        }
                        annotatedSet.removeAll(toRemove);
                    }
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) FacesConfigInfo(com.sun.faces.config.manager.FacesConfigInfo) ArrayList(java.util.ArrayList) URI(java.net.URI) DocumentInfo(com.sun.faces.config.manager.documents.DocumentInfo)

Aggregations

FacesConfigInfo (com.sun.faces.config.manager.FacesConfigInfo)2 DocumentInfo (com.sun.faces.config.manager.documents.DocumentInfo)2 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 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Matcher (java.util.regex.Matcher)1 NamingException (javax.naming.NamingException)1