View Javadoc

1   /*
2    * $Id: ApplicationMap.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dispatcher;
23  
24  import java.io.Serializable;
25  import java.util.AbstractMap;
26  import java.util.Enumeration;
27  import java.util.HashSet;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import javax.servlet.ServletContext;
32  
33  
34  /***
35   * A simple implementation of the {@link java.util.Map} interface to handle a collection of attributes and
36   * init parameters in a {@link javax.servlet.ServletContext} object. The {@link #entrySet()} method
37   * enumerates over all servlet context attributes and init parameters and returns a collection of both.
38   * Note, this will occur lazily - only when the entry set is asked for.
39   *
40   */
41  public class ApplicationMap extends AbstractMap implements Serializable {
42  
43      private static final long serialVersionUID = 9136809763083228202L;
44  
45      private ServletContext context;
46      private Set<Object> entries;
47  
48  
49      /***
50       * Creates a new map object given the servlet context.
51       *
52       * @param ctx the servlet context
53       */
54      public ApplicationMap(ServletContext ctx) {
55          this.context = ctx;
56      }
57  
58  
59      /***
60       * Removes all entries from the Map and removes all attributes from the servlet context.
61       */
62      public void clear() {
63          entries = null;
64  
65          Enumeration e = context.getAttributeNames();
66  
67          while (e.hasMoreElements()) {
68              context.removeAttribute(e.nextElement().toString());
69          }
70      }
71  
72      /***
73       * Creates a Set of all servlet context attributes as well as context init parameters.
74       *
75       * @return a Set of all servlet context attributes as well as context init parameters.
76       */
77      public Set entrySet() {
78          if (entries == null) {
79              entries = new HashSet<Object>();
80  
81              // Add servlet context attributes
82              Enumeration enumeration = context.getAttributeNames();
83  
84              while (enumeration.hasMoreElements()) {
85                  final String key = enumeration.nextElement().toString();
86                  final Object value = context.getAttribute(key);
87                  entries.add(new Map.Entry() {
88                      public boolean equals(Object obj) {
89                          Map.Entry entry = (Map.Entry) obj;
90  
91                          return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
92                      }
93  
94                      public int hashCode() {
95                          return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
96                      }
97  
98                      public Object getKey() {
99                          return key;
100                     }
101 
102                     public Object getValue() {
103                         return value;
104                     }
105 
106                     public Object setValue(Object obj) {
107                         context.setAttribute(key, obj);
108 
109                         return value;
110                     }
111                 });
112             }
113 
114             // Add servlet context init params
115             enumeration = context.getInitParameterNames();
116 
117             while (enumeration.hasMoreElements()) {
118                 final String key = enumeration.nextElement().toString();
119                 final Object value = context.getInitParameter(key);
120                 entries.add(new Map.Entry() {
121                     public boolean equals(Object obj) {
122                         Map.Entry entry = (Map.Entry) obj;
123 
124                         return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
125                     }
126 
127                     public int hashCode() {
128                         return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
129                     }
130 
131                     public Object getKey() {
132                         return key;
133                     }
134 
135                     public Object getValue() {
136                         return value;
137                     }
138 
139                     public Object setValue(Object obj) {
140                         context.setAttribute(key, obj);
141 
142                         return value;
143                     }
144                 });
145             }
146         }
147 
148         return entries;
149     }
150 
151     /***
152      * Returns the servlet context attribute or init parameter based on the given key. If the
153      * entry is not found, <tt>null</tt> is returned.
154      *
155      * @param key the entry key.
156      * @return the servlet context attribute or init parameter or <tt>null</tt> if the entry is not found.
157      */
158     public Object get(Object key) {
159         // Try context attributes first, then init params
160         // This gives the proper shadowing effects
161         String keyString = key.toString();
162         Object value = context.getAttribute(keyString);
163 
164         return (value == null) ? context.getInitParameter(keyString) : value;
165     }
166 
167     /***
168      * Sets a servlet context attribute given a attribute name and value.
169      *
170      * @param key   the name of the attribute.
171      * @param value the value to set.
172      * @return the attribute that was just set.
173      */
174     public Object put(Object key, Object value) {
175         entries = null;
176         context.setAttribute(key.toString(), value);
177 
178         return get(key);
179     }
180 
181     /***
182      * Removes the specified servlet context attribute.
183      *
184      * @param key the attribute to remove.
185      * @return the entry that was just removed.
186      */
187     public Object remove(Object key) {
188         entries = null;
189 
190         Object value = get(key);
191         context.removeAttribute(key.toString());
192 
193         return value;
194     }
195 }