KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > guice > ModifiableServletConfig


1 /*
2  * Copyright 2007 Tim Peierls
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.directwebremoting.guice;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import static java.util.Collections.synchronizedMap JavaDoc;
25
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc
34 {
35     ModifiableServletConfig(ServletConfig JavaDoc servletConfig)
36     {
37         this.servletConfig = servletConfig;
38     }
39
40     public String JavaDoc getInitParameter(String JavaDoc name)
41     {
42         if (overrides.containsKey(name))
43         {
44             return overrides.get(name);
45         }
46         else
47         {
48             return servletConfig.getInitParameter(name);
49         }
50     }
51
52     public Enumeration JavaDoc getInitParameterNames()
53     {
54         Set JavaDoc<String JavaDoc> names = new HashSet JavaDoc<String JavaDoc>();
55         Enumeration JavaDoc enumeration = servletConfig.getInitParameterNames();
56         while (enumeration.hasMoreElements())
57         {
58             names.add(enumeration.nextElement().toString());
59         }
60         names.addAll(overrides.keySet());
61         return toEnumeration(names.iterator());
62     }
63
64     public ServletContext JavaDoc getServletContext()
65     {
66         return servletConfig.getServletContext();
67     }
68
69     public String JavaDoc getServletName()
70     {
71         return servletConfig.getServletName();
72     }
73
74     public void setInitParameter(String JavaDoc name, String JavaDoc value)
75     {
76         overrides.put(name, value);
77     }
78
79     private static <E> Enumeration JavaDoc<E> toEnumeration(final Iterator JavaDoc<E> iterator)
80     {
81         return new Enumeration JavaDoc<E>()
82         {
83             public boolean hasMoreElements()
84             {
85                 return iterator.hasNext();
86             }
87             
88             public E nextElement()
89             {
90                 return iterator.next();
91             }
92         };
93     }
94     
95     private final ServletConfig JavaDoc servletConfig;
96     
97     private final Map JavaDoc<String JavaDoc, String JavaDoc> overrides = synchronizedMap(new HashMap JavaDoc<String JavaDoc, String JavaDoc>());
98 }
99
Popular Tags