~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/www/src/main/java/com/eucalyptus/webui/server/WebSessionManager.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.webui.server;
 
2
 
 
3
import java.util.Map;
 
4
import com.eucalyptus.auth.AuthenticationProperties;
 
5
import com.eucalyptus.auth.AuthenticationProperties.LicChangeListener;
 
6
import com.eucalyptus.configurable.ConfigurableClass;
 
7
import com.eucalyptus.configurable.ConfigurableField;
 
8
import com.google.common.collect.Maps;
 
9
 
 
10
/**
 
11
 * Web session manager, maintaining a web session registrar.
 
12
 * 
 
13
 * @author Ye Wen (wenye@eucalyptus.com)
 
14
 *
 
15
 */
 
16
public class WebSessionManager {
 
17
  
 
18
  private static WebSessionManager instance = null;
 
19
  
 
20
  private Map<String, WebSession> sessions = Maps.newHashMap( );
 
21
  
 
22
  private WebSessionManager( ) {
 
23
    
 
24
  }
 
25
  
 
26
  public static synchronized WebSessionManager getInstance( ) {
 
27
    if ( instance == null ) {
 
28
      instance = new WebSessionManager( );
 
29
    }
 
30
    return instance;
 
31
  }
 
32
  
 
33
  /**
 
34
   * Create new web session record.
 
35
   * 
 
36
   * @param userName
 
37
   * @param accountName
 
38
   * @return the new session ID.
 
39
   */
 
40
  public synchronized String newSession( String userName, String accountName ) {
 
41
    String id = ServletUtils.genGUID( );
 
42
    long time = System.currentTimeMillis( );
 
43
    WebSession session = new WebSession( id, userName, accountName, time/*creationTime*/, time/*lastAccessTime*/ );
 
44
    sessions.put( id, session );
 
45
    return id;
 
46
  }
 
47
  
 
48
  /**
 
49
   * Get a session by ID. Remove this session if expired.
 
50
   * 
 
51
   * @param id
 
52
   * @return the session, null if not exists or expired.
 
53
   */
 
54
  public synchronized WebSession getSession( String id ) {
 
55
    WebSession session = sessions.get( id );
 
56
    if ( session != null ) {
 
57
      if ( System.currentTimeMillis( ) - session.getCreationTime( ) > AuthenticationProperties.WEBSESSION_LIFE_IN_MINUTES * 60 * 1000 ) {
 
58
        sessions.remove( id );
 
59
        session = null;
 
60
      }
 
61
    }
 
62
    return session;
 
63
  }
 
64
  
 
65
  /**
 
66
   * Get a session by user name and account name. Remove the found session if expired.
 
67
   * 
 
68
   * @param userName
 
69
   * @param accountName
 
70
   * @return
 
71
   */
 
72
  public synchronized WebSession getSession( String userName, String accountName ) {
 
73
        for ( WebSession session : sessions.values( ) ) {
 
74
          if ( session != null && session.getUserName( ).equals( userName ) && session.getAccountName( ).equals( accountName ) ) {
 
75
            if ( System.currentTimeMillis( ) - session.getCreationTime( ) > AuthenticationProperties.WEBSESSION_LIFE_IN_MINUTES * 60 * 1000 ) {
 
76
              sessions.remove( session.getId( ) );
 
77
              return null;
 
78
            }
 
79
            return session;
 
80
          }
 
81
        }
 
82
        return null;
 
83
  }
 
84
  
 
85
  /**
 
86
   * Remove a session.
 
87
   * 
 
88
   * @param id
 
89
   */
 
90
  public synchronized void removeSession( String id ) {
 
91
    if ( id != null ) {
 
92
      sessions.remove( id );
 
93
    }
 
94
  }
 
95
  
 
96
}