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.custom.scope;
21  
22  import java.io.Serializable;
23  import java.util.Collections;
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.el.ValueBinding;
30  
31  /**
32   * A central sessions holder, which stores
33   * the triggered scopes and also does
34   * the scope synching at the end
35   *
36   * @author Werner Punz werpu@gmx.at
37   *
38   */
39  public class ScopeHolder implements Serializable
40  {
41  
42      /**
43       *
44       */
45      private static final long serialVersionUID = 2340601728913516991L;
46      /*dunno of a log(n) map is suitable here, after all we have only a handful of scopes*/
47      Map scopeMap = Collections.synchronizedMap(new TreeMap());
48      Map oldScopes = Collections.synchronizedMap(new TreeMap());
49  
50      /**
51       * fetches an old scope from the scope map
52       *
53       * @param key
54       * @return Object old scope
55       */
56      public Object restoreScopeEntry(String key)
57      {
58          Object theentry = oldScopes.get(key);
59          return theentry;
60      }
61  
62      /**
63       * note we use the global session internally not the portlet one, if you use
64       * portlets please-keep that in mind with your scope names
65       *
66       * @param parent
67       *            the parent component
68       * @param context
69       *            the current faces context
70       * @param scopeBindingValue
71       *            the scope value binding
72       */
73      public void saveScopeEntry(UIComponent parent, FacesContext context,
74              ValueBinding scopeBindingValue)
75      {
76  
77          scopeMap.put(scopeBindingValue.getExpressionString(), scopeBindingValue
78                  .getValue(context));
79      }
80  
81      /**
82       * the rendering is done we need to dump the unused scopes
83       *
84       */
85      public void pageRefresh()
86      {
87          oldScopes = scopeMap;
88          scopeMap = Collections.synchronizedMap(new TreeMap());
89      }
90  
91      public void resetScopes()
92      {
93          oldScopes = Collections.synchronizedMap(new TreeMap());
94          scopeMap = Collections.synchronizedMap(new TreeMap());
95      }
96  
97      public void resetScope(String key)
98      {
99          oldScopes.remove(key);
100         scopeMap.remove(key);
101     }
102 }