001: /*
002: * $Id: PortletApplicationMap.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet;
022:
023: import java.io.Serializable;
024: import java.util.AbstractMap;
025: import java.util.Enumeration;
026: import java.util.HashSet;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import javax.portlet.PortletContext;
031:
032: /**
033: * Portlet specific {@link java.util.Map} implementation representing the
034: * {@link javax.portlet.PortletContext} of a Portlet.
035: *
036: */
037: public class PortletApplicationMap extends AbstractMap implements
038: Serializable {
039:
040: private static final long serialVersionUID = 2296107511063504414L;
041:
042: private PortletContext context;
043:
044: private Set<Object> entries;
045:
046: /**
047: * Creates a new map object given the {@link PortletContext}.
048: *
049: * @param ctx The portlet context.
050: */
051: public PortletApplicationMap(PortletContext ctx) {
052: this .context = ctx;
053: }
054:
055: /**
056: * Removes all entries from the Map and removes all attributes from the
057: * portlet context.
058: */
059: public void clear() {
060: entries = null;
061:
062: Enumeration e = context.getAttributeNames();
063:
064: while (e.hasMoreElements()) {
065: context.removeAttribute(e.nextElement().toString());
066: }
067: }
068:
069: /**
070: * Creates a Set of all portlet context attributes as well as context init
071: * parameters.
072: *
073: * @return a Set of all portlet context attributes as well as context init
074: * parameters.
075: */
076: public Set entrySet() {
077: if (entries == null) {
078: entries = new HashSet<Object>();
079:
080: // Add portlet context attributes
081: Enumeration enumeration = context.getAttributeNames();
082:
083: while (enumeration.hasMoreElements()) {
084: final String key = enumeration.nextElement().toString();
085: final Object value = context.getAttribute(key);
086: entries.add(new Map.Entry() {
087: public boolean equals(Object obj) {
088: Map.Entry entry = (Map.Entry) obj;
089:
090: return ((key == null) ? (entry.getKey() == null)
091: : key.equals(entry.getKey()))
092: && ((value == null) ? (entry.getValue() == null)
093: : value
094: .equals(entry
095: .getValue()));
096: }
097:
098: public int hashCode() {
099: return ((key == null) ? 0 : key.hashCode())
100: ^ ((value == null) ? 0 : value
101: .hashCode());
102: }
103:
104: public Object getKey() {
105: return key;
106: }
107:
108: public Object getValue() {
109: return value;
110: }
111:
112: public Object setValue(Object obj) {
113: context.setAttribute(key.toString(), obj);
114:
115: return value;
116: }
117: });
118: }
119:
120: // Add portlet context init params
121: enumeration = context.getInitParameterNames();
122:
123: while (enumeration.hasMoreElements()) {
124: final String key = enumeration.nextElement().toString();
125: final Object value = context.getInitParameter(key);
126: entries.add(new Map.Entry() {
127: public boolean equals(Object obj) {
128: Map.Entry entry = (Map.Entry) obj;
129:
130: return ((key == null) ? (entry.getKey() == null)
131: : key.equals(entry.getKey()))
132: && ((value == null) ? (entry.getValue() == null)
133: : value
134: .equals(entry
135: .getValue()));
136: }
137:
138: public int hashCode() {
139: return ((key == null) ? 0 : key.hashCode())
140: ^ ((value == null) ? 0 : value
141: .hashCode());
142: }
143:
144: public Object getKey() {
145: return key;
146: }
147:
148: public Object getValue() {
149: return value;
150: }
151:
152: public Object setValue(Object obj) {
153: context.setAttribute(key.toString(), obj);
154:
155: return value;
156: }
157: });
158: }
159: }
160:
161: return entries;
162: }
163:
164: /**
165: * Returns the portlet context attribute or init parameter based on the
166: * given key. If the entry is not found, <tt>null</tt> is returned.
167: *
168: * @param key
169: * the entry key.
170: * @return the portlet context attribute or init parameter or <tt>null</tt>
171: * if the entry is not found.
172: */
173: public Object get(Object key) {
174: // Try context attributes first, then init params
175: // This gives the proper shadowing effects
176: String keyString = key.toString();
177: Object value = context.getAttribute(keyString);
178:
179: return (value == null) ? context.getInitParameter(keyString)
180: : value;
181: }
182:
183: /**
184: * Sets a portlet context attribute given a attribute name and value.
185: *
186: * @param key
187: * the name of the attribute.
188: * @param value
189: * the value to set.
190: * @return the attribute that was just set.
191: */
192: public Object put(Object key, Object value) {
193: entries = null;
194: context.setAttribute(key.toString(), value);
195:
196: return get(key);
197: }
198:
199: /**
200: * Removes the specified portlet context attribute.
201: *
202: * @param key
203: * the attribute to remove.
204: * @return the entry that was just removed.
205: */
206: public Object remove(Object key) {
207: entries = null;
208:
209: Object value = get(key);
210: context.removeAttribute(key.toString());
211:
212: return value;
213: }
214: }
|