1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 "session map" 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
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