KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > ServletInitParamMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
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: 1.4 $ $Date: 2004/09/10 22:43:20 $
37  */

38
39 final class ServletInitParamMap implements Map JavaDoc {
40
41
42     public ServletInitParamMap(ServletContext JavaDoc context) {
43         this.context = context;
44     }
45
46
47     private ServletContext JavaDoc context = null;
48
49
50     public void clear() {
51         throw new UnsupportedOperationException JavaDoc();
52     }
53
54
55     public boolean containsKey(Object JavaDoc key) {
56         return (context.getInitParameter(key(key)) != null);
57     }
58
59
60     public boolean containsValue(Object JavaDoc value) {
61         Iterator JavaDoc 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 JavaDoc entrySet() {
72         Set JavaDoc set = new HashSet JavaDoc();
73         Enumeration JavaDoc keys = context.getInitParameterNames();
74         String JavaDoc key;
75         while (keys.hasMoreElements()) {
76             key = (String JavaDoc) keys.nextElement();
77             set.add(new MapEntry(key, context.getInitParameter(key), false));
78         }
79         return (set);
80     }
81
82
83     public boolean equals(Object JavaDoc o) {
84         return (context.equals(o));
85     }
86
87
88     public Object JavaDoc get(Object JavaDoc 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 JavaDoc keySet() {
104         Set JavaDoc set = new HashSet JavaDoc();
105         Enumeration JavaDoc keys = context.getInitParameterNames();
106         while (keys.hasMoreElements()) {
107             set.add(keys.nextElement());
108         }
109         return (set);
110     }
111
112
113     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
114         throw new UnsupportedOperationException JavaDoc();
115     }
116
117
118     public void putAll(Map JavaDoc map) {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121
122
123     public Object JavaDoc remove(Object JavaDoc key) {
124         throw new UnsupportedOperationException JavaDoc();
125     }
126
127
128     public int size() {
129         int n = 0;
130         Enumeration JavaDoc keys = context.getInitParameterNames();
131         while (keys.hasMoreElements()) {
132             keys.nextElement();
133             n++;
134         }
135         return (n);
136     }
137
138
139     public Collection JavaDoc values() {
140         List JavaDoc list = new ArrayList JavaDoc();
141         Enumeration JavaDoc keys = context.getInitParameterNames();
142         while (keys.hasMoreElements()) {
143             list.add(context.getInitParameter((String JavaDoc) keys.nextElement()));
144         }
145         return (list);
146     }
147
148
149     private String JavaDoc key(Object JavaDoc key) {
150         if (key == null) {
151             throw new IllegalArgumentException JavaDoc();
152         } else if (key instanceof String JavaDoc) {
153             return ((String JavaDoc) key);
154         } else {
155             return (key.toString());
156         }
157     }
158
159
160 }
161
Popular Tags