001: /*
002: * $Id: AttributeMap.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.servlet.jsp.PageContext;
029:
030: import org.apache.struts2.ServletActionContext;
031:
032: /**
033: * A Map that holds 4 levels of scope.
034: * <p/>
035: * The scopes are the ones known in the web world.:
036: * <ul>
037: * <li>Page scope</li>
038: * <li>Request scope</li>
039: * <li>Session scope</li>
040: * <li>Application scope</li>
041: * </ul>
042: * A object is searched in the order above, starting from page and ending at application scope.
043: *
044: */
045: public class AttributeMap implements Map {
046:
047: protected static final String UNSUPPORTED = "method makes no sense for a simplified map";
048:
049: Map context;
050:
051: public AttributeMap(Map context) {
052: this .context = context;
053: }
054:
055: public boolean isEmpty() {
056: throw new UnsupportedOperationException(UNSUPPORTED);
057: }
058:
059: public void clear() {
060: throw new UnsupportedOperationException(UNSUPPORTED);
061: }
062:
063: public boolean containsKey(Object key) {
064: return (get(key) != null);
065: }
066:
067: public boolean containsValue(Object value) {
068: throw new UnsupportedOperationException(UNSUPPORTED);
069: }
070:
071: public Set entrySet() {
072: return Collections.EMPTY_SET;
073: }
074:
075: public Object get(Object key) {
076: PageContext pc = getPageContext();
077:
078: if (pc == null) {
079: Map request = (Map) context.get("request");
080: Map session = (Map) context.get("session");
081: Map application = (Map) context.get("application");
082:
083: if ((request != null) && (request.get(key) != null)) {
084: return request.get(key);
085: } else if ((session != null) && (session.get(key) != null)) {
086: return session.get(key);
087: } else if ((application != null)
088: && (application.get(key) != null)) {
089: return application.get(key);
090: }
091: } else {
092: try {
093: return pc.findAttribute(key.toString());
094: } catch (NullPointerException npe) {
095: return null;
096: }
097: }
098:
099: return null;
100: }
101:
102: public Set keySet() {
103: return Collections.EMPTY_SET;
104: }
105:
106: public Object put(Object key, Object value) {
107: PageContext pc = getPageContext();
108: if (pc != null) {
109: pc.setAttribute(key.toString(), value);
110: }
111:
112: return null;
113: }
114:
115: public void putAll(Map t) {
116: throw new UnsupportedOperationException(UNSUPPORTED);
117: }
118:
119: public Object remove(Object key) {
120: throw new UnsupportedOperationException(UNSUPPORTED);
121: }
122:
123: public int size() {
124: throw new UnsupportedOperationException(UNSUPPORTED);
125: }
126:
127: public Collection values() {
128: return Collections.EMPTY_SET;
129: }
130:
131: private PageContext getPageContext() {
132: return (PageContext) context
133: .get(ServletActionContext.PAGE_CONTEXT);
134: }
135: }
|