View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.apache.myfaces.tobago.application.ProjectStage;
23  import org.apache.myfaces.tobago.config.TobagoConfig;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.faces.application.FacesMessage;
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.PhaseEvent;
31  import javax.faces.event.PhaseId;
32  import javax.faces.event.PhaseListener;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.text.MessageFormat;
36  import java.util.Iterator;
37  import java.util.Map;
38  
39  public class DebugPhaseListener implements PhaseListener {
40  
41    private static final Logger LOG = LoggerFactory.getLogger(DebugPhaseListener.class);
42  
43    private static final String KEY = DebugPhaseListener.class.getName() + "_ID_";
44  
45    public void afterPhase(final PhaseEvent phaseEvent) {
46      final FacesContext facesContext = phaseEvent.getFacesContext();
47      final boolean productionMode = TobagoConfig.getInstance(facesContext).getProjectStage() == ProjectStage.Production;
48      if (facesContext.getResponseComplete() || productionMode) {
49        return;
50      }
51  
52      if (LOG.isInfoEnabled()) {
53        final Long end = System.currentTimeMillis();
54        final Map<String, Object> map = facesContext.getExternalContext().getRequestMap();
55        map.put(KEY + phaseEvent.getPhaseId().getOrdinal() + "E", end);
56  
57        if (LOG.isTraceEnabled()) {
58          LOG.trace("After Phase :" + phaseEvent.getPhaseId() + " Time=" + end);
59        }
60  
61        if (LOG.isDebugEnabled()) {
62          final Long start = (Long) map.get(KEY + phaseEvent.getPhaseId().getOrdinal() + "S");
63          LOG.debug("Phase " + phaseEvent.getPhaseId() + " needs " + (end - start + " milliseconds"));
64        }
65  
66        if (phaseEvent.getPhaseId().getOrdinal() == 6) {
67          if (LOG.isTraceEnabled()) {
68            final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
69            LOG.trace(" response Locale            = '" + response.getLocale() + "'");
70            LOG.trace(" response ContentType       = '" + response.getContentType() + "'");
71            LOG.trace(" response CharacterEncoding = '{" + response.getCharacterEncoding() + "}'");
72          }
73  
74          final Long start = (Long) map.get(KEY + "1S");
75          if (start != null) {
76            LOG.info("Total response time : " + (end - start + " milliseconds"));
77          }
78        }
79        for (final Iterator iterator = facesContext.getClientIdsWithMessages(); iterator.hasNext();) {
80          final String clientId = (String) iterator.next();
81  
82          for (final Iterator messageIterator = facesContext.getMessages(clientId); messageIterator.hasNext();) {
83            final FacesMessage msg = (FacesMessage) messageIterator.next();
84            LOG.info(MessageFormat.format("Faces message found."
85                + "\n  Component: {0} \n  Severity : {1}"
86                + "\n  Summary  : {2} \n  Detail   : {3}",
87                clientId, msg.getSeverity(), msg.getSummary(), msg.getDetail()));
88          }
89        }
90      }
91    }
92  
93    public void beforePhase(final PhaseEvent phaseEvent) {
94      final FacesContext facesContext = phaseEvent.getFacesContext();
95      final boolean productionMode = TobagoConfig.getInstance(facesContext).getProjectStage() == ProjectStage.Production;
96      if (facesContext.getResponseComplete()) {
97        LOG.info("Response is completed.");
98        return;
99      }
100     if (productionMode) {
101       LOG.warn("DebugPhaseListener disabled, because the project stage is 'production'.");
102       return;
103     }
104 
105     if (LOG.isInfoEnabled()) {
106       final PhaseId phaseId = phaseEvent.getPhaseId();
107       if (LOG.isDebugEnabled() || phaseId.getOrdinal() == 1) {
108 
109         final ExternalContext externalContext = facesContext.getExternalContext();
110 
111         if (LOG.isTraceEnabled() && PhaseId.RESTORE_VIEW == phaseId) {
112           // this is before restoreView
113 
114           final Object request = externalContext.getRequest();
115           if (request instanceof HttpServletRequest) {
116             final HttpServletRequest servletRequest = (HttpServletRequest) request;
117             LOG.trace("RequestURI = " + servletRequest.getRequestURI());
118           }
119           final Map headerMap = externalContext.getRequestHeaderMap();
120           for (final Object key : headerMap.keySet()) {
121             LOG.trace("Header : '" + key + "' = '" + headerMap.get(key) + "'");
122           }
123           final Map parameterMap = externalContext.getRequestParameterMap();
124           for (final Object key : parameterMap.keySet()) {
125             LOG.trace("Param  : '" + key + "' = '" + parameterMap.get(key) + "'");
126           }
127         }
128 
129         final Long start = System.currentTimeMillis();
130         final Map<String, Object> map = externalContext.getRequestMap();
131         map.put(KEY + phaseId.getOrdinal() + "S", start);
132 
133         if (LOG.isDebugEnabled()) {
134           Long end = null;
135           int ordinal = phaseId.getOrdinal();
136           while (end == null && ordinal > 0) {
137             end = (Long) map.get(KEY + --ordinal + "E");
138           }
139           if (end != null) {
140             LOG.debug("Time between phases " + ordinal + " and " + phaseId.getOrdinal() + ": "
141                 + (start - end) + " milliseconds");
142           }
143         }
144         if (LOG.isTraceEnabled()) {
145           LOG.trace("Before Phase :" + phaseId + " Time=" + start);
146         }
147       }
148     }
149   }
150 
151   public PhaseId getPhaseId() {
152     return PhaseId.ANY_PHASE;
153   }
154 }