Search in sources :

Example 6 with DocumentInfo

use of com.sun.faces.config.manager.documents.DocumentInfo 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 7 with DocumentInfo

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

the class NavigationConfigProcessor method process.

// -------------------------------------------- Methods from ConfigProcessor
@Override
public void process(ServletContext sc, FacesContext facesContext, DocumentInfo[] documentInfos) throws Exception {
    NavigationHandler handler = getApplication().getNavigationHandler();
    for (DocumentInfo documentInfo : documentInfos) {
        if (LOGGER.isLoggable(FINE)) {
            LOGGER.log(FINE, format("Processing navigation-rule elements for document: ''{0}''", documentInfo.getSourceURI()));
        }
        Document document = documentInfo.getDocument();
        String namespace = document.getDocumentElement().getNamespaceURI();
        NodeList navigationRules = document.getDocumentElement().getElementsByTagNameNS(namespace, NAVIGATION_RULE);
        if (navigationRules != null && navigationRules.getLength() > 0) {
            addNavigationRules(navigationRules, handler, sc);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) NavigationHandler(jakarta.faces.application.NavigationHandler) Document(org.w3c.dom.Document) DocumentInfo(com.sun.faces.config.manager.documents.DocumentInfo)

Example 8 with DocumentInfo

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

the class ParseConfigResourceToDOMTask method call.

// ----------------------------------------------- Methods from Callable
/**
 * @return the result of the parse operation (a DOM)
 * @throws Exception if an error occurs during the parsing process
 */
@Override
public DocumentInfo call() throws Exception {
    try {
        Timer timer = Timer.getInstance();
        if (timer != null) {
            timer.startTiming();
        }
        Document document = getDocument();
        if (timer != null) {
            timer.stopTiming();
            timer.logResult("Parse " + documentURI.toURL().toExternalForm());
        }
        return new DocumentInfo(document, documentURI);
    } catch (Exception e) {
        throw new ConfigurationException(format("Unable to parse document ''{0}'': {1}", documentURI.toURL().toExternalForm(), e.getMessage()), e);
    }
}
Also used : Timer(com.sun.faces.util.Timer) ConfigurationException(com.sun.faces.config.ConfigurationException) Document(org.w3c.dom.Document) ConfigurationException(com.sun.faces.config.ConfigurationException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) DocumentInfo(com.sun.faces.config.manager.documents.DocumentInfo)

Example 9 with DocumentInfo

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

Example 10 with DocumentInfo

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

the class Documents method getXMLDocuments.

/**
 * <p>
 * Obtains an array of <code>Document</code>s to be processed
 * </p>
 *
 * @param servletContext the <code>ServletContext</code> for the application to be processed
 * @param providers <code>List</code> of <code>ConfigurationResourceProvider</code> instances that provide the URL of
 * the documents to parse.
 * @param executor the <code>ExecutorService</code> used to dispatch parse request to
 * @param validating flag indicating whether or not the documents should be validated
 * @return an array of <code>DocumentInfo</code>s
 */
public static DocumentInfo[] getXMLDocuments(ServletContext servletContext, List<ConfigurationResourceProvider> providers, ExecutorService executor, boolean validating) {
    // Query all configuration providers to give us a URL to the configuration they are providing
    List<FutureTask<Collection<URI>>> uriTasks = new ArrayList<>(providers.size());
    for (ConfigurationResourceProvider provider : providers) {
        FutureTask<Collection<URI>> uriTask = new FutureTask<>(new FindConfigResourceURIsTask(provider, servletContext));
        uriTasks.add(uriTask);
        if (executor != null) {
            executor.execute(uriTask);
        } else {
            uriTask.run();
        }
    }
    // Load and XML parse all documents to which the URLs that we collected above point to
    List<FutureTask<DocumentInfo>> docTasks = new ArrayList<>(providers.size() << 1);
    for (FutureTask<Collection<URI>> uriTask : uriTasks) {
        try {
            for (URI uri : uriTask.get()) {
                FutureTask<DocumentInfo> docTask = new FutureTask<>(new ParseConfigResourceToDOMTask(servletContext, validating, uri));
                docTasks.add(docTask);
                if (executor != null) {
                    executor.execute(docTask);
                } else {
                    docTask.run();
                }
            }
        } catch (InterruptedException ignored) {
        } catch (Exception e) {
            throw new ConfigurationException(e);
        }
    }
    // Collect the results of the documents we parsed above
    List<DocumentInfo> docs = new ArrayList<>(docTasks.size());
    for (FutureTask<DocumentInfo> docTask : docTasks) {
        try {
            docs.add(docTask.get());
        } catch (ExecutionException e) {
            throw new ConfigurationException(e);
        } catch (InterruptedException ignored) {
        }
    }
    return docs.toArray(new DocumentInfo[docs.size()]);
}
Also used : ParseConfigResourceToDOMTask(com.sun.faces.config.manager.tasks.ParseConfigResourceToDOMTask) ConfigurationResourceProvider(com.sun.faces.spi.ConfigurationResourceProvider) ArrayList(java.util.ArrayList) URI(java.net.URI) ConfigurationException(com.sun.faces.config.ConfigurationException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FutureTask(java.util.concurrent.FutureTask) ConfigurationException(com.sun.faces.config.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Collection(java.util.Collection) FindConfigResourceURIsTask(com.sun.faces.config.manager.tasks.FindConfigResourceURIsTask) ExecutionException(java.util.concurrent.ExecutionException) DocumentInfo(com.sun.faces.config.manager.documents.DocumentInfo)

Aggregations

DocumentInfo (com.sun.faces.config.manager.documents.DocumentInfo)13 ArrayList (java.util.ArrayList)9 DocumentOrderingWrapper (com.sun.faces.config.manager.documents.DocumentOrderingWrapper)6 Document (org.w3c.dom.Document)4 ConfigurationException (com.sun.faces.config.ConfigurationException)2 FacesConfigInfo (com.sun.faces.config.manager.FacesConfigInfo)2 URI (java.net.URI)2 ExecutionException (java.util.concurrent.ExecutionException)2 FindConfigResourceURIsTask (com.sun.faces.config.manager.tasks.FindConfigResourceURIsTask)1 ParseConfigResourceToDOMTask (com.sun.faces.config.manager.tasks.ParseConfigResourceToDOMTask)1 ConfigurationResourceProvider (com.sun.faces.spi.ConfigurationResourceProvider)1 HighAvailabilityEnabler (com.sun.faces.spi.HighAvailabilityEnabler)1 InjectionProvider (com.sun.faces.spi.InjectionProvider)1 ThreadContext (com.sun.faces.spi.ThreadContext)1 Timer (com.sun.faces.util.Timer)1 FacesException (jakarta.faces.FacesException)1 ApplicationConfigurationPopulator (jakarta.faces.application.ApplicationConfigurationPopulator)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 IOException (java.io.IOException)1