View Javadoc

    1   /*
    2    * $Id: ApplicationMap.java 471756 2006-11-06 15:01:43Z husted $
    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  package org.apache.struts2.dispatcher;
    22
    23  import java.io.Serializable;
    24  import java.util.AbstractMap;
    25  import java.util.Enumeration;
    26  import java.util.HashSet;
    27  import java.util.Map;
    28  import java.util.Set;
    29
    30  import javax.servlet.ServletContext;
    31
    32
    33  /***
    34   * A simple implementation of the {@link java.util.Map} interface to handle a collection of attributes and
    35   * init parameters in a {@link javax.servlet.ServletContext} object. The {@link #entrySet()} method
    36   * enumerates over all servlet context attributes and init parameters and returns a collection of both.
    37   * Note, this will occur lazily - only when the entry set is asked for.
    38   *
    39   */
    40  public class ApplicationMap extends AbstractMap implements Serializable {
    41
    42      private static final long serialVersionUID = 9136809763083228202L;
    43
    44      private ServletContext context;
    45      private Set<Object> entries;
    46
    47
    48      /***
    49       * Creates a new map object given the servlet context.
    50       *
    51       * @param ctx the servlet context
    52       */
    53      public ApplicationMap(ServletContext ctx) {
    54          this.context = ctx;
    55      }
    56
    57
    58      /***
    59       * Removes all entries from the Map and removes all attributes from the servlet context.
    60       */
    61      public void clear() {
    62          entries = null;
    63
    64          Enumeration e = context.getAttributeNames();
    65
    66          while (e.hasMoreElements()) {
    67              context.removeAttribute(e.nextElement().toString());
    68          }
    69      }
    70
    71      /***
    72       * Creates a Set of all servlet context attributes as well as context init parameters.
    73       *
    74       * @return a Set of all servlet context attributes as well as context init parameters.
    75       */
    76      public Set entrySet() {
    77          if (entries == null) {
    78              entries = new HashSet<Object>();
    79
    80              // Add servlet context attributes
    81              Enumeration enumeration = context.getAttributeNames();
    82
    83              while (enumeration.hasMoreElements()) {
    84                  final String key = enumeration.nextElement().toString();
    85                  final Object value = context.getAttribute(key);
    86                  entries.add(new Map.Entry() {
    87                      public boolean equals(Object obj) {
    88                          Map.Entry entry = (Map.Entry) obj;
    89
    90                          return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
    91                      }
    92
    93                      public int hashCode() {
    94                          return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
    95                      }
    96
    97                      public Object getKey() {
    98                          return key;
    99                      }
    100
    101                     public Object getValue() {
    102                         return value;
    103                     }
    104
    105                     public Object setValue(Object obj) {
    106                         context.setAttribute(key.toString(), obj);
    107
    108                         return value;
    109                     }
    110                 });
    111             }
    112
    113             // Add servlet context init params
    114             enumeration = context.getInitParameterNames();
    115
    116             while (enumeration.hasMoreElements()) {
    117                 final String key = enumeration.nextElement().toString();
    118                 final Object value = context.getInitParameter(key);
    119                 entries.add(new Map.Entry() {
    120                     public boolean equals(Object obj) {
    121                         Map.Entry entry = (Map.Entry) obj;
    122
    123                         return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
    124                     }
    125
    126                     public int hashCode() {
    127                         return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
    128                     }
    129
    130                     public Object getKey() {
    131                         return key;
    132                     }
    133
    134                     public Object getValue() {
    135                         return value;
    136                     }
    137
    138                     public Object setValue(Object obj) {
    139                         context.setAttribute(key.toString(), obj);
    140
    141                         return value;
    142                     }
    143                 });
    144             }
    145         }
    146
    147         return entries;
    148     }
    149
    150     /***
    151      * Returns the servlet context attribute or init parameter based on the given key. If the
    152      * entry is not found, <tt>null</tt> is returned.
    153      *
    154      * @param key the entry key.
    155      * @return the servlet context attribute or init parameter or <tt>null</tt> if the entry is not found.
    156      */
    157     public Object get(Object key) {
    158         // Try context attributes first, then init params
    159         // This gives the proper shadowing effects
    160         String keyString = key.toString();
    161         Object value = context.getAttribute(keyString);
    162
    163         return (value == null) ? context.getInitParameter(keyString) : value;
    164     }
    165
    166     /***
    167      * Sets a servlet context attribute given a attribute name and value.
    168      *
    169      * @param key   the name of the attribute.
    170      * @param value the value to set.
    171      * @return the attribute that was just set.
    172      */
    173     public Object put(Object key, Object value) {
    174         entries = null;
    175         context.setAttribute(key.toString(), value);
    176
    177         return get(key);
    178     }
    179
    180     /***
    181      * Removes the specified servlet context attribute.
    182      *
    183      * @param key the attribute to remove.
    184      * @return the entry that was just removed.
    185      */
    186     public Object remove(Object key) {
    187         entries = null;
    188
    189         Object value = get(key);
    190         context.removeAttribute(key.toString());
    191
    192         return value;
    193     }
    194 }