Coverage Report - org.apache.myfaces.lifecycle.LifecycleImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
LifecycleImpl
0%
0/76
0%
0/48
0
 
 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  
 package org.apache.myfaces.lifecycle;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 
 24  
 import javax.faces.FacesException;
 25  
 import javax.faces.context.FacesContext;
 26  
 import javax.faces.event.PhaseId;
 27  
 import javax.faces.event.PhaseListener;
 28  
 import javax.faces.lifecycle.Lifecycle;
 29  
 
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 import org.apache.myfaces.util.DebugUtils;
 33  
 import org.apache.myfaces.config.FacesConfigurator;
 34  
 import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
 35  
 
 36  
 /**
 37  
  * Implements the lifecycle as described in Spec. 1.0 PFD Chapter 2
 38  
  * @author Manfred Geiler
 39  
  * @author Nikolay Petrov
 40  
  */
 41  
 public class LifecycleImpl
 42  
         extends Lifecycle
 43  
 {
 44  0
     private static final Log log = LogFactory.getLog(LifecycleImpl.class);
 45  
 
 46  
     private PhaseExecutor[] lifecycleExecutors;
 47  
     private PhaseExecutor renderExecutor;
 48  
 
 49  0
     private final List<PhaseListener> _phaseListenerList = new ArrayList<PhaseListener>();
 50  
 
 51  
     /**
 52  
      * Lazy cache for returning _phaseListenerList as an Array.
 53  
      */
 54  0
     private PhaseListener[] _phaseListenerArray = null;
 55  
 
 56  0
     public LifecycleImpl() {
 57  
         // hide from public access
 58  0
         lifecycleExecutors = new PhaseExecutor[] {
 59  
                 new RestoreViewExecutor(),
 60  
                 new ApplyRequestValuesExecutor(),
 61  
                 new ProcessValidationsExecutor(),
 62  
                 new UpdateModelValuesExecutor(),
 63  
                 new InvokeApplicationExecutor()
 64  
         };
 65  
 
 66  0
         renderExecutor = new RenderResponseExecutor();
 67  0
     }
 68  
 
 69  
     public void execute(FacesContext facesContext) throws FacesException {
 70  
          //refresh all configuration information if according web-xml parameter is set.
 71  0
         WebXml.update(facesContext.getExternalContext());
 72  0
         new FacesConfigurator(facesContext.getExternalContext()).update();
 73  
 
 74  0
         PhaseListenerManager phaseListenerMgr = new PhaseListenerManager(this, facesContext, getPhaseListeners());
 75  0
         for(int executorIndex = 0;executorIndex < lifecycleExecutors.length;executorIndex++) {
 76  0
             if(executePhase(facesContext, lifecycleExecutors[executorIndex], phaseListenerMgr)) {
 77  0
                 return;
 78  
             }
 79  
         }
 80  0
     }
 81  
 
 82  
 
 83  
     private boolean executePhase(FacesContext facesContext, PhaseExecutor executor,
 84  
             PhaseListenerManager phaseListenerMgr) throws FacesException {
 85  
 
 86  0
         boolean skipFurtherProcessing = false;
 87  
 
 88  0
         if (log.isTraceEnabled()) {
 89  0
             log.trace("entering " + executor.getPhase() + " in " + LifecycleImpl.class.getName());
 90  
         }
 91  
 
 92  
         try {
 93  0
             phaseListenerMgr.informPhaseListenersBefore(executor.getPhase());
 94  
 
 95  0
             if(isResponseComplete(facesContext, executor.getPhase(), true)) {
 96  
                 // have to return right away
 97  0
                 return true;
 98  
             }
 99  0
             if(shouldRenderResponse(facesContext, executor.getPhase(), true)) {
 100  0
                 skipFurtherProcessing = true;
 101  
             }
 102  
 
 103  0
             if(executor.execute(facesContext)) {
 104  0
                 return true;
 105  
             }
 106  
         } finally {
 107  0
             phaseListenerMgr.informPhaseListenersAfter(executor.getPhase());
 108  0
         }
 109  
 
 110  
 
 111  0
         if (isResponseComplete(facesContext, executor.getPhase(), false)
 112  
                 || shouldRenderResponse(facesContext, executor.getPhase(), false)) {
 113  
             // since this phase is completed we don't need to return right away even if the response is completed
 114  0
             skipFurtherProcessing = true;
 115  
         }
 116  
 
 117  0
         if (!skipFurtherProcessing && log.isTraceEnabled()) {
 118  0
             log.trace("exiting " + executor.getPhase() + " in " + LifecycleImpl.class.getName());
 119  
         }
 120  
 
 121  0
         return skipFurtherProcessing;
 122  
     }
 123  
 
 124  
     public void render(FacesContext facesContext) throws FacesException {
 125  
         // if the response is complete we should not be invoking the phase listeners
 126  0
         if(isResponseComplete(facesContext, renderExecutor.getPhase(), true)) {
 127  0
             return;
 128  
         }
 129  0
         if (log.isTraceEnabled()) log.trace("entering " + renderExecutor.getPhase() + " in " + LifecycleImpl.class.getName());
 130  
 
 131  0
         PhaseListenerManager phaseListenerMgr = new PhaseListenerManager(this, facesContext, getPhaseListeners());
 132  
 
 133  
         try {
 134  0
             phaseListenerMgr.informPhaseListenersBefore(renderExecutor.getPhase());
 135  
             // also possible that one of the listeners completed the response
 136  0
             if(isResponseComplete(facesContext, renderExecutor.getPhase(), true)) {
 137  
                 return;
 138  
             }
 139  
 
 140  0
             renderExecutor.execute(facesContext);
 141  
         } finally {
 142  0
             phaseListenerMgr.informPhaseListenersAfter(renderExecutor.getPhase());
 143  0
         }
 144  
 
 145  0
         if (log.isTraceEnabled()) {
 146  
             //Note: DebugUtils Logger must also be in trace level
 147  0
             DebugUtils.traceView("View after rendering");
 148  
         }
 149  
 
 150  0
         if (log.isTraceEnabled()) {
 151  0
             log.trace("exiting " + renderExecutor.getPhase() + " in " + LifecycleImpl.class.getName());
 152  
         }
 153  0
     }
 154  
 
 155  
     private boolean isResponseComplete(FacesContext facesContext, PhaseId phase, boolean before) {
 156  0
         boolean flag = false;
 157  0
         if (facesContext.getResponseComplete()) {
 158  0
             if (log.isDebugEnabled()) {
 159  0
                 log.debug("exiting from lifecycle.execute in " + phase
 160  
                         + " because getResponseComplete is true from one of the " +
 161  
                         (before ? "before" : "after") + " listeners");
 162  
             }
 163  0
             flag = true;
 164  
         }
 165  0
         return flag;
 166  
     }
 167  
 
 168  
     private boolean shouldRenderResponse(FacesContext facesContext, PhaseId phase, boolean before) {
 169  0
             boolean flag = false;
 170  0
         if (facesContext.getRenderResponse()) {
 171  0
             if (log.isDebugEnabled()) {
 172  0
                 log.debug("exiting from lifecycle.execute in " + phase
 173  
                         + " because getRenderResponse is true from one of the " +
 174  
                         (before ? "before" : "after") + " listeners");
 175  
             }
 176  0
             flag = true;
 177  
         }
 178  0
         return flag;
 179  
     }
 180  
 
 181  
     public void addPhaseListener(PhaseListener phaseListener) {
 182  0
         if (phaseListener == null) {
 183  0
             throw new NullPointerException("PhaseListener must not be null.");
 184  
         }
 185  0
         synchronized (_phaseListenerList) {
 186  0
             _phaseListenerList.add(phaseListener);
 187  0
             _phaseListenerArray = null; // reset lazy cache array
 188  0
         }
 189  0
     }
 190  
 
 191  
     public void removePhaseListener(PhaseListener phaseListener) {
 192  0
         if (phaseListener == null) {
 193  0
             throw new NullPointerException("PhaseListener must not be null.");
 194  
         }
 195  0
         synchronized (_phaseListenerList) {
 196  0
             _phaseListenerList.remove(phaseListener);
 197  0
             _phaseListenerArray = null; // reset lazy cache array
 198  0
         }
 199  0
     }
 200  
 
 201  
     public PhaseListener[] getPhaseListeners() {
 202  0
         synchronized (_phaseListenerList) {
 203  
             // (re)build lazy cache array if necessary
 204  0
             if (_phaseListenerArray == null) {
 205  0
                 _phaseListenerArray = _phaseListenerList.toArray(new PhaseListener[_phaseListenerList
 206  
                         .size()]);
 207  
             }
 208  0
             return _phaseListenerArray;
 209  0
         }
 210  
     }
 211  
 }