View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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      protected HashedSession(HashSessionManager hashSessionManager, HttpServletRequest request)
56      {
57          super(hashSessionManager,request);
58          _hashSessionManager = hashSessionManager;
59      }
60  
61      /* ------------------------------------------------------------- */
62      protected HashedSession(HashSessionManager hashSessionManager, long created, long accessed, String clusterId)
63      {
64          super(hashSessionManager,created, accessed, clusterId);
65          _hashSessionManager = hashSessionManager;
66      }
67  
68      /* ------------------------------------------------------------- */
69      protected void checkValid()
70      {
71          if (_hashSessionManager._idleSavePeriodMs!=0)
72              deIdle();
73          super.checkValid();
74      }
75      
76      /* ------------------------------------------------------------- */
77      @Override
78      public void setMaxInactiveInterval(int secs)
79      {
80          super.setMaxInactiveInterval(secs);
81          if (getMaxInactiveInterval()>0&&(getMaxInactiveInterval()*1000L/10)<_hashSessionManager._scavengePeriodMs)
82              _hashSessionManager.setScavengePeriod((secs+9)/10);
83      }
84  
85      /* ------------------------------------------------------------ */
86      @Override
87      protected void doInvalidate()
88      throws IllegalStateException
89      {
90          super.doInvalidate();
91          
92          // Remove from the disk
93          if (_hashSessionManager._storeDir!=null && getId()!=null)
94          {
95              String id=getId();
96              File f = new File(_hashSessionManager._storeDir, id);
97              f.delete();
98          }
99      }
100 
101     /* ------------------------------------------------------------ */
102     synchronized void save(boolean reactivate)
103     {
104         // Only idle the session if not already idled and no previous save/idle has failed
105         if (!isIdled() && !_saveFailed)
106         {
107             if (LOG.isDebugEnabled())
108                 LOG.debug("Saving {} {}",super.getId(),reactivate);
109 
110             File file = null;
111             FileOutputStream fos = null;
112             
113             try
114             {
115                 file = new File(_hashSessionManager._storeDir, super.getId());
116 
117                 if (file.exists())
118                     file.delete();
119                 file.createNewFile();
120                 fos = new FileOutputStream(file);
121                 willPassivate();
122                 save(fos);
123                 if (reactivate)
124                     didActivate();
125                 else
126                     clearAttributes();
127             }
128             catch (Exception e)
129             {
130                 saveFailed(); // We won't try again for this session
131 
132                 LOG.warn("Problem saving session " + super.getId(), e);
133 
134                 if (fos != null)
135                 {
136                     // Must not leave the file open if the saving failed
137                     IO.close(fos);
138                     // No point keeping the file if we didn't save the whole session
139                     file.delete();
140                     _idled=false; // assume problem was before _values.clear();
141                 }
142             }
143         }
144     }
145     /* ------------------------------------------------------------ */
146     public synchronized void save(OutputStream os)  throws IOException 
147     {
148         DataOutputStream out = new DataOutputStream(os);
149         out.writeUTF(getClusterId());
150         out.writeUTF(getNodeId());
151         out.writeLong(getCreationTime());
152         out.writeLong(getAccessed());
153         
154         /* Don't write these out, as they don't make sense to store because they
155          * either they cannot be true or their value will be restored in the 
156          * Session constructor.
157          */
158         //out.writeBoolean(_invalid);
159         //out.writeBoolean(_doInvalidate);
160         //out.writeLong(_maxIdleMs);
161         //out.writeBoolean( _newSession);
162         out.writeInt(getRequests());
163         out.writeInt(getAttributes());
164         ObjectOutputStream oos = new ObjectOutputStream(out);
165         Enumeration<String> e=getAttributeNames();
166         while(e.hasMoreElements())
167         {
168             String key=e.nextElement();
169             oos.writeUTF(key);
170             oos.writeObject(doGet(key));
171         }
172         oos.close();
173     }
174 
175     /* ------------------------------------------------------------ */
176     public synchronized void deIdle()
177     {
178         if (isIdled())
179         {
180             // Access now to prevent race with idling period
181             access(System.currentTimeMillis());
182 
183             if (LOG.isDebugEnabled())
184                 LOG.debug("Deidling " + super.getId());
185 
186             FileInputStream fis = null;
187 
188             try
189             {
190                 File file = new File(_hashSessionManager._storeDir, super.getId());
191                 if (!file.exists() || !file.canRead())
192                     throw new FileNotFoundException(file.getName());
193 
194                 fis = new FileInputStream(file);
195                 _idled = false;
196                 _hashSessionManager.restoreSession(fis, this);
197 
198                 didActivate();
199                 
200                 // If we are doing period saves, then there is no point deleting at this point 
201                 if (_hashSessionManager._savePeriodMs == 0)
202                     file.delete();
203             }
204             catch (Exception e)
205             {
206                 LOG.warn("Problem deidling session " + super.getId(), e);
207                 IO.close(fis);
208                 invalidate();
209             }
210         }
211     }
212 
213     
214     /* ------------------------------------------------------------ */
215     /**
216      * Idle the session to reduce session memory footprint.
217      * 
218      * The session is idled by persisting it, then clearing the session values attribute map and finally setting 
219      * it to an idled state.  
220      */
221     public synchronized void idle()
222     {
223         save(false);
224     }
225     
226     /* ------------------------------------------------------------ */
227     public synchronized boolean isIdled()
228     {
229       return _idled;
230     }
231 
232     /* ------------------------------------------------------------ */
233     public synchronized boolean isSaveFailed()
234     {
235         return _saveFailed;
236     }
237 
238     /* ------------------------------------------------------------ */
239     public synchronized void saveFailed()
240     {
241         _saveFailed = true;
242     }
243 
244 }