001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet;
006:
007: import java.io.Serializable;
008: import java.util.AbstractMap;
009: import java.util.Enumeration;
010: import java.util.HashSet;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import javax.portlet.PortletContext;
015:
016: /**
017: * Portlet specific {@link java.util.Map} implementation representing the
018: * {@link javax.portlet.PortletContext} of a Portlet.
019: *
020: * @author Nils-Helge Garli
021: */
022: public class PortletApplicationMap extends AbstractMap implements
023: Serializable {
024:
025: PortletContext context;
026:
027: Set entries;
028:
029: /**
030: * Creates a new map object given the {@link PortletContext}.
031: *
032: * @param ctx The portlet context.
033: */
034: public PortletApplicationMap(PortletContext ctx) {
035: this .context = ctx;
036: }
037:
038: /**
039: * Removes all entries from the Map and removes all attributes from the
040: * portlet context.
041: */
042: public void clear() {
043: entries = null;
044:
045: Enumeration e = context.getAttributeNames();
046:
047: while (e.hasMoreElements()) {
048: context.removeAttribute(e.nextElement().toString());
049: }
050: }
051:
052: /**
053: * Creates a Set of all portlet context attributes as well as context init
054: * parameters.
055: *
056: * @return a Set of all portlet context attributes as well as context init
057: * parameters.
058: */
059: public Set entrySet() {
060: if (entries == null) {
061: entries = new HashSet();
062:
063: // Add portlet context attributes
064: Enumeration enumeration = context.getAttributeNames();
065:
066: while (enumeration.hasMoreElements()) {
067: final String key = enumeration.nextElement().toString();
068: final Object value = context.getAttribute(key);
069: entries.add(new Map.Entry() {
070: public boolean equals(Object obj) {
071: Map.Entry entry = (Map.Entry) obj;
072:
073: return ((key == null) ? (entry.getKey() == null)
074: : key.equals(entry.getKey()))
075: && ((value == null) ? (entry.getValue() == null)
076: : value
077: .equals(entry
078: .getValue()));
079: }
080:
081: public int hashCode() {
082: return ((key == null) ? 0 : key.hashCode())
083: ^ ((value == null) ? 0 : value
084: .hashCode());
085: }
086:
087: public Object getKey() {
088: return key;
089: }
090:
091: public Object getValue() {
092: return value;
093: }
094:
095: public Object setValue(Object obj) {
096: context.setAttribute(key.toString(), obj);
097:
098: return value;
099: }
100: });
101: }
102:
103: // Add portlet context init params
104: enumeration = context.getInitParameterNames();
105:
106: while (enumeration.hasMoreElements()) {
107: final String key = enumeration.nextElement().toString();
108: final Object value = context.getInitParameter(key);
109: entries.add(new Map.Entry() {
110: public boolean equals(Object obj) {
111: Map.Entry entry = (Map.Entry) obj;
112:
113: return ((key == null) ? (entry.getKey() == null)
114: : key.equals(entry.getKey()))
115: && ((value == null) ? (entry.getValue() == null)
116: : value
117: .equals(entry
118: .getValue()));
119: }
120:
121: public int hashCode() {
122: return ((key == null) ? 0 : key.hashCode())
123: ^ ((value == null) ? 0 : value
124: .hashCode());
125: }
126:
127: public Object getKey() {
128: return key;
129: }
130:
131: public Object getValue() {
132: return value;
133: }
134:
135: public Object setValue(Object obj) {
136: context.setAttribute(key.toString(), obj);
137:
138: return value;
139: }
140: });
141: }
142: }
143:
144: return entries;
145: }
146:
147: /**
148: * Returns the portlet context attribute or init parameter based on the
149: * given key. If the entry is not found, <tt>null</tt> is returned.
150: *
151: * @param key
152: * the entry key.
153: * @return the portlet context attribute or init parameter or <tt>null</tt>
154: * if the entry is not found.
155: */
156: public Object get(Object key) {
157: // Try context attributes first, then init params
158: // This gives the proper shadowing effects
159: String keyString = key.toString();
160: Object value = context.getAttribute(keyString);
161:
162: return (value == null) ? context.getInitParameter(keyString)
163: : value;
164: }
165:
166: /**
167: * Sets a portlet context attribute given a attribute name and value.
168: *
169: * @param key
170: * the name of the attribute.
171: * @param value
172: * the value to set.
173: * @return the attribute that was just set.
174: */
175: public Object put(Object key, Object value) {
176: entries = null;
177: context.setAttribute(key.toString(), value);
178:
179: return get(key);
180: }
181:
182: /**
183: * Removes the specified portlet context attribute.
184: *
185: * @param key
186: * the attribute to remove.
187: * @return the entry that was just removed.
188: */
189: public Object remove(Object key) {
190: entries = null;
191:
192: Object value = get(key);
193: context.removeAttribute(key.toString());
194:
195: return value;
196: }
197: }
|