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 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
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 throws Exception
104 {
105
106 if (!isIdled() && !_saveFailed)
107 {
108 if (LOG.isDebugEnabled())
109 LOG.debug("Saving {} {}",super.getId(),reactivate);
110
111 File file = null;
112 FileOutputStream fos = null;
113
114 try
115 {
116 file = new File(_hashSessionManager._storeDir, super.getId());
117
118 if (file.exists())
119 file.delete();
120 file.createNewFile();
121 fos = new FileOutputStream(file);
122 willPassivate();
123 save(fos);
124 if (reactivate)
125 didActivate();
126 else
127 clearAttributes();
128 }
129 catch (Exception e)
130 {
131 saveFailed();
132 if (fos != null)
133 {
134
135 IO.close(fos);
136
137 file.delete();
138 throw e;
139 }
140 }
141 }
142 }
143
144 public synchronized void save(OutputStream os) throws IOException
145 {
146 DataOutputStream out = new DataOutputStream(os);
147 out.writeUTF(getClusterId());
148 out.writeUTF(getNodeId());
149 out.writeLong(getCreationTime());
150 out.writeLong(getAccessed());
151
152
153
154
155
156
157
158
159
160 out.writeInt(getRequests());
161 out.writeInt(getAttributes());
162 ObjectOutputStream oos = new ObjectOutputStream(out);
163 Enumeration<String> e=getAttributeNames();
164 while(e.hasMoreElements())
165 {
166 String key=e.nextElement();
167 oos.writeUTF(key);
168 oos.writeObject(doGet(key));
169 }
170 oos.close();
171 }
172
173
174 public synchronized void deIdle()
175 {
176 if (isIdled())
177 {
178
179 access(System.currentTimeMillis());
180
181 if (LOG.isDebugEnabled())
182 LOG.debug("De-idling " + super.getId());
183
184 FileInputStream fis = null;
185
186 try
187 {
188 File file = new File(_hashSessionManager._storeDir, super.getId());
189 if (!file.exists() || !file.canRead())
190 throw new FileNotFoundException(file.getName());
191
192 fis = new FileInputStream(file);
193 _idled = false;
194 _hashSessionManager.restoreSession(fis, this);
195
196 didActivate();
197
198
199 if (_hashSessionManager._savePeriodMs == 0)
200 file.delete();
201 }
202 catch (Exception e)
203 {
204 LOG.warn("Problem de-idling session " + super.getId(), e);
205 IO.close(fis);
206 invalidate();
207 }
208 }
209 }
210
211
212
213
214
215
216
217
218
219 public synchronized void idle()
220 throws Exception
221 {
222 save(false);
223 _idled = true;
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 }