01: package org.mortbay.jetty.servlet;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.FileOutputStream;
06: import java.io.IOException;
07: import java.io.ObjectInputStream;
08: import java.io.ObjectOutputStream;
09: import java.io.Serializable;
10: import java.util.Arrays;
11: import java.util.List;
12:
13: import javax.servlet.http.HttpSession;
14:
15: import org.mortbay.jetty.servlet.CacheSessionManager.Store;
16: import org.mortbay.log.Log;
17:
18: public class FileStore implements Store {
19: private String _sessionsDir;
20: private File _sessions;
21:
22: public FileStore(String cacheDirectory) {
23: _sessionsDir = cacheDirectory;
24: }
25:
26: public FileStore() {
27: this (System.getProperty("java.io.tmp", ".")
28: + "/org.mortbay.jetty.sessions");
29: }
30:
31: public void setContext(String contextName) {
32: createSessionDir(_sessionsDir + "/" + contextName);
33: }
34:
35: private void createSessionDir(String path) {
36: _sessions = new File(path);
37: if (_sessions.exists())
38: _sessions.delete();
39: _sessions.mkdir();
40: }
41:
42: public Object get(String id) {
43: File sessionFile = new File(_sessions.getAbsolutePath() + "/"
44: + id);
45: Object session;
46: if (sessionFile.exists()) {
47: try {
48: FileInputStream out = new FileInputStream(sessionFile);
49: ObjectInputStream in = new ObjectInputStream(out);
50: session = in.readObject();
51: in.close();
52: return session;
53: } catch (IOException e) {
54: Log.debug(e);
55: } catch (ClassNotFoundException e) {
56: Log.debug(e);
57: }
58: }
59: return null;
60: }
61:
62: public void add(String id, Serializable session) {
63: FileOutputStream out = null;
64: ObjectOutputStream oos = null;
65:
66: try {
67: out = new FileOutputStream(_sessions.getAbsolutePath()
68: + "/" + ((HttpSession) session).getId());
69: oos = new ObjectOutputStream(out);
70: oos.writeObject(session);
71: oos.close();
72: } catch (IOException e) {
73: e.printStackTrace();
74: }
75: }
76:
77: public void remove(String id) {
78: File sessionFile = new File(_sessions.getAbsolutePath() + id);
79: if (sessionFile.exists()) {
80: sessionFile.delete();
81: }
82: }
83:
84: public List getKeys() {
85: String[] sessionArray = _sessions.list();
86: if (sessionArray != null) {
87: return Arrays.asList(sessionArray);
88: }
89: return null;
90: }
91:
92: }
|