View Javadoc

    1   /*
    2    * $Id: AttributeMap.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.util;
    22
    23  import java.util.Collection;
    24  import java.util.Collections;
    25  import java.util.Map;
    26  import java.util.Set;
    27
    28  import javax.servlet.jsp.PageContext;
    29
    30  import org.apache.struts2.ServletActionContext;
    31
    32
    33  /***
    34   * A Map that holds 4 levels of scope.
    35   * <p/>
    36   * The scopes are the ones known in the web world.:
    37   * <ul>
    38   *   <li>Page scope</li>
    39   *   <li>Request scope</li>
    40   *   <li>Session scope</li>
    41   *   <li>Application scope</li>
    42   * </ul>
    43   * A object is searched in the order above, starting from page and ending at application scope.
    44   *
    45   */
    46  public class AttributeMap implements Map {
    47
    48      protected static final String UNSUPPORTED = "method makes no sense for a simplified map";
    49
    50
    51      Map context;
    52
    53
    54      public AttributeMap(Map context) {
    55          this.context = context;
    56      }
    57
    58
    59      public boolean isEmpty() {
    60          throw new UnsupportedOperationException(UNSUPPORTED);
    61      }
    62
    63      public void clear() {
    64          throw new UnsupportedOperationException(UNSUPPORTED);
    65      }
    66
    67      public boolean containsKey(Object key) {
    68          return (get(key) != null);
    69      }
    70
    71      public boolean containsValue(Object value) {
    72          throw new UnsupportedOperationException(UNSUPPORTED);
    73      }
    74
    75      public Set entrySet() {
    76          return Collections.EMPTY_SET;
    77      }
    78
    79      public Object get(Object key) {
    80          PageContext pc = getPageContext();
    81
    82          if (pc == null) {
    83              Map request = (Map) context.get("request");
    84              Map session = (Map) context.get("session");
    85              Map application = (Map) context.get("application");
    86
    87              if ((request != null) && (request.get(key) != null)) {
    88                  return request.get(key);
    89              } else if ((session != null) && (session.get(key) != null)) {
    90                  return session.get(key);
    91              } else if ((application != null) && (application.get(key) != null)) {
    92                  return application.get(key);
    93              }
    94          } else {
    95              try{
    96                  return pc.findAttribute(key.toString());
    97              }catch (NullPointerException npe){
    98                  return null;
    99              }
    100         }
    101
    102         return null;
    103     }
    104
    105     public Set keySet() {
    106         return Collections.EMPTY_SET;
    107     }
    108
    109     public Object put(Object key, Object value) {
    110         PageContext pc = getPageContext();
    111         if (pc != null) {
    112             pc.setAttribute(key.toString(), value);
    113         }
    114
    115         return null;
    116     }
    117
    118     public void putAll(Map t) {
    119         throw new UnsupportedOperationException(UNSUPPORTED);
    120     }
    121
    122     public Object remove(Object key) {
    123         throw new UnsupportedOperationException(UNSUPPORTED);
    124     }
    125
    126     public int size() {
    127         throw new UnsupportedOperationException(UNSUPPORTED);
    128     }
    129
    130     public Collection values() {
    131         return Collections.EMPTY_SET;
    132     }
    133
    134     private PageContext getPageContext() {
    135         return (PageContext) context.get(ServletActionContext.PAGE_CONTEXT);
    136     }
    137 }