001 package org.apache.myfaces.tobago.util;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import javax.faces.context.FacesContext;
024 import javax.faces.context.ExternalContext;
025 import javax.faces.event.PhaseEvent;
026 import javax.faces.event.PhaseId;
027 import javax.faces.event.PhaseListener;
028 import javax.faces.application.FacesMessage;
029 import java.util.Date;
030 import java.util.Map;
031 import java.util.Iterator;
032 import java.text.MessageFormat;
033
034 /*
035 * Date: Dec 6, 2005
036 * Time: 7:50:25 PM
037 */
038 public class DebugPhaseListener implements PhaseListener {
039 private static final Log LOG = LogFactory.getLog(DebugPhaseListener.class);
040 private static final String KEY = DebugPhaseListener.class.getName() + "_ID_";
041
042 @SuppressWarnings("unchecked")
043 public void afterPhase(PhaseEvent phaseEvent) {
044 if (LOG.isInfoEnabled()) {
045 Date end = new Date();
046 FacesContext facesContext = phaseEvent.getFacesContext();
047 Map map = facesContext.getExternalContext().getRequestMap();
048 map.put(KEY + phaseEvent.getPhaseId().getOrdinal() + "E", end);
049
050 if (LOG.isTraceEnabled()) {
051 LOG.trace("After Phase :" + phaseEvent.getPhaseId()
052 + " Time=" + end.getTime());
053 }
054
055 if (LOG.isDebugEnabled()) {
056 Date start
057 = (Date) map.get(KEY + phaseEvent.getPhaseId().getOrdinal() + "S");
058 LOG.debug("Phase " + phaseEvent.getPhaseId() + " needs "
059 + (end.getTime() - start.getTime() + " milliseconds"));
060 }
061
062 if (phaseEvent.getPhaseId().getOrdinal() == 6) {
063 Date start = (Date) map.get(KEY + "1S");
064 if (start != null) {
065 LOG.info("Total response time : "
066 + (end.getTime() - start.getTime() + " milliseconds"));
067 }
068 }
069 for (Iterator iter = phaseEvent.getFacesContext().getClientIdsWithMessages(); iter.hasNext();) {
070 String clientId = (String) iter.next();
071
072 for (Iterator msgIter = facesContext.getMessages(clientId); msgIter.hasNext();) {
073 FacesMessage msg = (FacesMessage) msgIter.next();
074 LOG.info(MessageFormat.format("Faces message found."
075 + "\n Component: {0} \n Severity : {1}"
076 + "\n Summary : {2} \n Detail : {3}",
077 new Object[]{clientId, msg.getSeverity(), msg.getSummary(), msg.getDetail()}));
078 }
079 }
080 }
081 }
082
083 @SuppressWarnings("unchecked")
084 public void beforePhase(PhaseEvent phaseEvent) {
085 if (LOG.isInfoEnabled()) {
086 Date start = null;
087 Map map = null;
088 PhaseId phaseId = phaseEvent.getPhaseId();
089 if (LOG.isDebugEnabled() || phaseId.getOrdinal() == 1) {
090
091 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
092
093 if (LOG.isTraceEnabled() && PhaseId.RESTORE_VIEW == phaseId) {
094 // this is before restoreView
095 Map params = externalContext.getRequestParameterMap();
096 for (Object key : params.keySet()) {
097 LOG.trace("Param : \"" + key + "\" = \"" + params.get(key) + "\"");
098 }
099 }
100
101 start = new Date();
102 map = externalContext.getRequestMap();
103 map.put(KEY + phaseId.getOrdinal() + "S", start);
104 }
105
106 if (LOG.isDebugEnabled()) {
107 Date end = null;
108 int ordinal = phaseId.getOrdinal();
109 while (end == null && ordinal > 0) {
110 end = (Date) map.get(KEY + --ordinal + "E");
111 }
112 if (end != null) {
113 LOG.debug("Time between phases " + ordinal + " and " + phaseId.getOrdinal() + ": "
114 + (start.getTime() - end.getTime()) + " milliseconds");
115 }
116 }
117 if (LOG.isTraceEnabled()) {
118 LOG.trace("Before Phase :" + phaseId
119 + " Time=" + start.getTime());
120 }
121 }
122 }
123
124 public PhaseId getPhaseId() {
125 return PhaseId.ANY_PHASE;
126 }
127 }