View Javadoc

1   /*
2    *
3    *   Copyright 2005 The Apache Software Foundation.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.commons.scxml.env.faces;
19  
20  import java.util.Map;
21  
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.commons.scxml.Context;
25  import org.apache.commons.scxml.env.SimpleContext;
26  
27  /***
28   * <p>A Faces Session Context.</p>
29   *
30   * <p>Since the &quot;session map&quot; is obtained from a
31   * <code>FacesContext</code> object using the environment agnostic
32   * <code>getExternalContext()</code>, this <code>Context</code>
33   * will be useful in Servlet as well as Portlet environments.</p>
34   *
35   */
36  public class SessionContext extends SimpleContext {
37  
38      /*** The map of session scoped variables. */
39      private Map sessionMap;
40      /*** Bark if FacesContext is null. */
41      private static final String ERR_HOST_FACES_CTX_NULL =
42          "Host FacesContext cannot be null";
43  
44      /***
45       * Constructor.
46       *
47       * @param fc The current FacesContext
48       */
49      public SessionContext(final FacesContext fc) {
50          this(fc, null);
51      }
52  
53      /***
54       * Constructor.
55       *
56       * @param fc The current FacesContext
57       * @param parent A parent Context, can be null
58       */
59      public SessionContext(final FacesContext fc, final Context parent) {
60          super(parent);
61          if (fc == null) {
62              throw new IllegalArgumentException(ERR_HOST_FACES_CTX_NULL);
63          } else {
64            // only retain the session map
65            this.sessionMap = fc.getExternalContext().getSessionMap();
66          }
67  
68      }
69  
70      /***
71       * Get the value of the given variable in this Context.
72       *
73       * @param name The name of the variable
74       * @return The value (or null)
75       * @see org.apache.commons.scxml.Context#get(java.lang.String)
76       */
77      public Object get(final String name) {
78          Object value = getVars().get(name);
79          if (value == null) {
80              value = sessionMap.get(name);
81          }
82          return value;
83      }
84  
85      /***
86       * Does the given variable exist in this Context.
87       *
88       * @param name The name of the variable
89       * @return boolean true if the variable exists
90       * @see org.apache.commons.scxml.Context#has(java.lang.String)
91       */
92      public boolean has(final String name) {
93          return (sessionMap.containsKey(name) || getVars().containsKey(name));
94      }
95  
96  }
97