001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008:
009: import javax.servlet.jsp.PageContext;
010: import java.util.Collection;
011: import java.util.Collections;
012: import java.util.Map;
013: import java.util.Set;
014:
015: /**
016: * A Map that holds 4 levels of scope.
017: * <p/>
018: * The scopes are the ones known in the web world.:
019: * <ul>
020: * <li>Page scope</li>
021: * <li>Request scope</li>
022: * <li>Session scope</li>
023: * <li>Application scope</li>
024: * </ul>
025: * A object is searched in the order above, starting from page and ending at application scope.
026: *
027: * @author plightbo
028: */
029: public class AttributeMap implements Map {
030:
031: protected static final String UNSUPPORTED = "method makes no sense for a simplified map";
032:
033: Map context;
034:
035: public AttributeMap(Map context) {
036: this .context = context;
037: }
038:
039: public boolean isEmpty() {
040: throw new UnsupportedOperationException(UNSUPPORTED);
041: }
042:
043: public void clear() {
044: throw new UnsupportedOperationException(UNSUPPORTED);
045: }
046:
047: public boolean containsKey(Object key) {
048: return (get(key) != null);
049: }
050:
051: public boolean containsValue(Object value) {
052: throw new UnsupportedOperationException(UNSUPPORTED);
053: }
054:
055: public Set entrySet() {
056: return Collections.EMPTY_SET;
057: }
058:
059: public Object get(Object key) {
060: PageContext pc = getPageContext();
061:
062: if (pc == null) {
063: Map request = (Map) context.get("request");
064: Map session = (Map) context.get("session");
065: Map application = (Map) context.get("application");
066:
067: if ((request != null) && (request.get(key) != null)) {
068: return request.get(key);
069: } else if ((session != null) && (session.get(key) != null)) {
070: return session.get(key);
071: } else if ((application != null)
072: && (application.get(key) != null)) {
073: return application.get(key);
074: }
075: } else {
076: try {
077: return pc.findAttribute(key.toString());
078: } catch (NullPointerException npe) {
079: return null;
080: }
081: }
082:
083: return null;
084: }
085:
086: public Set keySet() {
087: return Collections.EMPTY_SET;
088: }
089:
090: public Object put(Object key, Object value) {
091: PageContext pc = getPageContext();
092: if (pc != null) {
093: pc.setAttribute(key.toString(), value);
094: }
095:
096: return null;
097: }
098:
099: public void putAll(Map t) {
100: throw new UnsupportedOperationException(UNSUPPORTED);
101: }
102:
103: public Object remove(Object key) {
104: throw new UnsupportedOperationException(UNSUPPORTED);
105: }
106:
107: public int size() {
108: throw new UnsupportedOperationException(UNSUPPORTED);
109: }
110:
111: public Collection values() {
112: return Collections.EMPTY_SET;
113: }
114:
115: private PageContext getPageContext() {
116: return (PageContext) context
117: .get(ServletActionContext.PAGE_CONTEXT);
118: }
119: }
|