]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/session/HashedSession.java
Merge "upd: remove 'browser install'"
[gigi.git] / lib / jetty / org / eclipse / jetty / server / session / HashedSession.java
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.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 MemSession
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         if (!_saveFailed && _hashSessionManager._storeDir != null)
149         {
150             file = new File(_hashSessionManager._storeDir, super.getId());
151             if (file.exists())
152             {
153                 file.delete();
154             }
155
156             try(FileOutputStream fos = new FileOutputStream(file,false))
157             {
158                 save(fos);
159             }
160             catch (Exception e)
161             {
162                 saveFailed(); // We won't try again for this session
163                 if (file != null) 
164                     file.delete(); // No point keeping the file if we didn't save the whole session
165                 throw e;             
166             }
167         }
168     }
169     
170     
171     /* ------------------------------------------------------------ */
172     public synchronized void save(OutputStream os)  throws IOException
173     {
174         DataOutputStream out = new DataOutputStream(os);
175         out.writeUTF(getClusterId());
176         out.writeUTF(getNodeId());
177         out.writeLong(getCreationTime());
178         out.writeLong(getAccessed());
179
180         /* Don't write these out, as they don't make sense to store because they
181          * either they cannot be true or their value will be restored in the
182          * Session constructor.
183          */
184         //out.writeBoolean(_invalid);
185         //out.writeBoolean(_doInvalidate);
186         //out.writeBoolean( _newSession);
187         out.writeInt(getRequests());
188         out.writeInt(getAttributes());
189         ObjectOutputStream oos = new ObjectOutputStream(out);
190         Enumeration<String> e=getAttributeNames();
191         while(e.hasMoreElements())
192         {
193             String key=e.nextElement();
194             oos.writeUTF(key);
195             oos.writeObject(doGet(key));
196         }
197         
198         out.writeInt(getMaxInactiveInterval());
199     }
200
201     /* ------------------------------------------------------------ */
202     public synchronized void deIdle()
203     {
204         if (isIdled() && !_deIdleFailed)
205         {
206             // Access now to prevent race with idling period
207             access(System.currentTimeMillis());
208
209             if (LOG.isDebugEnabled())
210                 LOG.debug("De-idling " + super.getId());
211
212             FileInputStream fis = null;
213
214             try
215             {
216                 File file = new File(_hashSessionManager._storeDir, super.getId());
217                 if (!file.exists() || !file.canRead())
218                     throw new FileNotFoundException(file.getName());
219
220                 fis = new FileInputStream(file);
221                 _idled = false;
222                 _hashSessionManager.restoreSession(fis, this);
223                 IO.close(fis); 
224                 
225                 didActivate();
226
227                 // If we are doing period saves, then there is no point deleting at this point 
228                 if (_hashSessionManager._savePeriodMs == 0)
229                     file.delete();
230             }
231             catch (Exception e)
232             {
233                 deIdleFailed();
234                 LOG.warn("Problem de-idling session " + super.getId(), e);
235                 if (fis != null) IO.close(fis);//Must ensure closed before invalidate
236                 invalidate();
237             }
238         }
239     }
240
241
242     /* ------------------------------------------------------------ */
243     /**
244      * Idle the session to reduce session memory footprint.
245      *
246      * The session is idled by persisting it, then clearing the session values attribute map and finally setting
247      * it to an idled state.
248      */
249     public synchronized void idle()
250     throws Exception
251     {
252         save(false);
253         _idled = true;
254     }
255
256     /* ------------------------------------------------------------ */
257     public synchronized boolean isIdled()
258     {
259       return _idled;
260     }
261
262     /* ------------------------------------------------------------ */
263     public synchronized boolean isSaveFailed()
264     {
265         return _saveFailed;
266     }
267
268     /* ------------------------------------------------------------ */
269     public synchronized void saveFailed()
270     {
271         _saveFailed = true;
272     }
273
274     /* ------------------------------------------------------------ */
275     public synchronized void deIdleFailed()
276     {
277         _deIdleFailed = true;
278     }
279     
280     /* ------------------------------------------------------------ */
281     public synchronized boolean isDeIdleFailed()
282     {
283         return _deIdleFailed;
284     }
285 }