1 /*
2 * Replica is published under the terms
3 * of the Apache Software License.
4 */
5 package replica.session.support;
6
7 import java.util.Hashtable;
8 import java.util.Random;
9 import java.util.logging.Logger;
10
11 import replica.session.*;
12
13 /***
14 * This is a very simple implementation of a session manager.
15 * More sofisticated implementations would provide other services
16 * like security.
17 *
18 * @author Pedro Costa
19 * @author Helder Silva
20 * @since 19/Jan/2004
21 */
22 public class SimpleSessionManager implements SessionManager {
23
24 static Logger logger = Logger.getLogger(SimpleSessionManager.class.getName());
25
26 private Hashtable sessions = new Hashtable();
27
28 /***
29 * Default constructor.
30 */
31 public SimpleSessionManager() {
32 super();
33 }
34
35 /***
36 * Create a new user session. Returns the user session Id.
37 * The user session Id can be user to get the user session using
38 * {@link #getSession(String)}.
39 *
40 * @param String userId can be null
41 * @param String userPassword can be null
42 * @return the session id of the new session.
43 */
44 public String createSession( String userId, String userPassword){
45
46
47 SessionImpl session = new SessionImpl();
48
49 login( session, userId, userPassword);
50
51 // TODO re-think the creation of the session ID.
52 // In general it should be a unique number each time is created.
53 // It shouldn't repeat old, not stored anymore keys.
54 long time = System.currentTimeMillis();
55 String sessionId = time * new Random( time ).nextDouble() + "_" + time + "_" + session.hashCode();
56
57 sessions.put( sessionId, session );
58
59 return sessionId;
60 }
61
62 public Session getSession( String sessionId ){
63 return (Session)sessions.get( sessionId );
64 }
65
66 public void destroySession( String sessionId ){
67 sessions.remove( sessionId );
68 }
69
70 public void login( Session session, String userId, String userPassword ){
71
72 //TODO for now we simple store the user Id and password in session
73 // no security check is made.
74
75 if( userId != null ){
76 session.setUserId( userId );
77 session.setLoggedIn( true );
78
79 if( userPassword != null )
80 session.setUserPassword( userPassword );
81 }
82 }
83 /* (non-Javadoc)
84 * @see replica.session.SessionManager#createSession(java.lang.String, java.lang.String, java.lang.String)
85 */
86 public void createSession( String sessionId, String userId, String userPassword) {
87
88 SessionImpl session = new SessionImpl();
89
90 login( session, userId, userPassword);
91
92 sessions.put( sessionId, session );
93 }
94
95 }
This page was automatically generated by Maven