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.io.DataOutputStream;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectOutputStream;
28 import java.io.OutputStream;
29 import java.util.Enumeration;
31 import javax.servlet.http.HttpServletRequest;
33 import org.eclipse.jetty.util.IO;
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.log.Logger;
37 public class HashedSession extends MemSession
39 private static final Logger LOG = Log.getLogger(HashedSession.class);
41 private final HashSessionManager _hashSessionManager;
43 /** Whether the session has been saved because it has been deemed idle;
44 * in which case its attribute map will have been saved and cleared. */
45 private transient boolean _idled = false;
47 /** Whether there has already been an attempt to save this session
48 * which has failed. If there has, there will be no more save attempts
49 * for this session. This is to stop the logs being flooded with errors
50 * due to serialization failures that are most likely caused by user
51 * data stored in the session that is not serializable. */
52 private transient boolean _saveFailed = false;
55 * True if an attempt has been made to de-idle a session and it failed. Once
56 * true, the session will not be attempted to be de-idled again.
58 private transient boolean _deIdleFailed = false;
60 /* ------------------------------------------------------------- */
61 protected HashedSession(HashSessionManager hashSessionManager, HttpServletRequest request)
63 super(hashSessionManager,request);
64 _hashSessionManager = hashSessionManager;
67 /* ------------------------------------------------------------- */
68 protected HashedSession(HashSessionManager hashSessionManager, long created, long accessed, String clusterId)
70 super(hashSessionManager,created, accessed, clusterId);
71 _hashSessionManager = hashSessionManager;
74 /* ------------------------------------------------------------- */
75 protected void checkValid()
77 if (!_deIdleFailed && _hashSessionManager._idleSavePeriodMs!=0)
82 /* ------------------------------------------------------------- */
84 public void setMaxInactiveInterval(int secs)
86 super.setMaxInactiveInterval(secs);
87 if (getMaxInactiveInterval()>0&&(getMaxInactiveInterval()*1000L/10)<_hashSessionManager._scavengePeriodMs)
88 _hashSessionManager.setScavengePeriod((secs+9)/10);
91 /* ------------------------------------------------------------ */
93 protected void doInvalidate()
94 throws IllegalStateException
101 /* ------------------------------------------------------------ */
103 * Remove from the disk
105 synchronized void remove ()
107 if (_hashSessionManager._storeDir!=null && getId()!=null)
110 File f = new File(_hashSessionManager._storeDir, id);
115 /* ------------------------------------------------------------ */
116 synchronized void save(boolean reactivate)
119 // Only idle the session if not already idled and no previous save/idle has failed
120 if (!isIdled() && !_saveFailed)
122 if (LOG.isDebugEnabled())
123 LOG.debug("Saving {} {}",super.getId(),reactivate);
136 LOG.warn("Problem saving session " + super.getId(), e);
137 _idled=false; // assume problem was before _values.clear();
144 synchronized void save ()
148 FileOutputStream fos = null;
149 if (!_saveFailed && _hashSessionManager._storeDir != null)
153 file = new File(_hashSessionManager._storeDir, super.getId());
156 file.createNewFile();
157 fos = new FileOutputStream(file);
163 saveFailed(); // We won't try again for this session
164 if (fos != null) IO.close(fos);
165 if (file != null) file.delete(); // No point keeping the file if we didn't save the whole session
172 /* ------------------------------------------------------------ */
173 public synchronized void save(OutputStream os) throws IOException
175 DataOutputStream out = new DataOutputStream(os);
176 out.writeUTF(getClusterId());
177 out.writeUTF(getNodeId());
178 out.writeLong(getCreationTime());
179 out.writeLong(getAccessed());
181 /* Don't write these out, as they don't make sense to store because they
182 * either they cannot be true or their value will be restored in the
183 * Session constructor.
185 //out.writeBoolean(_invalid);
186 //out.writeBoolean(_doInvalidate);
187 //out.writeBoolean( _newSession);
188 out.writeInt(getRequests());
189 out.writeInt(getAttributes());
190 ObjectOutputStream oos = new ObjectOutputStream(out);
191 Enumeration<String> e=getAttributeNames();
192 while(e.hasMoreElements())
194 String key=e.nextElement();
196 oos.writeObject(doGet(key));
199 out.writeInt(getMaxInactiveInterval());
202 /* ------------------------------------------------------------ */
203 public synchronized void deIdle()
205 if (isIdled() && !_deIdleFailed)
207 // Access now to prevent race with idling period
208 access(System.currentTimeMillis());
210 if (LOG.isDebugEnabled())
211 LOG.debug("De-idling " + super.getId());
213 FileInputStream fis = null;
217 File file = new File(_hashSessionManager._storeDir, super.getId());
218 if (!file.exists() || !file.canRead())
219 throw new FileNotFoundException(file.getName());
221 fis = new FileInputStream(file);
223 _hashSessionManager.restoreSession(fis, this);
228 // If we are doing period saves, then there is no point deleting at this point
229 if (_hashSessionManager._savePeriodMs == 0)
235 LOG.warn("Problem de-idling session " + super.getId(), e);
236 if (fis != null) IO.close(fis);//Must ensure closed before invalidate
243 /* ------------------------------------------------------------ */
245 * Idle the session to reduce session memory footprint.
247 * The session is idled by persisting it, then clearing the session values attribute map and finally setting
248 * it to an idled state.
250 public synchronized void idle()
257 /* ------------------------------------------------------------ */
258 public synchronized boolean isIdled()
263 /* ------------------------------------------------------------ */
264 public synchronized boolean isSaveFailed()
269 /* ------------------------------------------------------------ */
270 public synchronized void saveFailed()
275 /* ------------------------------------------------------------ */
276 public synchronized void deIdleFailed()
278 _deIdleFailed = true;
281 /* ------------------------------------------------------------ */
282 public synchronized boolean isDeIdleFailed()
284 return _deIdleFailed;