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.shared_orchestra.renderkit;
20  
21  import javax.faces.context.ExternalContext;
22  import javax.faces.context.FacesContext;
23  import java.util.Map;
24  
25  /**
26   * @author Thomas Spiegl
27   */
28  public class ViewSequenceUtils {
29      private static final String SEQUENCE_PARAM = "jsf_sequence";
30  
31      /**
32       * Increments view sequence by 1.
33       *
34       * @param facescontext
35       */
36      public static void nextViewSequence(FacesContext facescontext) {
37          ExternalContext externalContext = facescontext.getExternalContext();
38          Object sessionObj = externalContext.getSession(true);
39          synchronized (sessionObj) // synchronized to increase sequence if multiple requests
40          // are handled at the same time for the session
41          {
42              Map map = externalContext.getSessionMap();
43              Integer sequence = (Integer) map.get(SEQUENCE_PARAM);
44              if (sequence == null || sequence.intValue() == Integer.MAX_VALUE) {
45                  sequence = new Integer(1);
46              }
47              else {
48                  sequence = new Integer(sequence.intValue() + 1);
49              }
50              map.put(SEQUENCE_PARAM, sequence);
51              externalContext.getRequestMap().put(SEQUENCE_PARAM, sequence);
52          }
53      }
54  
55      /**
56       * Returns current view sequence and sets current view sequence to Integer(1) if
57       * current sequence value is NULL.
58       *
59       * @param facescontext
60       * @return
61       */
62      public static Integer getViewSequence(FacesContext facescontext) {
63          Map map = facescontext.getExternalContext().getRequestMap();
64          Integer sequence = (Integer) map.get(SEQUENCE_PARAM);
65          if (sequence == null) {
66              sequence = new Integer(1);
67              map.put(SEQUENCE_PARAM, sequence);
68  
69              synchronized (facescontext.getExternalContext().getSession(true)) {
70                  facescontext.getExternalContext().getSessionMap().put(SEQUENCE_PARAM, sequence);
71              }
72          }
73          return sequence;
74      }
75  
76  
77      /**
78       * Current view sequence, may be NULL!
79       *
80       * @param context
81       * @return
82       */
83      public static Integer getCurrentSequence(FacesContext context) {
84          Map map = context.getExternalContext().getSessionMap();
85          return (Integer) map.get(SEQUENCE_PARAM);
86      }
87  
88  }