All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.myfaces.webapp.FacesInitializerImpl Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.myfaces.webapp;

import org.apache.myfaces.config.FacesConfigValidator;
import org.apache.myfaces.config.FacesConfigurator;
import org.apache.myfaces.config.RuntimeConfig;
import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
import org.apache.myfaces.context.ExceptionHandlerImpl;
import org.apache.myfaces.application.viewstate.StateUtils;
import org.apache.myfaces.util.WebConfigParamUtils;
import org.apache.myfaces.cdi.util.BeanEntry;
import org.apache.myfaces.spi.InjectionProvider;
import org.apache.myfaces.spi.InjectionProviderException;
import org.apache.myfaces.spi.InjectionProviderFactory;
import org.apache.myfaces.util.ExternalSpecifications;
import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;

import jakarta.el.ELManager;
import jakarta.el.ExpressionFactory;
import jakarta.faces.application.Application;
import jakarta.faces.application.ProjectStage;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.ExceptionHandler;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.PostConstructApplicationEvent;
import jakarta.faces.event.PreDestroyApplicationEvent;
import jakarta.faces.event.SystemEvent;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.application.ViewVisitOption;
import jakarta.faces.push.PushContext;
import jakarta.websocket.DeploymentException;
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpointConfig;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ServiceLoader;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.myfaces.cdi.util.CDIUtils;
import org.apache.myfaces.config.annotation.CdiAnnotationProviderExtension;
import org.apache.myfaces.config.webparameters.MyfacesConfig;
import org.apache.myfaces.push.EndpointImpl;
import org.apache.myfaces.push.WebsocketConfigurator;
import org.apache.myfaces.push.cdi.WebsocketSessionManager;
import org.apache.myfaces.util.lang.ClassUtils;
import org.apache.myfaces.spi.ServiceProviderFinder;
import org.apache.myfaces.spi.ServiceProviderFinderFactory;
import org.apache.myfaces.view.facelets.ViewPoolProcessor;
import org.apache.myfaces.util.lang.StringUtils;

/**
 * Performs common initialization tasks.
 */
public class FacesInitializerImpl implements FacesInitializer
{
    private static final Logger log = Logger.getLogger(FacesInitializerImpl.class.getName());

    public static final String CDI_BEAN_MANAGER_INSTANCE = "oam.cdi.BEAN_MANAGER_INSTANCE";

    private static final String CDI_SERVLET_CONTEXT_BEAN_MANAGER_ATTRIBUTE = 
        "jakarta.enterprise.inject.spi.BeanManager";

    public static final String INJECTED_BEAN_STORAGE_KEY = "org.apache.myfaces.spi.BEAN_ENTRY_STORAGE";

    public static final String INITIALIZED = "org.apache.myfaces.INITIALIZED";
    public static final String PUSH_INITIALIZED = "org.apache.myfaces.push.INITIALIZED";

    private static final byte FACES_INIT_PHASE_PREINIT = 0;
    private static final byte FACES_INIT_PHASE_POSTINIT = 1;
    private static final byte FACES_INIT_PHASE_PREDESTROY = 2;
    private static final byte FACES_INIT_PHASE_POSTDESTROY = 3;

    /**
     * Performs all necessary initialization tasks like configuring this Faces
     * application.
     * 
     * @param servletContext The current {@link ServletContext}
     */
    @Override
    public void initFaces(ServletContext servletContext)
    {
        if (Boolean.TRUE.equals(servletContext.getAttribute(INITIALIZED)))
        {
            if (log.isLoggable(Level.FINEST))
            {
                log.finest("MyFaces already initialized");
            }
            return;
        }

        try
        {
            if (log.isLoggable(Level.FINEST))
            {
                log.finest("Initializing MyFaces");
            }

            long start = System.currentTimeMillis();

            // Some parts of the following configuration tasks have been implemented 
            // by using an ExternalContext. However, that's no problem as long as no 
            // one tries to call methods depending on either the ServletRequest or 
            // the ServletResponse.
            // Faces 2.0: FacesInitializer now has some new methods to
            // use proper startup FacesContext and ExternalContext instances.
            FacesContext facesContext = initStartupFacesContext(servletContext);
            ExternalContext externalContext = facesContext.getExternalContext();
            
            dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_PREINIT);

            // Setup ServiceProviderFinder
            ServiceProviderFinder spf = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext);
            Map> spfConfig = spf.calculateKnownServiceProviderMapInfo(
                externalContext, ServiceProviderFinder.KNOWN_SERVICES);
            if (spfConfig != null)
            {
                spf.initKnownServiceProviderMapInfo(externalContext, spfConfig);
            }

