1 package org.eclipse.jetty.server.session;
2
3 import java.io.DataOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.ObjectOutputStream;
10 import java.io.OutputStream;
11 import java.util.Enumeration;
12
13 import javax.servlet.http.HttpServletRequest;
14
15 import org.eclipse.jetty.util.IO;
16 import org.eclipse.jetty.util.log.Log;
17 import org.eclipse.jetty.util.log.Logger;
18
19 public class HashedSession extends AbstractSession
20 {
21 private static final Logger LOG = Log.getLogger(HashedSession.class);
22
23 private final HashSessionManager _hashSessionManager;
24
25
26
27 private transient boolean _idled = false;
28
29
30
31
32
33
34 private transient boolean _saveFailed = false;
35
36
37 protected HashedSession(HashSessionManager hashSessionManager, HttpServletRequest request)
38 {
39 super(hashSessionManager,request);
40 _hashSessionManager = hashSessionManager;
41 }
42
43
44 protected HashedSession(HashSessionManager hashSessionManager, long created, long accessed, String clusterId)
45 {
46 super(hashSessionManager,created, accessed, clusterId);
47 _hashSessionManager = hashSessionManager;
48 }
49
50
51 protected void checkValid()
52 {
53 if (_hashSessionManager._idleSavePeriodMs!=0)
54 deIdle();
55 super.checkValid();
56 }
57
58
59 @Override
60 public void setMaxInactiveInterval(int secs)
61 {
62 super.setMaxInactiveInterval(secs);
63 if (getMaxInactiveInterval()>0&&(getMaxInactiveInterval()*1000/10)<_hashSessionManager._scavengePeriodMs)
64 _hashSessionManager.setScavengePeriod((secs+9)/10);
65 }
66
67
68 @Override
69 protected void doInvalidate()
70 throws IllegalStateException
71 {
72 super.doInvalidate();
73
74
75 if (_hashSessionManager._storeDir!=null && getId()!=null)
76 {
77 String id=getId();
78 File f = new File(_hashSessionManager._storeDir, id);
79 f.delete();
80 }
81 }
82
83
84 synchronized void save(boolean reactivate)
85 {
86
87 if (!isIdled() && !_saveFailed)
88 {
89 if (LOG.isDebugEnabled())
90 LOG.debug("Saving {} {}",super.getId(),reactivate);
91
92 File file = null;
93 FileOutputStream fos = null;
94
95 try
96 {
97 file = new File(_hashSessionManager._storeDir, super.getId());
98
99 if (file.exists())
100 file.delete();
101 file.createNewFile();
102 fos = new FileOutputStream(file);
103 willPassivate();
104 save(fos);
105 if (reactivate)
106 didActivate();
107 else
108 clearAttributes();
109 }
110 catch (Exception e)
111 {
112 saveFailed();
113
114 LOG.warn("Problem saving session " + super.getId(), e);
115
116 if (fos != null)
117 {
118
119 IO.close(fos);
120
121 file.delete();
122 _idled=false;
123 }
124 }
125 }
126 }
127
128 public synchronized void save(OutputStream os) throws IOException
129 {
130 DataOutputStream out = new DataOutputStream(os);
131 out.writeUTF(getClusterId());
132 out.writeUTF(getNodeId());
133 out.writeLong(getCreationTime());
134 out.writeLong(getAccessed());
135
136
137
138
139
140
141
142
143
144 out.writeInt(getRequests());
145 out.writeInt(getAttributes());
146 ObjectOutputStream oos = new ObjectOutputStream(out);
147 Enumeration<String> e=getAttributeNames();
148 while(e.hasMoreElements())
149 {
150 String key=e.nextElement();
151 oos.writeUTF(key);
152 oos.writeObject(doGet(key));
153 }
154 oos.close();
155 }
156
157
158 public synchronized void deIdle()
159 {
160 if (isIdled())
161 {
162
163 access(System.currentTimeMillis());
164
165
166 if (LOG.isDebugEnabled())
167 {
168 LOG.debug("Deidling " + super.getId());
169 }
170
171 FileInputStream fis = null;
172
173 try
174 {
175 File file = new File(_hashSessionManager._storeDir, super.getId());
176 if (!file.exists() || !file.canRead())
177 throw new FileNotFoundException(file.getName());
178
179 fis = new FileInputStream(file);
180 _idled = false;
181 _hashSessionManager.restoreSession(fis, this);
182
183 didActivate();
184
185
186 if (_hashSessionManager._savePeriodMs == 0)
187 file.delete();
188 }
189 catch (Exception e)
190 {
191 LOG.warn("Problem deidling session " + super.getId(), e);
192 IO.close(fis);
193 invalidate();
194 }
195 }
196 }
197
198
199
200
201
202
203
204
205
206 public synchronized void idle()
207 {
208 save(false);
209 }
210
211
212 public synchronized boolean isIdled()
213 {
214 return _idled;
215 }
216
217
218 public synchronized boolean isSaveFailed()
219 {
220 return _saveFailed;
221 }
222
223
224 public synchronized void saveFailed()
225 {
226 _saveFailed = true;
227 }
228
229 }