Search in sources :

Example 11 with ApplicationAssociate

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

the class CdiUtils method createConverter.

/**
 * Create a converter using the FacesConverter forClass attribute.
 *
 * @param beanManager the bean manager.
 * @param forClass the for class.
 * @return the converter, or null if we could not match one.
 */
public static Converter<?> createConverter(BeanManager beanManager, Class<?> forClass) {
    Converter<?> managedConverter = null;
    for (Class<?> forClassOrSuperclass = forClass; managedConverter == null && forClassOrSuperclass != null && forClassOrSuperclass != Object.class; forClassOrSuperclass = forClassOrSuperclass.getSuperclass()) {
        managedConverter = createConverter(beanManager, FacesConverter.Literal.of("", forClassOrSuperclass, true));
    }
    if (managedConverter != null) {
        ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance();
        // #4913
        associate.getAnnotationManager().applyConverterAnnotations(FacesContext.getCurrentInstance(), managedConverter);
        return new CdiConverter("", forClass, managedConverter);
    }
    return null;
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate)

Example 12 with ApplicationAssociate

use of com.sun.faces.application.ApplicationAssociate in project faces by jakartaee.

the class UserBean method init.

@PostConstruct
private void init() {
    ApplicationAssociate appAss = ApplicationAssociate.getInstance(extContext);
    firstName = "" + appAss.getFaceletFactory().getRefreshPeriod();
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) PostConstruct(jakarta.annotation.PostConstruct)

Example 13 with ApplicationAssociate

use of com.sun.faces.application.ApplicationAssociate in project faces by jakartaee.

the class UserBean method init.

@PostConstruct
private void init() {
    ApplicationAssociate appAss = ApplicationAssociate.getInstance(extContext);
    firstName = "" + appAss.getFaceletFactory().getRefreshPeriod();
}
Also used : ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) PostConstruct(jakarta.annotation.PostConstruct)

Example 14 with ApplicationAssociate

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

Example 15 with ApplicationAssociate

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

the class WebConfiguration method discoverResourceLibraryContracts.

private void discoverResourceLibraryContracts() {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext extContex = context.getExternalContext();
    Set<String> foundContracts = new HashSet<>();
    Set<String> candidates;
    // Scan for "contractMappings" in the web app root
    String contractsDirName = getOptionValue(WebContextInitParameter.WebAppContractsDirectory);
    assert null != contractsDirName;
    candidates = extContex.getResourcePaths(contractsDirName);
    if (null != candidates) {
        int contractsDirNameLen = contractsDirName.length();
        int end;
        for (String cur : candidates) {
            end = cur.length();
            if (cur.endsWith("/")) {
                end--;
            }
            foundContracts.add(cur.substring(contractsDirNameLen + 1, end));
        }
    }
    // Scan for "META-INF" contractMappings in the classpath
    try {
        URL[] candidateURLs = Classpath.search(Util.getCurrentLoader(this), META_INF_CONTRACTS_DIR, RESOURCE_CONTRACT_SUFFIX, Classpath.SearchAdvice.AllMatches);
        for (URL curURL : candidateURLs) {
            String cur = curURL.toExternalForm();
            int i = cur.indexOf(META_INF_CONTRACTS_DIR) + META_INF_CONTRACTS_DIR_LEN + 1;
            int j = cur.indexOf(RESOURCE_CONTRACT_SUFFIX);
            if (i < j) {
                foundContracts.add(cur.substring(i, j));
            }
        }
    } catch (IOException ioe) {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.log(Level.FINEST, "Unable to scan " + META_INF_CONTRACTS_DIR, ioe);
        }
    }
    if (foundContracts.isEmpty()) {
        return;
    }
    Map<String, List<String>> contractMappings = new HashMap<>();
    ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance();
    Map<String, List<String>> contractsFromConfig = associate.getResourceLibraryContracts();
    List<String> contractsToExpose;
    if (null != contractsFromConfig && !contractsFromConfig.isEmpty()) {
        List<String> contractsFromMapping;
        for (Map.Entry<String, List<String>> cur : contractsFromConfig.entrySet()) {
            // Verify that the contractsToExpose in this mapping actually exist
            // in the application. If not, log a message.
            contractsFromMapping = cur.getValue();
            if (null == contractsFromMapping || contractsFromMapping.isEmpty()) {
                if (LOGGER.isLoggable(Level.CONFIG)) {
                    LOGGER.log(Level.CONFIG, "resource library contract mapping for pattern {0} has no contracts.", cur.getKey());
                }
            } else {
                contractsToExpose = new ArrayList<>();
                for (String curContractFromMapping : contractsFromMapping) {
                    if (foundContracts.contains(curContractFromMapping)) {
                        contractsToExpose.add(curContractFromMapping);
                    } else {
                        if (LOGGER.isLoggable(Level.CONFIG)) {
                            LOGGER.log(Level.CONFIG, "resource library contract mapping for pattern {0} exposes contract {1}, but that contract is not available to the application.", new String[] { cur.getKey(), curContractFromMapping });
                        }
                    }
                }
                if (!contractsToExpose.isEmpty()) {
                    contractMappings.put(cur.getKey(), contractsToExpose);
                }
            }
        }
    } else {
        contractsToExpose = new ArrayList<>();
        contractsToExpose.addAll(foundContracts);
        contractMappings.put("*", contractsToExpose);
    }
    extContex.getApplicationMap().put(FaceletViewHandlingStrategy.RESOURCE_LIBRARY_CONTRACT_DATA_STRUCTURE_KEY, contractMappings);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ApplicationAssociate(com.sun.faces.application.ApplicationAssociate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IOException(java.io.IOException) URL(java.net.URL) ExternalContext(jakarta.faces.context.ExternalContext) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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