2009-04-14 09:55:58 +02:00
|
|
|
package org.gcube.application.framework.core.session;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
public class SessionManager {
|
|
|
|
|
2011-04-12 14:46:24 +02:00
|
|
|
protected static Thread thread = new CleanSessionThread();
|
2009-04-14 09:55:58 +02:00
|
|
|
protected static SessionManager sessionManager = new SessionManager();
|
|
|
|
protected HashMap<String, ASLSession> sessions;
|
|
|
|
|
|
|
|
protected SessionManager() {
|
|
|
|
sessions = new HashMap<String, ASLSession>();
|
2011-04-12 14:46:24 +02:00
|
|
|
thread.setDaemon(true);
|
|
|
|
thread.start();
|
2009-04-14 09:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static SessionManager getInstance() {
|
|
|
|
return sessionManager;
|
|
|
|
}
|
|
|
|
|
2009-04-14 13:43:01 +02:00
|
|
|
public ASLSession getASLSession(String externalSessionID, String username)
|
2009-04-14 09:55:58 +02:00
|
|
|
{
|
|
|
|
ASLSession session = sessions.get(externalSessionID + "_" + username);
|
2011-04-12 14:46:24 +02:00
|
|
|
if(session == null || !session.isValid() || !session.getUsername().equals(username))
|
2009-04-14 09:55:58 +02:00
|
|
|
{
|
|
|
|
session = new ASLSession(externalSessionID, username);
|
|
|
|
sessions.put(externalSessionID + "_" + username, session);
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void finalize() throws Throwable {
|
2011-04-12 14:46:24 +02:00
|
|
|
thread.interrupt();
|
|
|
|
System.out.println(new Date(System.currentTimeMillis()) + " clean thread was interrupted");
|
|
|
|
thread.join();
|
|
|
|
System.out.println(new Date(System.currentTimeMillis()) + " clean thread was joint");
|
2009-04-14 09:55:58 +02:00
|
|
|
super.finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static class CleanSessionThread extends Thread
|
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
try {
|
2011-11-07 10:38:19 +01:00
|
|
|
Thread.sleep(300000);
|
2009-04-14 09:55:58 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println(new Date(System.currentTimeMillis()) + " clean thread was interrupted (in clean thread)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//TODO: cleanup invalid sessions: add locks...
|
|
|
|
Set<String> keys = sessionManager.sessions.keySet();
|
|
|
|
Iterator<String> iter = keys.iterator();
|
|
|
|
while(iter.hasNext())
|
|
|
|
{
|
|
|
|
String extSessionID = iter.next();
|
|
|
|
if(!sessionManager.sessions.get(extSessionID).isValid())
|
|
|
|
{
|
|
|
|
sessionManager.sessions.remove(extSessionID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println(new Date(System.currentTimeMillis()) + " clean thread was terminated");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|