Coverage Report - org.apache.myfaces.lifecycle.PhaseListenerManager
 
Classes in this File Line Coverage Branch Coverage Complexity
PhaseListenerManager
0%
0/31
0%
0/14
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  
 
 20  
 package org.apache.myfaces.lifecycle;
 21  
 
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.faces.event.PhaseEvent;
 26  
 import javax.faces.event.PhaseId;
 27  
 import javax.faces.event.PhaseListener;
 28  
 import javax.faces.lifecycle.Lifecycle;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 /**
 33  
  * This class encapsulates the logic used to call PhaseListeners.  It was 
 34  
  * needed because of issue 9 of the JSF 1.2 spec.  See section 11.3 for more
 35  
  * details.
 36  
  *
 37  
  * @author Stan Silvert
 38  
  */
 39  
 class PhaseListenerManager {
 40  
     
 41  0
     private static final Log log = LogFactory.getLog(PhaseListenerManager.class);
 42  
     
 43  
     private Lifecycle lifecycle;
 44  
     private FacesContext facesContext;
 45  
     private PhaseListener[] phaseListeners;
 46  
     
 47  
     // Tracks success in the beforePhase.  Listeners that throw an exception
 48  
     // in beforePhase or were never called because a previous listener threw
 49  
     // an exception should not have its afterPhase called
 50  0
     private Map<PhaseId, boolean[]> listenerSuccessMap = new HashMap<PhaseId, boolean[]>();
 51  
     
 52  
     /** Creates a new instance of PhaseListenerManager */
 53  0
     PhaseListenerManager(Lifecycle lifecycle, FacesContext facesContext, PhaseListener[] phaseListeners) {
 54  0
         this.lifecycle = lifecycle;
 55  0
         this.facesContext = facesContext;
 56  0
         this.phaseListeners = phaseListeners;
 57  0
     }
 58  
     
 59  
     private boolean isListenerForThisPhase(PhaseListener phaseListener, PhaseId phaseId) {
 60  0
         int listenerPhaseId = phaseListener.getPhaseId().getOrdinal();
 61  0
         return (listenerPhaseId == PhaseId.ANY_PHASE.getOrdinal() ||
 62  
                 listenerPhaseId == phaseId.getOrdinal());
 63  
     }
 64  
     
 65  
     void informPhaseListenersBefore(PhaseId phaseId) {
 66  0
         boolean[] beforePhaseSuccess = new boolean[phaseListeners.length];
 67  0
         listenerSuccessMap.put(phaseId, beforePhaseSuccess);
 68  
         
 69  0
         for (int i = 0; i < phaseListeners.length; i++) {
 70  0
             PhaseListener phaseListener = phaseListeners[i];
 71  0
             if (isListenerForThisPhase(phaseListener, phaseId)) {
 72  
                 try {
 73  0
                     phaseListener.beforePhase(new PhaseEvent(facesContext, phaseId, lifecycle));
 74  0
                     beforePhaseSuccess[i] = true;
 75  0
                 } catch (Exception e) {
 76  0
                     beforePhaseSuccess[i] = false; // redundant - for clarity
 77  0
                     log.error("Exception in PhaseListener " + phaseId.toString() + " beforePhase.", e);
 78  0
                     return;
 79  0
                 }
 80  
             }
 81  
         }
 82  0
     }
 83  
 
 84  
     void informPhaseListenersAfter(PhaseId phaseId) {
 85  0
         boolean[] beforePhaseSuccess = listenerSuccessMap.get(phaseId);
 86  
         
 87  0
         for (int i = phaseListeners.length - 1; i >= 0; i--)  {
 88  0
             PhaseListener phaseListener = phaseListeners[i];
 89  0
             if (isListenerForThisPhase(phaseListener, phaseId) 
 90  
                 && beforePhaseSuccess[i]) {
 91  
                 try {
 92  0
                     phaseListener.afterPhase(new PhaseEvent(facesContext, phaseId, lifecycle));
 93  0
                 } catch (Exception e) {
 94  0
                     log.error("Exception in PhaseListener " + phaseId.toString() + " afterPhase", e);
 95  0
                 }
 96  
             }
 97  
         }
 98  
 
 99  0
     }
 100  
 }