View Javadoc

1   /*
2    * $Id: AttributeMap.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.util;
19  
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.servlet.jsp.PageContext;
26  
27  import org.apache.struts2.ServletActionContext;
28  
29  
30  /***
31   * A Map that holds 4 levels of scope.
32   * <p/>
33   * The scopes are the ones known in the web world.:
34   * <ul>
35   *   <li>Page scope</li>
36   *   <li>Request scope</li>
37   *   <li>Session scope</li>
38   *   <li>Application scope</li>
39   * </ul>
40   * A object is searched in the order above, starting from page and ending at application scope.
41   *
42   */
43  public class AttributeMap implements Map {
44  
45      protected static final String UNSUPPORTED = "method makes no sense for a simplified map";
46  
47  
48      Map context;
49  
50  
51      public AttributeMap(Map context) {
52          this.context = context;
53      }
54  
55  
56      public boolean isEmpty() {
57          throw new UnsupportedOperationException(UNSUPPORTED);
58      }
59  
60      public void clear() {
61          throw new UnsupportedOperationException(UNSUPPORTED);
62      }
63  
64      public boolean containsKey(Object key) {
65          return (get(key) != null);
66      }
67  
68      public boolean containsValue(Object value) {
69          throw new UnsupportedOperationException(UNSUPPORTED);
70      }
71  
72      public Set entrySet() {
73          return Collections.EMPTY_SET;
74      }
75  
76      public Object get(Object key) {
77          PageContext pc = getPageContext();
78  
79          if (pc == null) {
80              Map request = (Map) context.get("request");
81              Map session = (Map) context.get("session");
82              Map application = (Map) context.get("application");
83  
84              if ((request != null) && (request.get(key) != null)) {
85                  return request.get(key);
86              } else if ((session != null) && (session.get(key) != null)) {
87                  return session.get(key);
88              } else if ((application != null) && (application.get(key) != null)) {
89                  return application.get(key);
90              }
91          } else {
92              try{
93                  return pc.findAttribute(key.toString());
94              }catch (NullPointerException npe){
95                  return null;
96              }
97          }
98  
99          return null;
100     }
101 
102     public Set keySet() {
103         return Collections.EMPTY_SET;
104     }
105 
106     public Object put(Object key, Object value) {
107         PageContext pc = getPageContext();
108         if (pc != null) {
109             pc.setAttribute(key.toString(), value);
110         }
111 
112         return null;
113     }
114 
115     public void putAll(Map t) {
116         throw new UnsupportedOperationException(UNSUPPORTED);
117     }
118 
119     public Object remove(Object key) {
120         throw new UnsupportedOperationException(UNSUPPORTED);
121     }
122 
123     public int size() {
124         throw new UnsupportedOperationException(UNSUPPORTED);
125     }
126 
127     public Collection values() {
128         return Collections.EMPTY_SET;
129     }
130 
131     private PageContext getPageContext() {
132         return (PageContext) context.get(ServletActionContext.PAGE_CONTEXT);
133     }
134 }