View Javadoc

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;
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  {
35      ModifiableServletConfig(ServletConfig servletConfig) 
36      {
37          this.servletConfig = servletConfig;
38      }
39  
40      public String getInitParameter(String 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 getInitParameterNames() 
53      {
54          Set<String> names = new HashSet<String>();
55          Enumeration 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 getServletContext() 
65      {
66          return servletConfig.getServletContext();
67      }
68  
69      public String getServletName() 
70      {
71          return servletConfig.getServletName();
72      }
73  
74      public void setInitParameter(String name, String value) 
75      {
76          overrides.put(name, value);
77      }
78  
79      private static <E> Enumeration<E> toEnumeration(final Iterator<E> iterator) 
80      {
81          return new Enumeration<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 servletConfig;
96      
97      private final Map<String, String> overrides = synchronizedMap(new HashMap<String, String>());
98  }