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