            if (!WebConfigParamUtils.getBooleanInitParameter(externalContext,
                    MyfacesConfig.INITIALIZE_ALWAYS_STANDALONE, false))
            {
                // check to see if the FacesServlet was found by MyFacesContainerInitializer
                Boolean mappingAdded = (Boolean) servletContext.getAttribute(
                    MyFacesContainerInitializer.FACES_SERVLET_FOUND);

                if (mappingAdded == null || !mappingAdded)
                {
                    // check if the FacesServlet has been added dynamically
                    // in a Servlet 3.0 environment by MyFacesContainerInitializer
                    mappingAdded = (Boolean) servletContext.getAttribute(
                        MyFacesContainerInitializer.FACES_SERVLET_ADDED_ATTRIBUTE);

                    if (mappingAdded == null || !mappingAdded)
                    {
                        if (log.isLoggable(Level.WARNING))
                        {
                            log.warning("No mappings of FacesServlet found. Abort initializing MyFaces.");
                        }
                        return;
                    }
                }
            }

            initCDIIntegration(servletContext, externalContext);

            initContainerIntegration(servletContext, externalContext);

            // log environment integrations
            ExternalSpecifications.isCDIAvailable(externalContext);
            ExternalSpecifications.isBeanValidationAvailable();

            String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION);
            if ("false".equals(useEncryption))
            {
                log.warning(StateUtils.USE_ENCRYPTION + " is set to false. " 
                        + "This is unsecure and should only be used for local or intranet applications!");
            }
            else
            {
                StateUtils.initSecret(servletContext);
            }

            _dispatchApplicationEvent(servletContext, PostConstructApplicationEvent.class);
            
            initWebsocketIntegration(servletContext, externalContext);

            WebConfigParamsLogger contextParamLogger = new WebConfigParamsLogger();
            contextParamLogger.logWebContextParams(facesContext);

            //Start ViewPoolProcessor if necessary
            ViewPoolProcessor.initialize(facesContext);

            MyfacesConfig config = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
            if (config.isAutomaticExtensionlessMapping())
            {
                initAutomaticExtensionlessMapping(facesContext, servletContext);
            }

            // publish resourceBundleControl to applicationMap, to make it available to the API
            ResourceBundle.Control resourceBundleControl = config.getResourceBundleControl();
            if (resourceBundleControl != null)
            {
                facesContext.getExternalContext().getApplicationMap().put(
                        MyfacesConfig.RESOURCE_BUNDLE_CONTROL, resourceBundleControl);
            }

            // print out a very prominent log message if the project stage is != Production
            if (!facesContext.isProjectStage(ProjectStage.Production)
                    && !facesContext.isProjectStage(ProjectStage.UnitTest))
            {
                ProjectStage projectStage = facesContext.getApplication().getProjectStage();
                StringBuilder message = new StringBuilder("\n\n");
                message.append("********************************************************************\n");
                message.append("*** WARNING: Apache MyFaces Core is running in ");
                message.append(projectStage.name().toUpperCase());        
                message.append(" mode.");
                int length = projectStage.name().length();
                for (int i = 0; i < 11 - length; i++)
                {
                    message.append(' ');
                }
                message.append(" ***\n");
                message.append("***                                            ");
                for (int i = 0; i < length; i++)
                {
                    message.append('^');
                }
                for (int i = 0; i < 18 - length; i++)
                {
                    message.append(' ');
                }
                message.append("***\n");
                message.append("*** Do NOT deploy to your live server(s) without changing this.  ***\n");
                message.append("*** See Application#getProjectStage() for more information.      ***\n");
                message.append("********************************************************************\n");
                message.append("\n");
                log.log(Level.WARNING, message.toString());
            }

            cleanupAfterStartup(facesContext);
            
            dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_POSTINIT);
            
            destroyStartupFacesContext(facesContext);
            
            servletContext.setAttribute(INITIALIZED, Boolean.TRUE);
            
            log.log(Level.INFO, "MyFaces Core has started, it took ["
                    + (System.currentTimeMillis() - start)
                    + "] ms.");
        }
        catch (Exception ex)
        {
            log.log(Level.SEVERE, "An error occurred while initializing MyFaces: "
                      + ex.getMessage(), ex);
        }
    }

    protected void cleanupAfterStartup(FacesContext facesContext)
    {
        ExternalContext externalContext = facesContext.getExternalContext();
        
        if (ExternalSpecifications.isCDIAvailable(externalContext))
        {
            BeanManager beanManager = CDIUtils.getBeanManager(externalContext);
            CdiAnnotationProviderExtension extension = CDIUtils.getOptional(beanManager,
                    CdiAnnotationProviderExtension.class);
            if (extension != null)
            {
                extension.release();
            }
        }
    }
    
    /**
     * Eventually we can use our plugin infrastructure for this as well
     * it would be a cleaner interception point than the base class
     * but for now this position is valid as well
     * 

* Note we add it for now here because the application factory object * leaves no possibility to have a destroy interceptor * and applications are per web application singletons * Note if this does not work out * move the event handler into the application factory * * @param servletContext The current {@link ServletContext} * @param eventClass the class to be passed down into the dispatching * code */ private void _dispatchApplicationEvent(ServletContext servletContext, Class eventClass) { FacesContext facesContext = FacesContext.getCurrentInstance(); Application application = facesContext.getApplication(); application.publishEvent(facesContext, eventClass, Application.class, application); } /** * Cleans up all remaining resources (well, theoretically). * * @param servletContext The current {@link ServletContext} */ @Override public void destroyFaces(ServletContext servletContext) { if (!Boolean.TRUE.equals(servletContext.getAttribute(INITIALIZED))) { return; } FacesContext facesContext = initShutdownFacesContext(servletContext); dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_PREDESTROY); _dispatchApplicationEvent(servletContext, PreDestroyApplicationEvent.class); _callPreDestroyOnInjectedJSFArtifacts(facesContext); // clear the cache of MetaRulesetImpl in order to prevent a memory leak MetaRulesetImpl.clearMetadataTargetCache(); if (facesContext.getExternalContext().getApplicationMap().containsKey(PUSH_INITIALIZED)) { BeanManager beanManager = CDIUtils.getBeanManager(facesContext); WebsocketSessionManager sessionManager = CDIUtils.get(beanManager, WebsocketSessionManager.class); sessionManager.clearSessions(); } // clear UIViewParameter default renderer map try { Method m = jakarta.faces.component.UIViewParameter.class.getDeclaredMethod("releaseRenderer"); m.setAccessible(true); m.invoke(null); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { log.log(Level.SEVERE, e.getMessage(), e); } // TODO is it possible to make a real cleanup? // Destroy startup FacesContext, but note we do before publish postdestroy event on // plugins and before release factories. destroyShutdownFacesContext(facesContext); FactoryFinder.releaseFactories(); dispatchInitializationEvent(servletContext, FACES_INIT_PHASE_POSTDESTROY); servletContext.removeAttribute(INITIALIZED); } /** * Configures this Faces application. It's required that every * FacesInitializer (i.e. every subclass) calls this method during * initialization. * * @param servletContext the current ServletContext * @param externalContext the current ExternalContext * @param expressionFactory the ExpressionFactory to use * @return the current runtime configuration */ protected RuntimeConfig buildConfiguration(ServletContext servletContext, ExternalContext externalContext, ExpressionFactory expressionFactory) { RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext); runtimeConfig.setExpressionFactory(expressionFactory); // And configure everything new FacesConfigurator(externalContext).configure(); validateFacesConfig(servletContext, externalContext); return runtimeConfig; } protected void validateFacesConfig(ServletContext servletContext, ExternalContext externalContext) { String validate = servletContext.getInitParameter(MyfacesConfig.VALIDATE); if ("true".equals(validate) && log.isLoggable(Level.WARNING)) // the default value is false { List warnings = FacesConfigValidator.validate(externalContext); for (String warning : warnings) { log.warning(warning); } } } /** * Try to load user-definied ExpressionFactory. Returns null, * if no custom ExpressionFactory was specified. * * @param externalContext the current ExternalContext * @return User-specified ExpressionFactory, or * null, if no no custom implementation was specified */ protected static ExpressionFactory getUserDefinedExpressionFactory(ExternalContext externalContext) { String expressionFactoryClassName = MyfacesConfig.getCurrentInstance(externalContext).getExpressionFactory(); if (StringUtils.isNotBlank(expressionFactoryClassName)) { if (log.isLoggable(Level.FINE)) { log.fine("Attempting to load the ExpressionFactory implementation " + "you've specified: '" + expressionFactoryClassName + "'."); } return loadExpressionFactory(expressionFactoryClassName); } return null; } /** * Loads and instantiates the given ExpressionFactory implementation. * * @param expressionFactoryClassName the class name of the ExpressionFactory implementation * @return the newly created ExpressionFactory implementation, or * null, if an error occurred */ protected static ExpressionFactory loadExpressionFactory(String expressionFactoryClassName) { return loadExpressionFactory(expressionFactoryClassName, true); } protected static ExpressionFactory loadExpressionFactory(String expressionFactoryClassName, boolean logMissing) { try { ClassLoader cl = ClassUtils.getContextClassLoader(); if (cl == null) { cl = FacesInitializerImpl.class.getClassLoader(); } Class expressionFactoryClass = cl.loadClass(expressionFactoryClassName); return (ExpressionFactory) expressionFactoryClass.newInstance(); } catch (Exception ex) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "An error occurred while instantiating a new ExpressionFactory. " + "Attempted to load class '" + expressionFactoryClassName + "'.", ex); } } return null; } @Override public FacesContext initStartupFacesContext(ServletContext servletContext) { // We cannot use FacesContextFactory, because it is necessary to initialize // before Application and RenderKit factories, so we should use different object. return _createFacesContext(servletContext, true); } @Override public void destroyStartupFacesContext(FacesContext facesContext) { _releaseFacesContext(facesContext); } @Override public FacesContext initShutdownFacesContext(ServletContext servletContext) { return _createFacesContext(servletContext, false); } @Override public void destroyShutdownFacesContext(FacesContext facesContext) { _releaseFacesContext(facesContext); } private FacesContext _createFacesContext(ServletContext servletContext, boolean startup) { ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, startup); ExceptionHandler exceptionHandler = new ExceptionHandlerImpl(); FacesContext facesContext = new StartupFacesContextImpl(externalContext, externalContext, exceptionHandler, startup); // If getViewRoot() is called during application startup or shutdown, // it should return a new UIViewRoot with its locale set to Locale.getDefault(). UIViewRoot startupViewRoot = new UIViewRoot(); startupViewRoot.setLocale(Locale.getDefault()); facesContext.setViewRoot(startupViewRoot); return facesContext; } private void _releaseFacesContext(FacesContext facesContext) { // make sure that the facesContext gets released. // This is important in an OSGi environment if (facesContext != null) { facesContext.release(); } } /** * The intention of this method is provide a point where CDI integration is done. * {@link jakarta.faces.flow.FlowScoped} and {@link jakarta.faces.view.ViewScoped} requires CDI in order to work, * so this method should set a BeanManager instance on application map under * the key "oam.cdi.BEAN_MANAGER_INSTANCE". * The default implementation look on ServletContext first and then use JNDI. * * @param servletContext * @param externalContext */ protected void initCDIIntegration( ServletContext servletContext, ExternalContext externalContext) { // Lookup bean manager and put it into an application scope attribute to // access it later. Remember the trick here is do not call any CDI api // directly, so if no CDI api is on the classpath no exception will be thrown. // Try with servlet context BeanManager beanManager = (BeanManager) servletContext.getAttribute(CDI_SERVLET_CONTEXT_BEAN_MANAGER_ATTRIBUTE); if (beanManager == null) { beanManager = lookupBeanManagerFromJndi(); } if (beanManager == null) { beanManager = lookupBeanManagerFromCDI(); } if (beanManager != null) { externalContext.getApplicationMap().put(CDI_BEAN_MANAGER_INSTANCE, beanManager); } } /** * This method tries to use the CDI-1.1 CDI.current() method to lookup the CDI BeanManager. * * @return the BeanManager or {@code null} if either not in a CDI-1.1 environment * or the BeanManager doesn't exist yet. */ private BeanManager lookupBeanManagerFromCDI() { try { return CDI.current().getBeanManager(); } catch (Exception | LinkageError e) { // ignore } return null; } /** * Try to lookup the CDI BeanManager from JNDI. */ private BeanManager lookupBeanManagerFromJndi() { BeanManager beanManager = null; try { // in an application server InitialContext initialContext = new InitialContext(); beanManager = (BeanManager) initialContext.lookup("java:comp/BeanManager"); } catch (NamingException e) { // silently ignore } catch (NoClassDefFoundError e) { // ignore } if (beanManager == null) { try { // in an application server InitialContext initialContext = new InitialContext(); beanManager = (BeanManager) initialContext.lookup("java:comp/env/BeanManager"); } catch (NamingException e) { // silently ignore } catch (NoClassDefFoundError e) { // ignore } } return beanManager; } public void _callPreDestroyOnInjectedJSFArtifacts(FacesContext facesContext) { InjectionProvider injectionProvider = InjectionProviderFactory.getInjectionProviderFactory( facesContext.getExternalContext()).getInjectionProvider(facesContext.getExternalContext()); List injectedBeanStorage = (List)facesContext.getExternalContext().getApplicationMap().get(INJECTED_BEAN_STORAGE_KEY); if (injectedBeanStorage != null) { for (BeanEntry entry : injectedBeanStorage) { try { injectionProvider.preDestroy(entry.getInstance(), entry.getCreationMetaData()); } catch (InjectionProviderException ex) { log.log(Level.INFO, "Exception on PreDestroy", ex); } } injectedBeanStorage.clear(); } } protected void initWebsocketIntegration(ServletContext servletContext, ExternalContext externalContext) { Boolean b = WebConfigParamUtils.getBooleanInitParameter(externalContext, PushContext.ENABLE_WEBSOCKET_ENDPOINT_PARAM_NAME); if (Boolean.TRUE.equals(b)) { // get the instance // see https://docs.oracle.com/javaee/7/api/javax/websocket/server/ServerContainer.html) final ServerContainer serverContainer = (ServerContainer) servletContext.getAttribute(ServerContainer.class.getName()); if (serverContainer == null) { log.log(Level.INFO, "f:websocket support is enabled but the WebSocket ServerContainer instance " + "cannot be found in the current ServletContext."); return; } try { serverContainer.addEndpoint(ServerEndpointConfig.Builder .create(EndpointImpl.class, EndpointImpl.JAKARTA_FACES_PUSH_PATH) .configurator(new WebsocketConfigurator(externalContext)).build()); //Init LRU cache BeanManager beanManager = CDIUtils.getBeanManager(externalContext); WebsocketSessionManager sessionManager = CDIUtils.get(beanManager, WebsocketSessionManager.class); sessionManager.initSessionMap(externalContext); externalContext.getApplicationMap().put(PUSH_INITIALIZED, true); } catch (DeploymentException e) { log.log(Level.INFO, "Exception on initialize Websocket Endpoint: ", e); } } } /** * * @since 2.3 * @param facesContext * @param servletContext */ protected void initAutomaticExtensionlessMapping(FacesContext facesContext, ServletContext servletContext) { ServletRegistration facesServletRegistration = (ServletRegistration) servletContext.getAttribute( MyFacesContainerInitializer.FACES_SERVLET_SERVLETREGISTRATION); if (facesServletRegistration != null) { facesContext.getApplication().getViewHandler().getViews(facesContext, "/", ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME).forEach(s -> { facesServletRegistration.addMapping(s); }); } } protected void initContainerIntegration(ServletContext servletContext, ExternalContext externalContext) { ExpressionFactory expressionFactory = getUserDefinedExpressionFactory(externalContext); if (expressionFactory == null) { expressionFactory = ELManager.getExpressionFactory(); } if (expressionFactory == null) { throw new FacesException("No " + ExpressionFactory.class.getName() + " implementation found. " + "Please provide in web.xml: org.apache.myfaces.EXPRESSION_FACTORY"); } buildConfiguration(servletContext, externalContext, expressionFactory); } /** * the central initialisation event dispatcher which calls * our listeners * * @param context * @param operation */ private void dispatchInitializationEvent(ServletContext context, int operation) { if (operation == FACES_INIT_PHASE_PREINIT) { if (!loadFacesInitPluginsViaServiceLoader(context)) { loadFacesInitViaContextParam(context); } } List pluginEntries = (List) context.getAttribute(MyfacesConfig.FACES_INIT_PLUGINS); if (pluginEntries == null) { return; } //now we process the plugins for (StartupListener initializer : pluginEntries) { log.info("Processing plugin"); //for now the initializers have to be stateless to //so that we do not have to enforce that the initializer //must be serializable switch (operation) { case FACES_INIT_PHASE_PREINIT: initializer.preInit(context); break; case FACES_INIT_PHASE_POSTINIT: initializer.postInit(context); break; case FACES_INIT_PHASE_PREDESTROY: initializer.preDestroy(context); break; case FACES_INIT_PHASE_POSTDESTROY: initializer.postDestroy(context); break; default: break; } } log.info("Processing MyFaces plugins done"); } /** * Loads the faces init plugins per Service loader. * * @return false if there are not plugins defined via ServiceLoader. */ private boolean loadFacesInitPluginsViaServiceLoader(ServletContext servletContext) { ServiceLoader loader = ServiceLoader.load(StartupListener.class, ClassUtils.getContextClassLoader()); Iterator it = loader.iterator(); if (!it.hasNext()) { return false; } List listeners = new LinkedList<>(); while (it.hasNext()) { listeners.add(it.next()); } servletContext.setAttribute(MyfacesConfig.FACES_INIT_PLUGINS, listeners); return true; } /** * loads the faces init plugins per reflection from the context param. */ private void loadFacesInitViaContextParam(ServletContext servletContext) { String plugins = servletContext.getInitParameter(MyfacesConfig.FACES_INIT_PLUGINS); if (plugins == null) { return; } log.info("MyFaces Plugins found"); String[] pluginEntries = plugins.split(","); List listeners = new ArrayList<>(pluginEntries.length); for (String pluginEntry : pluginEntries) { try { Class pluginClass = ClassUtils.getContextClassLoader().loadClass(pluginEntry); if (pluginClass == null) { pluginClass = this.getClass().getClassLoader().loadClass(pluginEntry); } listeners.add((StartupListener) pluginClass.newInstance()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { log.log(Level.SEVERE, e.getMessage(), e); } } servletContext.setAttribute(MyfacesConfig.FACES_INIT_PLUGINS, listeners); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy