01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 "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 = "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(
94: name));
95: }
96:
97: }
|