View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.scxml.env.faces;
18  
19  import java.util.Map;
20  
21  import javax.faces.context.FacesContext;
22  
23  import org.apache.commons.scxml.Context;
24  import org.apache.commons.scxml.env.SimpleContext;
25  
26  /***
27   * <p>A Faces Session Context.</p>
28   *
29   * <p>Since the &quot;session map&quot; is obtained from a
30   * <code>FacesContext</code> object using the environment agnostic
31   * <code>getExternalContext()</code>, this <code>Context</code>
32   * will be useful in Servlet as well as Portlet environments.</p>
33   *
34   */
35  public class SessionContext extends SimpleContext {
36  
37      /*** Serial version UID. */
38      private static final long serialVersionUID = 1L;
39      /*** The map of session scoped variables. */
40      private Map sessionMap;
41      /*** Bark if FacesContext is null. */
42      private static final String ERR_HOST_FACES_CTX_NULL =
43          "Host FacesContext cannot be null";
44  
45      /***
46       * Constructor.
47       *
48       * @param fc The current FacesContext
49       */
50      public SessionContext(final FacesContext fc) {
51          this(fc, null);
52      }
53  
54      /***
55       * Constructor.
56       *
57       * @param fc The current FacesContext
58       * @param parent A parent Context, can be null
59       */
60      public SessionContext(final FacesContext fc, final Context parent) {
61          super(parent);
62          if (fc == null) {
63              throw new IllegalArgumentException(ERR_HOST_FACES_CTX_NULL);
64          } else {
65            // only retain the session map
66            this.sessionMap = fc.getExternalContext().getSessionMap();
67          }
68  
69      }
70  
71      /***
72       * Get the value of the given variable in this Context.
73       *
74       * @param name The name of the variable
75       * @return The value (or null)
76       * @see org.apache.commons.scxml.Context#get(java.lang.String)
77       */
78      public Object get(final String name) {
79          Object value = getVars().get(name);
80          if (value == null) {
81              value = sessionMap.get(name);
82          }
83          return value;
84      }
85  
86      /***
87       * Does the given variable exist in this Context.
88       *
89       * @param name The name of the variable
90       * @return boolean true if the variable exists
91       * @see org.apache.commons.scxml.Context#has(java.lang.String)
92       */
93      public boolean has(final String name) {
94          return (sessionMap.containsKey(name) || getVars().containsKey(name));
95      }
96  
97  }
98