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   * <p/>
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(
60        final Lifecycle lifecycle, final FacesContext facesContext, final PhaseListener[] phaseListeners) {
61      this.lifecycle = lifecycle;
62      this.facesContext = facesContext;
63      this.phaseListeners = phaseListeners;
64    }
65  
66    private boolean isListenerForThisPhase(final PhaseListener phaseListener, final PhaseId phaseId) {
67      final int listenerPhaseId = phaseListener.getPhaseId().getOrdinal();
68      return (listenerPhaseId == PhaseId.ANY_PHASE.getOrdinal()
69          || listenerPhaseId == phaseId.getOrdinal());
70    }
71  
72    void informPhaseListenersBefore(final PhaseId phaseId) {
73      final boolean[] beforePhaseSuccess = new boolean[phaseListeners.length];
74      listenerSuccessMap.put(phaseId, beforePhaseSuccess);
75  
76      if (phaseListeners.length == 0) {
77        return;
78      }
79      final PhaseEvent event = new PhaseEvent(facesContext, phaseId, lifecycle);
80  
81      for (int i = 0; i < phaseListeners.length; i++) {
82        final PhaseListener phaseListener = phaseListeners[i];
83        if (isListenerForThisPhase(phaseListener, phaseId)) {
84          try {
85            phaseListener.beforePhase(event);
86            beforePhaseSuccess[i] = true;
87          } catch (final Exception e) {
88            beforePhaseSuccess[i] = false; // redundant - for clarity
89            LOG.error("Exception in PhaseListener " + phaseId.toString() + " beforePhase.", e);
90            return;
91          }
92        }
93      }
94    }
95  
96    void informPhaseListenersAfter(final PhaseId phaseId) {
97      final boolean[] beforePhaseSuccess = listenerSuccessMap.get(phaseId);
98  
99      if (phaseListeners.length == 0) {
100       return;
101     }
102     final PhaseEvent event = new PhaseEvent(facesContext, phaseId, lifecycle);
103 
104     for (int i = phaseListeners.length - 1; i >= 0; i--) {
105       final PhaseListener phaseListener = phaseListeners[i];
106       if (isListenerForThisPhase(phaseListener, phaseId)
107           && beforePhaseSuccess[i]) {
108         try {
109           phaseListener.afterPhase(event);
110         } catch (final Exception e) {
111           LOG.error("Exception in PhaseListener " + phaseId.toString() + " afterPhase", e);
112         }
113       }
114     }
115 
116   }
117 }