001/*
002 * (C) Copyright 2012 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.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.webapp.seam;
018
019import static org.jboss.seam.annotations.Install.FRAMEWORK;
020
021import java.io.Serializable;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.jboss.seam.ScopeType;
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Install;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.core.Events;
031import org.nuxeo.ecm.platform.ui.web.rest.RestfulPhaseListener;
032import org.nuxeo.ecm.webapp.helpers.EventNames;
033
034/**
035 * Conversation component that keeps the last update timestamp to handle hot reload when this timestamp changes.
036 * <p>
037 * Triggered by {@link RestfulPhaseListener} at the beginning of render response phase so that Seam components are not
038 * left in a strange state.
039 *
040 * @since 5.6
041 * @see RestfulPhaseListener
042 * @see NuxeoSeamHotReloader#shouldResetCache(Long)
043 * @see NuxeoSeamHotReloader#triggerResetOnSeamComponents()
044 */
045@Name("seamReloadContext")
046@Scope(ScopeType.CONVERSATION)
047@Install(precedence = FRAMEWORK)
048public class NuxeoSeamHotReloadContextKeeper implements Serializable {
049
050    private static final long serialVersionUID = 1L;
051
052    private static final Log log = LogFactory.getLog(NuxeoSeamHotReloader.class);
053
054    protected Long lastCacheKey;
055
056    @In(create = true)
057    protected NuxeoSeamHotReloader seamReload;
058
059    public void triggerReloadIdNeeded() {
060        if (lastCacheKey == null) {
061            doLog("No last cache key => no hot reload triggered");
062            lastCacheKey = Long.valueOf(System.currentTimeMillis());
063        } else {
064            if (seamReload.shouldResetCache(lastCacheKey)) {
065                doLog(String.format("Before reset, cache key=%s", lastCacheKey));
066                try {
067                    // trigger reset on Seam layer by raising the flush event
068                    Events.instance().raiseEvent(EventNames.FLUSH_EVENT);
069                } finally {
070                    // update cache key even if an error is triggered, to avoid
071                    // triggering cache reset over and over
072                    Long currentTimestamp = seamReload.getCurrentCacheTimestamp();
073                    if (currentTimestamp != null) {
074                        lastCacheKey = seamReload.getCurrentCacheTimestamp();
075                    }
076                }
077                doLog(String.format("After reset, cache key=%s", lastCacheKey));
078            } else {
079                doLog(String.format("No reset needed, cache key=%s", lastCacheKey));
080            }
081        }
082    }
083
084    protected void doLog(String message) {
085        if (log.isDebugEnabled()) {
086            log.debug(message);
087        }
088    }
089
090}