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 javax.servlet.http.HttpServletRequest; 030 import javax.servlet.http.HttpServletResponse; 031 import java.util.Date; 032 import java.util.Map; 033 import java.util.Iterator; 034 import java.text.MessageFormat; 035 036 /* 037 * Date: Dec 6, 2005 038 * Time: 7:50:25 PM 039 */ 040 public class DebugPhaseListener implements PhaseListener { 041 private static final Log LOG = LogFactory.getLog(DebugPhaseListener.class); 042 private static final String KEY = DebugPhaseListener.class.getName() + "_ID_"; 043 044 @SuppressWarnings("unchecked") 045 public void afterPhase(PhaseEvent phaseEvent) { 046 if (LOG.isInfoEnabled()) { 047 Date end = new Date(); 048 FacesContext facesContext = phaseEvent.getFacesContext(); 049 Map map = facesContext.getExternalContext().getRequestMap(); 050 map.put(KEY + phaseEvent.getPhaseId().getOrdinal() + "E", end); 051 052 if (LOG.isTraceEnabled()) { 053 LOG.trace("After Phase :" + phaseEvent.getPhaseId() 054 + " Time=" + end.getTime()); 055 } 056 057 if (LOG.isDebugEnabled()) { 058 Date start 059 = (Date) map.get(KEY + phaseEvent.getPhaseId().getOrdinal() + "S"); 060 LOG.debug("Phase " + phaseEvent.getPhaseId() + " needs " 061 + (end.getTime() - start.getTime() + " milliseconds")); 062 } 063 064 if (phaseEvent.getPhaseId().getOrdinal() == 6) { 065 if (LOG.isTraceEnabled()) { 066 HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); 067 LOG.trace(" response Locale = \"" + response.getLocale() + "\""); 068 LOG.trace(" response ContentType = \"" + response.getContentType() + "\""); 069 LOG.trace(" response CharacterEncoding = \"{" + response.getCharacterEncoding() + "}\""); 070 } 071 072 Date start = (Date) map.get(KEY + "1S"); 073 if (start != null) { 074 LOG.info("Total response time : " 075 + (end.getTime() - start.getTime() + " milliseconds")); 076 } 077 } 078 for (Iterator iter = facesContext.getClientIdsWithMessages(); iter.hasNext();) { 079 String clientId = (String) iter.next(); 080 081 for (Iterator msgIter = facesContext.getMessages(clientId); msgIter.hasNext();) { 082 FacesMessage msg = (FacesMessage) msgIter.next(); 083 LOG.info(MessageFormat.format("Faces message found." 084 + "\n Component: {0} \n Severity : {1}" 085 + "\n Summary : {2} \n Detail : {3}", 086 new Object[]{clientId, msg.getSeverity(), msg.getSummary(), msg.getDetail()})); 087 } 088 } 089 } 090 } 091 092 @SuppressWarnings("unchecked") 093 public void beforePhase(PhaseEvent phaseEvent) { 094 if (LOG.isInfoEnabled()) { 095 Date start = null; 096 Map map = null; 097 PhaseId phaseId = phaseEvent.getPhaseId(); 098 if (LOG.isDebugEnabled() || phaseId.getOrdinal() == 1) { 099 100 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 101 102 if (LOG.isTraceEnabled() && PhaseId.RESTORE_VIEW == phaseId) { 103 // this is before restoreView 104 105 Object request = externalContext.getRequest(); 106 if (request instanceof HttpServletRequest) { 107 HttpServletRequest servletRequest = (HttpServletRequest) request; 108 LOG.trace("RequestURI = " + servletRequest.getRequestURI()); 109 } 110 Map headerMap = externalContext.getRequestHeaderMap(); 111 for (Object key : headerMap.keySet()) { 112 LOG.trace("Header : \"" + key + "\" = \"" + headerMap.get(key) + "\""); 113 } 114 Map parameterMap = externalContext.getRequestParameterMap(); 115 for (Object key : parameterMap.keySet()) { 116 LOG.trace("Param : \"" + key + "\" = \"" + parameterMap.get(key) + "\""); 117 } 118 } 119 120 start = new Date(); 121 map = externalContext.getRequestMap(); 122 map.put(KEY + phaseId.getOrdinal() + "S", start); 123 } 124 125 if (LOG.isDebugEnabled()) { 126 Date end = null; 127 int ordinal = phaseId.getOrdinal(); 128 while (end == null && ordinal > 0) { 129 end = (Date) map.get(KEY + --ordinal + "E"); 130 } 131 if (end != null) { 132 LOG.debug("Time between phases " + ordinal + " and " + phaseId.getOrdinal() + ": " 133 + (start.getTime() - end.getTime()) + " milliseconds"); 134 } 135 } 136 if (LOG.isTraceEnabled()) { 137 LOG.trace("Before Phase :" + phaseId 138 + " Time=" + start.getTime()); 139 } 140 } 141 } 142 143 public PhaseId getPhaseId() { 144 return PhaseId.ANY_PHASE; 145 } 146 }