FileDocCategorySizeDatePackage
PortletSessionMap.javaAPI DocExample5761Mon Jul 23 13:26:40 BST 2007org.apache.struts2.portlet

PortletSessionMap

public class PortletSessionMap extends AbstractMap
A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries. Note, this will occur lazily - only when the entry set is asked for.

Fields Summary
private static final Log
LOG
private javax.portlet.PortletSession
session
private Set
entries
Constructors Summary
public PortletSessionMap(javax.portlet.PortletRequest request)
Creates a new session map given a portlet request.

param
request the portlet request object.


                        
       
        this.session = request.getPortletSession();
        if(LOG.isDebugEnabled()) {
            LOG.debug("Dumping session info: ");
            Enumeration enumeration = session.getAttributeNames();
            while(enumeration.hasMoreElements()) {
                String key = (String)enumeration.nextElement();
                Object val = session.getAttribute(key);
                LOG.debug(key + " = " + val);
            }
        }
    
Methods Summary
public voidclear()

see
java.util.Map#clear()

        synchronized (session) {
            entries = null;
            session.invalidate();
        }
    
public java.util.SetentrySet()

see
java.util.Map#entrySet()

        synchronized (session) {
            if (entries == null) {
                entries = new HashSet<Object>();

                Enumeration enumeration = session.getAttributeNames();

                while (enumeration.hasMoreElements()) {
                    final String key = enumeration.nextElement().toString();
                    final Object value = session.getAttribute(key);
                    entries.add(new Map.Entry() {
                        public boolean equals(Object obj) {
                            Map.Entry entry = (Map.Entry) obj;

                            return ((key == null) ? (entry.getKey() == null)
                                    : key.equals(entry.getKey()))
                                    && ((value == null) ? (entry.getValue() == null)
                                            : value.equals(entry.getValue()));
                        }

                        public int hashCode() {
                            return ((key == null) ? 0 : key.hashCode())
                                    ^ ((value == null) ? 0 : value.hashCode());
                        }

                        public Object getKey() {
                            return key;
                        }

                        public Object getValue() {
                            return value;
                        }

                        public Object setValue(Object obj) {
                            session.setAttribute(key, obj);

                            return value;
                        }
                    });
                }
            }
        }

        return entries;
    
public java.lang.Objectget(java.lang.Object key)
Returns the session attribute associated with the given key or null if it doesn't exist.

param
key the name of the session attribute.
return
the session attribute or null if it doesn't exist.

        synchronized (session) {
            return session.getAttribute(key.toString());
        }
    
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)
Saves an attribute in the session.

param
key the name of the session attribute.
param
value the value to set.
return
the object that was just set.

        synchronized (session) {
            entries = null;
            session.setAttribute(key.toString(), value);

            return get(key);
        }
    
public java.lang.Objectremove(java.lang.Object key)
Removes the specified session attribute.

param
key the name of the attribute to remove.
return
the value that was removed or null if the value was not found (and hence, not removed).

        synchronized (session) {
            entries = null;

            Object value = get(key);
            session.removeAttribute(key.toString());

            return value;
        }