001/*
002 * (C) Copyright 2016 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.rest;
020
021import javax.faces.event.PhaseEvent;
022import javax.faces.event.PhaseId;
023import javax.faces.event.PhaseListener;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028/**
029 * Logs time rendering for each JSF phase.
030 * <p>
031 * WARN: this listener is shared by all threads -- to be used in a controlled environment.
032 *
033 * @since 8.2
034 */
035public class JSFDebugPhaseListener implements PhaseListener {
036
037    private static final long serialVersionUID = 1L;
038
039    private static final Log log = LogFactory.getLog(JSFDebugPhaseListener.class);
040
041    protected long start;
042
043    @Override
044    public PhaseId getPhaseId() {
045        return PhaseId.ANY_PHASE;
046    }
047
048    @Override
049    public void beforePhase(PhaseEvent event) {
050        if (isEnabled()) {
051            start = System.currentTimeMillis();
052        }
053    }
054
055    @Override
056    public void afterPhase(PhaseEvent event) {
057        if (isEnabled()) {
058            long end = System.currentTimeMillis();
059            log.debug(event.getPhaseId() + ": " + (end - start) + " ms");
060        }
061    }
062
063    protected static boolean isEnabled() {
064        return log.isDebugEnabled();
065    }
066
067}