2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.session;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
37 * A session whose data is kept in memory
39 public class MemSession extends AbstractSession
42 private final Map<String,Object> _attributes=new HashMap<String, Object>();
44 protected MemSession(AbstractSessionManager abstractSessionManager, HttpServletRequest request)
46 super(abstractSessionManager, request);
49 public MemSession(AbstractSessionManager abstractSessionManager, long created, long accessed, String clusterId)
51 super(abstractSessionManager, created, accessed, clusterId);
55 /* ------------------------------------------------------------- */
57 public Map<String,Object> getAttributeMap()
63 /* ------------------------------------------------------------ */
65 public int getAttributes()
70 return _attributes.size();
74 /* ------------------------------------------------------------ */
75 @SuppressWarnings({ "unchecked" })
77 public Enumeration<String> doGetAttributeNames()
79 List<String> names=_attributes==null?Collections.EMPTY_LIST:new ArrayList<String>(_attributes.keySet());
80 return Collections.enumeration(names);
84 /* ------------------------------------------------------------ */
86 public Set<String> getNames()
90 return new HashSet<String>(_attributes.keySet());
95 /* ------------------------------------------------------------- */
97 public void clearAttributes()
99 while (_attributes!=null && _attributes.size()>0)
101 ArrayList<String> keys;
104 keys=new ArrayList<String>(_attributes.keySet());
107 Iterator<String> iter=keys.iterator();
108 while (iter.hasNext())
110 String key=(String)iter.next();
115 value=doPutOrRemove(key,null);
117 unbindValue(key,value);
119 ((AbstractSessionManager)getSessionManager()).doSessionAttributeListeners(this,key,value,null);
122 if (_attributes!=null)
126 /* ------------------------------------------------------------ */
127 public void addAttributes(Map<String,Object> map)
129 _attributes.putAll(map);
132 /* ------------------------------------------------------------ */
134 public Object doPutOrRemove(String name, Object value)
136 return value==null?_attributes.remove(name):_attributes.put(name,value);
139 /* ------------------------------------------------------------ */
141 public Object doGet(String name)
143 return _attributes.get(name);