1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Enumeration;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import javax.servlet.ServletContext;
28 import org.apache.commons.chain.web.MapEntry;
29
30
31 /***
32 * <p>Private implementation of <code>Map</code> for servlet context
33 * init parameters.</p>
34 *
35 * @author Craig R. McClanahan
36 * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
37 */
38
39 final class ServletInitParamMap implements Map {
40
41
42 public ServletInitParamMap(ServletContext context) {
43 this.context = context;
44 }
45
46
47 private ServletContext context = null;
48
49
50 public void clear() {
51 throw new UnsupportedOperationException();
52 }
53
54
55 public boolean containsKey(Object key) {
56 return (context.getInitParameter(key(key)) != null);
57 }
58
59
60 public boolean containsValue(Object value) {
61 Iterator values = values().iterator();
62 while (values.hasNext()) {
63 if (value.equals(values.next())) {
64 return (true);
65 }
66 }
67 return (false);
68 }
69
70
71 public Set entrySet() {
72 Set set = new HashSet();
73 Enumeration keys = context.getInitParameterNames();
74 String key;
75 while (keys.hasMoreElements()) {
76 key = (String) keys.nextElement();
77 set.add(new MapEntry(key, context.getInitParameter(key), false));
78 }
79 return (set);
80 }
81
82
83 public boolean equals(Object o) {
84 return (context.equals(o));
85 }
86
87
88 public Object get(Object key) {
89 return (context.getInitParameter(key(key)));
90 }
91
92
93 public int hashCode() {
94 return (context.hashCode());
95 }
96
97
98 public boolean isEmpty() {
99 return (size() < 1);
100 }
101
102
103 public Set keySet() {
104 Set set = new HashSet();
105 Enumeration keys = context.getInitParameterNames();
106 while (keys.hasMoreElements()) {
107 set.add(keys.nextElement());
108 }
109 return (set);
110 }
111
112
113 public Object put(Object key, Object value) {
114 throw new UnsupportedOperationException();
115 }
116
117
118 public void putAll(Map map) {
119 throw new UnsupportedOperationException();
120 }
121
122
123 public Object remove(Object key) {
124 throw new UnsupportedOperationException();
125 }
126
127
128 public int size() {
129 int n = 0;
130 Enumeration keys = context.getInitParameterNames();
131 while (keys.hasMoreElements()) {
132 keys.nextElement();
133 n++;
134 }
135 return (n);
136 }
137
138
139 public Collection values() {
140 List list = new ArrayList();
141 Enumeration keys = context.getInitParameterNames();
142 while (keys.hasMoreElements()) {
143 list.add(context.getInitParameter((String) keys.nextElement()));
144 }
145 return (list);
146 }
147
148
149 private String key(Object key) {
150 if (key == null) {
151 throw new IllegalArgumentException();
152 } else if (key instanceof String) {
153 return ((String) key);
154 } else {
155 return (key.toString());
156 }
157 }
158
159
160 }