01: /*
02: * Copyright 2007 Tim Peierls
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.directwebremoting.guice;
17:
18: import java.util.Enumeration;
19: import java.util.HashMap;
20: import java.util.HashSet;
21: import java.util.Iterator;
22: import java.util.Map;
23: import java.util.Set;
24: import static java.util.Collections.synchronizedMap;
25:
26: import javax.servlet.ServletConfig;
27: import javax.servlet.ServletContext;
28:
29: /**
30: * Wraps an existing ServletConfig, allowing changes to be made to the existing initParams.
31: * @author Tim Peierls [tim at peierls dot net]
32: */
33: class ModifiableServletConfig implements ServletConfig {
34: ModifiableServletConfig(ServletConfig servletConfig) {
35: this .servletConfig = servletConfig;
36: }
37:
38: public String getInitParameter(String name) {
39: if (overrides.containsKey(name)) {
40: return overrides.get(name);
41: } else {
42: return servletConfig.getInitParameter(name);
43: }
44: }
45:
46: public Enumeration<String> getInitParameterNames() {
47: Set<String> names = new HashSet<String>();
48: @SuppressWarnings("unchecked")
49: Enumeration<String> enumeration = servletConfig
50: .getInitParameterNames();
51: while (enumeration.hasMoreElements()) {
52: names.add(enumeration.nextElement());
53: }
54: names.addAll(overrides.keySet());
55: return toEnumeration(names.iterator());
56: }
57:
58: public ServletContext getServletContext() {
59: return servletConfig.getServletContext();
60: }
61:
62: public String getServletName() {
63: return servletConfig.getServletName();
64: }
65:
66: public void setInitParameter(String name, String value) {
67: overrides.put(name, value);
68: }
69:
70: private static <E> Enumeration<E> toEnumeration(
71: final Iterator<E> iterator) {
72: return new Enumeration<E>() {
73: public boolean hasMoreElements() {
74: return iterator.hasNext();
75: }
76:
77: public E nextElement() {
78: return iterator.next();
79: }
80: };
81: }
82:
83: private final ServletConfig servletConfig;
84:
85: private final Map<String, String> overrides = synchronizedMap(new HashMap<String, String>());
86: }
|