001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.application.config;
018
019import javax.servlet.ServletContextEvent;
020import javax.servlet.ServletContextListener;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.runtime.RuntimeServiceEvent;
025import org.nuxeo.runtime.RuntimeServiceListener;
026import org.nuxeo.runtime.api.Framework;
027
028import com.sun.faces.config.ConfigureListener;
029
030/**
031 * Handles JSF configuration at startup.
032 * <p>
033 * Delays configuration until runtime is initialized to handle correctly dev mode.
034 *
035 * @since 6.0
036 */
037public class JSFConfigureListener implements ServletContextListener, RuntimeServiceListener {
038
039    private static final Log log = LogFactory.getLog(JSFConfigureListener.class);
040
041    protected ConfigureListener confListener;
042
043    protected ServletContextEvent origEvent;
044
045    @Override
046    public void contextInitialized(ServletContextEvent sce) {
047        origEvent = sce;
048        confListener = new ConfigureListener();
049        confListener.contextInitialized(sce);
050        Framework.addListener(this);
051    }
052
053    @Override
054    public void contextDestroyed(ServletContextEvent sce) {
055        confListener.contextDestroyed(sce);
056        Framework.removeListener(this);
057    }
058
059    @Override
060    public void handleEvent(RuntimeServiceEvent event) {
061        if (event.id == RuntimeServiceEvent.RUNTIME_STARTED) {
062            // reload app to make sure dev mode is taken into account by
063            // facelets cache factory
064            reload();
065            Framework.removeListener(this);
066        }
067    }
068
069    protected void reload() {
070        if (log.isDebugEnabled()) {
071            log.debug("Reloading JSF configuration");
072        }
073        if (Framework.isDevModeSet() && confListener != null) {
074            confListener.contextDestroyed(origEvent);
075            confListener.contextInitialized(origEvent);
076        }
077    }
078
079}