001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import javax.servlet.ServletContext;
008: import java.io.Serializable;
009: import java.util.*;
010:
011: /**
012: * A simple implementation of the {@link java.util.Map} interface to handle a collection of attributes and
013: * init parameters in a {@link javax.servlet.ServletContext} object. The {@link #entrySet()} method
014: * enumerates over all servlet context attributes and init parameters and returns a collection of both.
015: * Note, this will occur lazily - only when the entry set is asked for.
016: *
017: * @author <a href="mailto:rickard@middleware-company.com">Rickard Öberg</a>
018: * @author Bill Lynch (docs)
019: */
020: public class ApplicationMap extends AbstractMap implements Serializable {
021:
022: ServletContext context;
023: Set entries;
024:
025: /**
026: * Creates a new map object given the servlet context.
027: *
028: * @param ctx the servlet context
029: */
030: public ApplicationMap(ServletContext ctx) {
031: this .context = ctx;
032: }
033:
034: /**
035: * Removes all entries from the Map and removes all attributes from the servlet context.
036: */
037: public void clear() {
038: entries = null;
039:
040: Enumeration e = context.getAttributeNames();
041:
042: while (e.hasMoreElements()) {
043: context.removeAttribute(e.nextElement().toString());
044: }
045: }
046:
047: /**
048: * Creates a Set of all servlet context attributes as well as context init parameters.
049: *
050: * @return a Set of all servlet context attributes as well as context init parameters.
051: */
052: public Set entrySet() {
053: if (entries == null) {
054: entries = new HashSet();
055:
056: // Add servlet context attributes
057: Enumeration enumeration = context.getAttributeNames();
058:
059: while (enumeration.hasMoreElements()) {
060: final String key = enumeration.nextElement().toString();
061: final Object value = context.getAttribute(key);
062: entries.add(new Map.Entry() {
063: public boolean equals(Object obj) {
064: Map.Entry entry = (Map.Entry) obj;
065:
066: return ((key == null) ? (entry.getKey() == null)
067: : key.equals(entry.getKey()))
068: && ((value == null) ? (entry.getValue() == null)
069: : value
070: .equals(entry
071: .getValue()));
072: }
073:
074: public int hashCode() {
075: return ((key == null) ? 0 : key.hashCode())
076: ^ ((value == null) ? 0 : value
077: .hashCode());
078: }
079:
080: public Object getKey() {
081: return key;
082: }
083:
084: public Object getValue() {
085: return value;
086: }
087:
088: public Object setValue(Object obj) {
089: context.setAttribute(key.toString(), obj);
090:
091: return value;
092: }
093: });
094: }
095:
096: // Add servlet context init params
097: enumeration = context.getInitParameterNames();
098:
099: while (enumeration.hasMoreElements()) {
100: final String key = enumeration.nextElement().toString();
101: final Object value = context.getInitParameter(key);
102: entries.add(new Map.Entry() {
103: public boolean equals(Object obj) {
104: Map.Entry entry = (Map.Entry) obj;
105:
106: return ((key == null) ? (entry.getKey() == null)
107: : key.equals(entry.getKey()))
108: && ((value == null) ? (entry.getValue() == null)
109: : value
110: .equals(entry
111: .getValue()));
112: }
113:
114: public int hashCode() {
115: return ((key == null) ? 0 : key.hashCode())
116: ^ ((value == null) ? 0 : value
117: .hashCode());
118: }
119:
120: public Object getKey() {
121: return key;
122: }
123:
124: public Object getValue() {
125: return value;
126: }
127:
128: public Object setValue(Object obj) {
129: context.setAttribute(key.toString(), obj);
130:
131: return value;
132: }
133: });
134: }
135: }
136:
137: return entries;
138: }
139:
140: /**
141: * Returns the servlet context attribute or init parameter based on the given key. If the
142: * entry is not found, <tt>null</tt> is returned.
143: *
144: * @param key the entry key.
145: * @return the servlet context attribute or init parameter or <tt>null</tt> if the entry is not found.
146: */
147: public Object get(Object key) {
148: // Try context attributes first, then init params
149: // This gives the proper shadowing effects
150: String keyString = key.toString();
151: Object value = context.getAttribute(keyString);
152:
153: return (value == null) ? context.getInitParameter(keyString)
154: : value;
155: }
156:
157: /**
158: * Sets a servlet context attribute given a attribute name and value.
159: *
160: * @param key the name of the attribute.
161: * @param value the value to set.
162: * @return the attribute that was just set.
163: */
164: public Object put(Object key, Object value) {
165: entries = null;
166: context.setAttribute(key.toString(), value);
167:
168: return get(key);
169: }
170:
171: /**
172: * Removes the specified servlet context attribute.
173: *
174: * @param key the attribute to remove.
175: * @return the entry that was just removed.
176: */
177: public Object remove(Object key) {
178: entries = null;
179:
180: Object value = get(key);
181: context.removeAttribute(key.toString());
182:
183: return value;
184: }
185: }
|