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 AbstractSession
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 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();
164 if (fos != null) IO.close(fos);
165 if (file != null) file.delete();
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
182
183
184
185
186
187
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
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
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);
237 invalidate();
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 }