001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.scxml.env.faces;
018
019 import java.util.Map;
020
021 import javax.faces.context.FacesContext;
022
023 import org.apache.commons.scxml.Context;
024 import org.apache.commons.scxml.env.SimpleContext;
025
026 /**
027 * <p>A Faces Session Context.</p>
028 *
029 * <p>Since the "session map" is obtained from a
030 * <code>FacesContext</code> object using the environment agnostic
031 * <code>getExternalContext()</code>, this <code>Context</code>
032 * will be useful in Servlet as well as Portlet environments.</p>
033 *
034 */
035 public class SessionContext extends SimpleContext {
036
037 /** Serial version UID. */
038 private static final long serialVersionUID = 1L;
039 /** The map of session scoped variables. */
040 private Map sessionMap;
041 /** Bark if FacesContext is null. */
042 private static final String ERR_HOST_FACES_CTX_NULL =
043 "Host FacesContext cannot be null";
044
045 /**
046 * Constructor.
047 *
048 * @param fc The current FacesContext
049 */
050 public SessionContext(final FacesContext fc) {
051 this(fc, null);
052 }
053
054 /**
055 * Constructor.
056 *
057 * @param fc The current FacesContext
058 * @param parent A parent Context, can be null
059 */
060 public SessionContext(final FacesContext fc, final Context parent) {
061 super(parent);
062 if (fc == null) {
063 throw new IllegalArgumentException(ERR_HOST_FACES_CTX_NULL);
064 } else {
065 // only retain the session map
066 this.sessionMap = fc.getExternalContext().getSessionMap();
067 }
068
069 }
070
071 /**
072 * Get the value of the given variable in this Context.
073 *
074 * @param name The name of the variable
075 * @return The value (or null)
076 * @see org.apache.commons.scxml.Context#get(java.lang.String)
077 */
078 public Object get(final String name) {
079 Object value = getVars().get(name);
080 if (value == null) {
081 value = sessionMap.get(name);
082 }
083 return value;
084 }
085
086 /**
087 * Does the given variable exist in this Context.
088 *
089 * @param name The name of the variable
090 * @return boolean true if the variable exists
091 * @see org.apache.commons.scxml.Context#has(java.lang.String)
092 */
093 public boolean has(final String name) {
094 return (sessionMap.containsKey(name) || getVars().containsKey(name));
095 }
096
097 }
098