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