View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.session;
20  
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;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import javax.servlet.http.HttpServletRequest;
32  
33  
34  /**
35   * MemSession
36   *
37   * A session whose data is kept in memory
38   */
39  public class MemSession extends AbstractSession
40  {
41  
42      private final Map<String,Object> _attributes=new HashMap<String, Object>();
43  
44      protected MemSession(AbstractSessionManager abstractSessionManager, HttpServletRequest request)
45      {
46          super(abstractSessionManager, request);
47      }
48  
49      public MemSession(AbstractSessionManager abstractSessionManager, long created, long accessed, String clusterId)
50      {
51          super(abstractSessionManager, created, accessed, clusterId);
52      }
53      
54      
55      /* ------------------------------------------------------------- */
56      @Override
57      public Map<String,Object> getAttributeMap()
58      {
59          return _attributes;
60      }
61     
62  
63      /* ------------------------------------------------------------ */
64      @Override
65      public int getAttributes()
66      {
67          synchronized (this)
68          {
69              checkValid();
70              return _attributes.size();
71          }
72      }
73  
74      /* ------------------------------------------------------------ */
75      @SuppressWarnings({ "unchecked" })
76      @Override
77      public Enumeration<String> doGetAttributeNames()
78      {
79          List<String> names=_attributes==null?Collections.EMPTY_LIST:new ArrayList<String>(_attributes.keySet());
80          return Collections.enumeration(names);
81      }
82  
83      
84      /* ------------------------------------------------------------ */
85      @Override
86      public Set<String> getNames()
87      {
88          synchronized (this)
89          {
90              return new HashSet<String>(_attributes.keySet());
91          }
92      }
93     
94      
95      /* ------------------------------------------------------------- */
96      @Override
97      public void clearAttributes()
98      {
99          while (_attributes!=null && _attributes.size()>0)
100         {
101             ArrayList<String> keys;
102             synchronized(this)
103             {
104                 keys=new ArrayList<String>(_attributes.keySet());
105             }
106 
107             Iterator<String> iter=keys.iterator();
108             while (iter.hasNext())
109             {
110                 String key=(String)iter.next();
111 
112                 Object value;
113                 synchronized(this)
114                 {
115                     value=doPutOrRemove(key,null);
116                 }
117                 unbindValue(key,value);
118 
119                 ((AbstractSessionManager)getSessionManager()).doSessionAttributeListeners(this,key,value,null);
120             }
121         }
122         if (_attributes!=null)
123             _attributes.clear();
124     }
125 
126     /* ------------------------------------------------------------ */
127     public void addAttributes(Map<String,Object> map)
128     {
129         _attributes.putAll(map);
130     }
131     
132     /* ------------------------------------------------------------ */
133     @Override
134     public Object doPutOrRemove(String name, Object value)
135     {
136         return value==null?_attributes.remove(name):_attributes.put(name,value);
137     }
138 
139     /* ------------------------------------------------------------ */
140     @Override
141     public Object doGet(String name)
142     {
143         return _attributes.get(name);
144     }
145     
146 }