src/java/isac/session/SessionManager.java
author wneuper
Thu, 17 Jan 2008 16:27:03 +0100
changeset 3881 72f0be16d83b
parent 3817 ceceac791737
child 3918 0417a220729b
permissions -rw-r--r--
start-work-070517 merged into HEAD
     1 /*
     2  * @author Robert Koenighofer, member of the ISAC-team, 
     3  * Copyright (c) 2006 by Robert Koenighofer
     4  * created Jul 11, 2006 10:09:13 AM
     5  * Institute for Softwaretechnology, Graz University of Technology, Austria.
     6  * 
     7  * Use is subject to PGPL license terms.
     8  */
     9 
    10 package isac.session;
    11 
    12 import isac.users.User;
    13 import isac.users.UserLogger;
    14 import isac.users.UserManager;
    15 
    16 import java.io.Serializable;
    17 import java.rmi.RemoteException;
    18 import java.rmi.server.UnicastRemoteObject;
    19 import java.util.HashMap;
    20 
    21 import org.apache.log4j.Logger;
    22 
    23 /**
    24  * This class administrates all sessions. It is implemented as Singleton (design
    25  * Pattern of Gamma et.al.), which means, that there can be only one instance of
    26  * this class. This instance can be accessed by the getInstance() method. The
    27  * class allows to generate new Sessions and to deliver session objects with a
    28  * specific session id. The session id is always unique.
    29  * 
    30  * @author Robert Koenighofer
    31  */
    32 public class SessionManager extends UnicastRemoteObject implements
    33         ISessionManager, Serializable {
    34 
    35     static final long serialVersionUID = 284066283;// FIXXME generate with
    36 
    37     // plugin
    38 
    39     private static final Logger logger = Logger.getLogger(SessionManager.class
    40             .getName());;
    41 
    42     private int next_id_;
    43 
    44     private static SessionManager instance_ = null;
    45 
    46     private HashMap<String, Session> sessionid_map_session_;
    47 
    48     private HashMap<String, String> sessionid_map_username_;
    49     
    50     public ICompod compod_;
    51 
    52     /**
    53      * The constructor is private, since this class is implemented as singleton.
    54      * Use the getInstance() method to get the one and only instance of this
    55      * class.
    56      * 
    57      * @throws RemoteException
    58      *             if remote object is not available
    59      */
    60     private SessionManager() throws RemoteException {
    61         sessionid_map_session_ = new HashMap<String, Session>();
    62         sessionid_map_username_ = new HashMap<String, String>();
    63         next_id_ = 0;
    64         compod_ = new CompodDummy();
    65         try {
    66             compod_.setLMSID("isac");
    67         } catch (Exception e) {
    68             e.printStackTrace();
    69         }
    70     }
    71 
    72     /**
    73      * This method creates a new session for the user with the name 'user_name'.
    74      * It returns the created session. One user can have more than one session
    75      * at a time.
    76      * 
    77      * @param user_name
    78      *            the name of the user of the new session
    79      * @return the created session
    80      * @throws RemoteException
    81      *             if remote object is not available
    82      */
    83     public Session addSession(User user) throws RemoteException {
    84         String new_id = String.valueOf(next_id_++);
    85         Session new_session = new Session(new_id, user, compod_);
    86         sessionid_map_session_.put(new_id, new_session);
    87         sessionid_map_username_.put(new_id, user.getUsername());
    88         if (logger.isDebugEnabled())
    89             logger.debug(" SM: new Session with ID: " + new_id + " created");
    90         return new_session;
    91     }
    92 
    93     /**
    94      * This method removes the session with the specified id from the maps. It
    95      * is called, whenever a logout is done (by closing the WindowApplication).
    96      * 
    97      * @param session_id
    98      *            the id of the session to remove
    99      * @throws RemoteException
   100      *             if remote object is not available any more
   101      */
   102     public void removeSession(String session_id) throws RemoteException {
   103         // WN0710 the similar UI_CLOSE_WORKSHEET is done in the gui;
   104         // analogously this could be done on closing the WindowApplication:
   105         UserLogger user_logger = getSessionByID(session_id).getUserLogger();
   106         user_logger.logStopSession();
   107 
   108         sessionid_map_session_.remove(session_id);
   109         sessionid_map_username_.remove(session_id);
   110     }
   111 
   112     /**
   113      * returns the session with the desired session_id. If the session does not
   114      * exist, null is returned.
   115      * 
   116      * @param session_id
   117      *            the unique id of the desired session
   118      * @return the desired session or null if it doesn't exist
   119      * @throws RemoteException
   120      *             if remote object is not available
   121      */
   122     public Session getSessionByID(String session_id) throws RemoteException {
   123         return sessionid_map_session_.get(session_id);
   124     }
   125 
   126     /**
   127      * returns the name of the user, which owns a specific session, as String.
   128      * If the session does not exist, null is returned.
   129      * 
   130      * @param session_id
   131      *            the unique id of the desired session
   132      * @return the desired user name or null if the session does not exist
   133      * @throws RemoteException
   134      *             if remote object is not available
   135      */
   136     public String getUserNameByID(String session_id) throws RemoteException {
   137         return sessionid_map_username_.get(session_id);
   138     }
   139 
   140     public HashMap getSessionMap() {
   141         return sessionid_map_session_;
   142     }
   143 
   144     /**
   145      * returns the instance of the singleton SessionManager. Since the
   146      * constructor is private, this is the only method to get a SessionManager.
   147      * It always returns one and the same instance of this class.
   148      * 
   149      * @return the one and only instance of this class.
   150      * @throws RemoteException
   151      *             if remote object is not available
   152      */
   153     public static SessionManager getInstance() throws RemoteException {
   154         if (instance_ == null) {
   155             try {
   156                 instance_ = new SessionManager();
   157             } catch (RemoteException e) {
   158                 e.printStackTrace();
   159             }
   160         }
   161         return instance_;
   162     }
   163 }