View Javadoc

1   //
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.
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.io.DataOutputStream;
22  import java.io.File;
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;
30  
31  import javax.servlet.http.HttpServletRequest;
32  
33  import org.eclipse.jetty.util.IO;
34  import org.eclipse.jetty.util.log.Log;
35  import org.eclipse.jetty.util.log.Logger;
36  
37  public class HashedSession extends AbstractSession
38  {
39      private static final Logger LOG = Log.getLogger(HashedSession.class);
40  
41      private final HashSessionManager _hashSessionManager;
42  
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;
46  
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;
53      
54      /**
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.
57       */
58      private transient boolean _deIdleFailed = false;
59  
60      /* ------------------------------------------------------------- */
61      protected HashedSession(HashSessionManager hashSessionManager, HttpServletRequest request)
62      {
63          super(hashSessionManager,request);
64          _hashSessionManager = hashSessionManager;
65      }
66  
67      /* ------------------------------------------------------------- */
68      protected HashedSession(HashSessionManager hashSessionManager, long created, long accessed, String clusterId)
69      {
70          super(hashSessionManager,created, accessed, clusterId);
71          _hashSessionManager = hashSessionManager;
72      }
73  
74      /* ------------------------------------------------------------- */
75      protected void checkValid()
76      {
77          if (!_deIdleFailed && _hashSessionManager._idleSavePeriodMs!=0)
78              deIdle();
79          super.checkValid();
80      }
81  
82      /* ------------------------------------------------------------- */
83      @Override
84      public void setMaxInactiveInterval(int secs)
85      {
86          super.setMaxInactiveInterval(secs);
87          if (getMaxInactiveInterval()>0&&(getMaxInactiveInterval()*1000L/10)<_hashSessionManager._scavengePeriodMs)
88              _hashSessionManager.setScavengePeriod((secs+9)/10);
89      }
90  
91      /* ------------------------------------------------------------ */
92      @Override
93      protected void doInvalidate()
94      throws IllegalStateException
95      {
96          super.doInvalidate();
97          remove();
98      }
99      
100     
101     /* ------------------------------------------------------------ */
102     /**
103      * Remove from the disk
104      */
105     synchronized void remove ()
106     {
107         if (_hashSessionManager._storeDir!=null && getId()!=null)
108         {
109             String id=getId();
110             File f = new File(_hashSessionManager._storeDir, id);
111             f.delete();
112         }
113     }
114 
115     /* ------------------------------------------------------------ */
116     synchronized void save(boolean reactivate)
117     throws Exception
118     {
119         // Only idle the session if not already idled and no previous save/idle has failed
120         if (!isIdled() && !_saveFailed)
121         {
122             if (LOG.isDebugEnabled())
123                 LOG.debug("Saving {} {}",super.getId(),reactivate);
124 
125             try
126             {
127                 willPassivate();
128                 save();
129                 if (reactivate)
130                     didActivate();
131                 else
132                     clearAttributes();
133             }
134             catch (Exception e)
135             {       
136                 LOG.warn("Problem saving session " + super.getId(), e);
137                 _idled=false; // assume problem was before _values.clear();
138             }
139         }
140     }
141     
142     
143     
144     synchronized void save ()
145     throws Exception
146     {   
147         File file = null;
148         FileOutputStream fos = null;
149         if (!_saveFailed && _hashSessionManager._storeDir != null)
150         {
151             try
152             {
153                 file = new File(_hashSessionManager._storeDir, super.getId());
154                 if (file.exists())
155                     file.delete();
156                 file.createNewFile();
157                 fos = new FileOutputStream(file);
158                 save(fos);
159                 IO.close(fos);
160             }
161             catch (Exception e)
162             {
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
166                 throw e;             
167             }
168         }
169     }
170     
171     
172     /* ------------------------------------------------------------ */
173     public synchronized void save(OutputStream os)  throws IOException
174     {
175         DataOutputStream out = new DataOutputStream(os);
176         out.writeUTF(getClusterId());
177         out.writeUTF(getNodeId());
178         out.writeLong(getCreationTime());
179         out.writeLong(getAccessed());
180 
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.
184          */
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())
193         {
194             String key=e.nextElement();
195             oos.writeUTF(key);
196             oos.writeObject(doGet(key));
197         }
198         
199         out.writeInt(getMaxInactiveInterval());
200     }
201 
202     /* ------------------------------------------------------------ */
203     public synchronized void deIdle()
204     {
205         if (isIdled() && !_deIdleFailed)
206         {
207             // Access now to prevent race with idling period
208             access(System.currentTimeMillis());
209 
210             if (LOG.isDebugEnabled())
211                 LOG.debug("De-idling " + super.getId());
212 
213             FileInputStream fis = null;
214 
215             try
216             {
217                 File file = new File(_hashSessionManager._storeDir, super.getId());
218                 if (!file.exists() || !file.canRead())
219                     throw new FileNotFoundException(file.getName());
220 
221                 fis = new FileInputStream(file);
222                 _idled = false;
223                 _hashSessionManager.restoreSession(fis, this);
224                 IO.close(fis); 
225                 
226                 didActivate();
227 
228                 // If we are doing period saves, then there is no point deleting at this point 
229                 if (_hashSessionManager._savePeriodMs == 0)
230                     file.delete();
231             }
232             catch (Exception e)
233             {
234                 deIdleFailed();
235                 LOG.warn("Problem de-idling session " + super.getId(), e);
236                 if (fis != null) IO.close(fis);//Must ensure closed before invalidate
237                 invalidate();
238             }
239         }
240     }
241 
242 
243     /* ------------------------------------------------------------ */
244     /**
245      * Idle the session to reduce session memory footprint.
246      *
247      * The session is idled by persisting it, then clearing the session values attribute map and finally setting
248      * it to an idled state.
249      */
250     public synchronized void idle()
251     throws Exception
252     {
253         save(false);
254         _idled = true;
255     }
256 
257     /* ------------------------------------------------------------ */
258     public synchronized boolean isIdled()
259     {
260       return _idled;
261     }
262 
263     /* ------------------------------------------------------------ */
264     public synchronized boolean isSaveFailed()
265     {
266         return _saveFailed;
267     }
268 
269     /* ------------------------------------------------------------ */
270     public synchronized void saveFailed()
271     {
272         _saveFailed = true;
273     }
274 
275     /* ------------------------------------------------------------ */
276     public synchronized void deIdleFailed()
277     {
278         _deIdleFailed = true;
279     }
280     
281     /* ------------------------------------------------------------ */
282     public synchronized boolean isDeIdleFailed()
283     {
284         return _deIdleFailed;
285     }
286 }