View Javadoc

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