1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45 private transient boolean _idled = false;
46
47
48
49
50
51
52 private transient boolean _saveFailed = false;
53
54
55
56
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
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
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;
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();
163 if (file != null)
164 file.delete();
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
181
182
183
184
185
186
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
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
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);
236 invalidate();
237 }
238 }
239 }
240
241
242
243
244
245
246
247
248
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 }