|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SimpleSessionManager.java | 25% | 58,8% | 83,3% | 59,3% |
|
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 | 7 |
public SimpleSessionManager() {
|
32 | 7 |
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 | 2 |
public String createSession( String userId, String userPassword){
|
45 |
|
|
46 |
|
|
47 | 2 |
SessionImpl session = new SessionImpl();
|
48 |
|
|
49 | 2 |
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 | 2 |
long time = System.currentTimeMillis();
|
55 | 2 |
String sessionId = time * new Random( time ).nextDouble() + "_" + time + "_" + session.hashCode(); |
56 |
|
|
57 | 2 |
sessions.put( sessionId, session ); |
58 |
|
|
59 | 2 |
return sessionId;
|
60 |
} |
|
61 |
|
|
62 | 2 |
public Session getSession( String sessionId ){
|
63 | 2 |
return (Session)sessions.get( sessionId );
|
64 |
} |
|
65 |
|
|
66 | 2 |
public void destroySession( String sessionId ){ |
67 | 2 |
sessions.remove( sessionId ); |
68 |
} |
|
69 |
|
|
70 | 2 |
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 | 2 |
if( userId != null ){ |
76 | 0 |
session.setUserId( userId ); |
77 | 0 |
session.setLoggedIn( true );
|
78 |
|
|
79 | 0 |
if( userPassword != null ) |
80 | 0 |
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 | 0 |
public void createSession( String sessionId, String userId, String userPassword) { |
87 |
|
|
88 | 0 |
SessionImpl session = new SessionImpl();
|
89 |
|
|
90 | 0 |
login( session, userId, userPassword); |
91 |
|
|
92 | 0 |
sessions.put( sessionId, session ); |
93 |
} |
|
94 |
|
|
95 |
} |
|
96 |
|
|