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  package org.apache.myfaces.orchestra.flow;
20  
21  import java.util.Map;
22  
23  import javax.faces.context.FacesContext;
24  import javax.faces.event.PhaseEvent;
25  import javax.faces.event.PhaseId;
26  import javax.faces.event.PhaseListener;
27  
28  /**
29   * A simple class to allow code to query what the current phase is; see static method
30   * getCurrentPhase.
31   * <p>
32   * JSF1.2 provides an API for this but JSF1.1 does not.
33   * <p>
34   * Note that this cannot reliably be called from the beforePhase method of another
35   * PhaseListener, as the result depends on the order in which the PhaseListeners are
36   * registered.
37   */
38  public class FlowPhaseTracker implements PhaseListener
39  {
40      private static final long serialVersionUID = 1L;
41      private static final String PHASE_KEY = FlowPhaseTracker.class.getName() + ":phase";
42  
43      public PhaseId getPhaseId()
44      {
45          return PhaseId.ANY_PHASE;
46      }
47  
48      public void beforePhase(PhaseEvent event)
49      {
50          Map<String, Object> reqScope = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
51          reqScope.put(PHASE_KEY, event.getPhaseId());
52      }
53  
54      public void afterPhase(PhaseEvent event)
55      {
56      }
57  
58      public static PhaseId getCurrentPhase()
59      {
60          Map<String, Object> reqScope = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
61          PhaseId phaseId = (PhaseId) reqScope.get(PHASE_KEY);
62          return phaseId;
63      }
64  }