001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.reload;
020
021import java.util.ResourceBundle;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.runtime.reload.ReloadEventNames;
028import org.nuxeo.runtime.services.event.Event;
029import org.nuxeo.runtime.services.event.EventListener;
030
031/**
032 * Handler for hot reload features.
033 * <p>
034 * Hot reload features should be enabled only if the application is in a "debug" mode, as some reloading can hurt a
035 * server in production (when altering document types declared on the platform, resetting caches, etc...)
036 * <p>
037 * Note that some hot reload features cannot be handled without additional debug configurations to be available (like
038 * the seam debug jar for instance).
039 * <p>
040 * This reload handler is supposed to handle at least (or most of) features used in Studio.
041 *
042 * @since 5.6
043 */
044public class NuxeoJSFReloadHandler implements EventListener {
045
046    private static final Log log = LogFactory.getLog(NuxeoJSFReloadHandler.class);
047
048    @Override
049    public boolean aboutToHandleEvent(Event event) {
050        return true;
051    }
052
053    @Override
054    public void handleEvent(Event event) {
055        if (!Framework.isDevModeSet()) {
056            log.info("Do not flush the JSF application: debug mode is not set");
057            return;
058        }
059        String id = event.getId();
060        if (ReloadEventNames.FLUSH_EVENT_ID.equals(id)) {
061            // force i18n messages reload at the bundle level
062            ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
063            // force reload of document default views.
064            DocumentModelFunctions.resetDefaultViewCache();
065        }
066    }
067
068}