View Javadoc

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