bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
ServerSessionManager.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.serversession;
2 
3 import java.util.Iterator;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6 import java.util.UUID;
7 import java.util.concurrent.TimeUnit;
8 
9 import org.openslx.bwlp.thrift.iface.ServerSessionData;
11 import org.openslx.util.QuickTimer;
12 import org.openslx.util.QuickTimer.Task;
13 
18 {
19  // Map of currently known sessions
20  private static final Map<String, ServerSession> serverSessions = new LinkedHashMap<String, ServerSession>();
21 
22  public static ServerSessionData addSession( ServerSession serverSession )
23  {
24  final String sessionId = Hash.sha256( UUID.randomUUID().toString() );
25 
26  synchronized ( serverSessions ) {
27  serverSessions.put( sessionId, serverSession );
28  }
29  return new ServerSessionData( sessionId );
30  }
31 
32  public static ServerSession getSession( String sessionId )
33  {
34  final ServerSession serverSession;
35  synchronized ( serverSessions ) {
36  serverSession = serverSessions.get( sessionId );
37  }
38  if ( serverSession == null || serverSession.timedOut() ) {
39  return null;
40  }
41  serverSession.refresh();
42  return serverSession;
43  }
44 
45  static {
46  QuickTimer.scheduleAtFixedDelay( new Task() {
47  @Override
48  public void fire()
49  {
50  synchronized ( serverSessions ) {
51  Iterator<ServerSession> it = serverSessions.values().iterator();
52  while ( it.hasNext() ) {
53  final ServerSession s = it.next();
54  if ( s.timedOut() ) {
55  it.remove();
56  }
57  }
58  }
59  }
60  }, 911, TimeUnit.MINUTES.toMillis( 14 ) );
61  }
62 
63 }
static ServerSessionData addSession(ServerSession serverSession)
Manages all server sessions and kicks timed out sessions.
static String sha256(final byte[] bytes)
Compute sha256 hash of given binary data.
Definition: Hash.java:85
static final Map< String, ServerSession > serverSessions
Holds the session id of the server and manages the timeout.