001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.servlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import javax.servlet.ServletContext;
027: import org.apache.commons.chain.web.MapEntry;
028:
029: /**
030: * <p>Private implementation of <code>Map</code> for servlet context
031: * init parameters.</p>
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
035: */
036:
037: final class ServletInitParamMap implements Map {
038:
039: public ServletInitParamMap(ServletContext context) {
040: this .context = context;
041: }
042:
043: private ServletContext context = null;
044:
045: public void clear() {
046: throw new UnsupportedOperationException();
047: }
048:
049: public boolean containsKey(Object key) {
050: return (context.getInitParameter(key(key)) != null);
051: }
052:
053: public boolean containsValue(Object value) {
054: Iterator values = values().iterator();
055: while (values.hasNext()) {
056: if (value.equals(values.next())) {
057: return (true);
058: }
059: }
060: return (false);
061: }
062:
063: public Set entrySet() {
064: Set set = new HashSet();
065: Enumeration keys = context.getInitParameterNames();
066: String key;
067: while (keys.hasMoreElements()) {
068: key = (String) keys.nextElement();
069: set.add(new MapEntry(key, context.getInitParameter(key),
070: false));
071: }
072: return (set);
073: }
074:
075: public boolean equals(Object o) {
076: return (context.equals(o));
077: }
078:
079: public Object get(Object key) {
080: return (context.getInitParameter(key(key)));
081: }
082:
083: public int hashCode() {
084: return (context.hashCode());
085: }
086:
087: public boolean isEmpty() {
088: return (size() < 1);
089: }
090:
091: public Set keySet() {
092: Set set = new HashSet();
093: Enumeration keys = context.getInitParameterNames();
094: while (keys.hasMoreElements()) {
095: set.add(keys.nextElement());
096: }
097: return (set);
098: }
099:
100: public Object put(Object key, Object value) {
101: throw new UnsupportedOperationException();
102: }
103:
104: public void putAll(Map map) {
105: throw new UnsupportedOperationException();
106: }
107:
108: public Object remove(Object key) {
109: throw new UnsupportedOperationException();
110: }
111:
112: public int size() {
113: int n = 0;
114: Enumeration keys = context.getInitParameterNames();
115: while (keys.hasMoreElements()) {
116: keys.nextElement();
117: n++;
118: }
119: return (n);
120: }
121:
122: public Collection values() {
123: List list = new ArrayList();
124: Enumeration keys = context.getInitParameterNames();
125: while (keys.hasMoreElements()) {
126: list.add(context.getInitParameter((String) keys
127: .nextElement()));
128: }
129: return (list);
130: }
131:
132: private String key(Object key) {
133: if (key == null) {
134: throw new IllegalArgumentException();
135: } else if (key instanceof String) {
136: return ((String) key);
137: } else {
138: return (key.toString());
139: }
140: }
141:
142: }
|