1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.caleigo.security;
19
20 import java.util.*;
21
22 /*** Interface for session classes
23 *
24 * @author tobias
25 */
26 public interface ISession
27 {
28 /*** Returns the ID for this session instance.
29 * @return Session ID.
30 */
31 public int getSessionID();
32 /*** Returns the UserInfo corresponding to this session.
33 * @return The <I>UserInfo</I>.
34 */
35 public UserInfo getUserInfo();
36 /*** Returns the created time when this session was instantiated.
37 * @return Time in milliseconds.
38 */
39 public long getCreatedTime();
40 /*** Returns the time when this session was last accessed.
41 * @return Time in millisecond.
42 */
43 public long getLastAccessTime();
44 /*** Updates the time when the session was last accessed.
45 * @param time The time when the session was accessed.
46 */
47 public void updateLastAccessTime();
48 /*** This method is used to check if the session is valid.
49 * @return Returns true if the session is valid.
50 */
51 public boolean isValid();
52 /*** This method is used to retrieve data which is stored for this session
53 * instance.
54 * @param key the key used to identify specific data.
55 * @return The data stored for the key.
56 */
57 public Object getAttribute(String key);
58 /*** This method is used to store session specific data.
59 * @param key The key used to identify the data.
60 * @param attribute The data to be stored.
61 */
62 public void setAttribute(String key, Object attribute);
63
64 public static class DefaultSession implements ISession
65 {
66
67 private int sessionID;
68 private HashMap attributes = new HashMap();
69 private long createdTime;
70 private long lastAccessTime;
71 private UserInfo userInfo;
72
73
74
75 public DefaultSession(UserInfo userInfo, int sessionID)
76 {
77 this.sessionID = sessionID;
78 this.createdTime = System.currentTimeMillis();
79 this.lastAccessTime = createdTime;
80 this.userInfo = userInfo;
81 }
82
83 public Object getAttribute(String key)
84 {
85 if (key != null)
86 return attributes.get(key);
87 else
88 return null;
89 }
90
91 public long getCreatedTime()
92 {
93 return createdTime;
94 }
95
96 public long getLastAccessTime()
97 {
98 return lastAccessTime;
99 }
100
101 public void updateLastAccessTime()
102 {
103 lastAccessTime = System.currentTimeMillis();
104 }
105
106 public int getSessionID()
107 {
108 return sessionID;
109 }
110
111 public UserInfo getUserInfo()
112 {
113 return userInfo;
114 }
115
116 public boolean isValid()
117 {
118 return true;
119 }
120
121 public void setAttribute(String key, Object attribute)
122 {
123 if (key != null)
124 {
125 if (attribute != null)
126 attributes.put(key, attribute);
127 else
128 attributes.remove(key);
129 }
130 }
131 }
132 }