src/java/isac/session/SessionDialog.java
author wneuper
Sat, 12 Mar 2005 06:14:55 +0100
changeset 2162 161819a0cbbc
parent 2133 4c9e512a37a1
child 2163 4642d95c621c
permissions -rw-r--r--
all-050311a-beforeStart: of impl. userSettings
     1 /*
     2  * Created on Jun 24, 2003
     3  *
     4  * To change the template for this generated file go to
     5  * Window>Preferences>Java>Code Generation>Code and Comments
     6  */
     7 package isac.session;
     8 
     9 import isac.browserdialog.BDialog;
    10 import isac.browserdialog.BrowserDialog;
    11 import isac.gui.WindowApplication;
    12 import isac.wsdialog.DGuide;
    13 
    14 import java.io.Serializable;
    15 import java.rmi.RemoteException;
    16 import java.rmi.server.UnicastRemoteObject;
    17 import java.util.HashMap;
    18 import java.util.Iterator;
    19 import java.util.Map;
    20 import java.util.Vector;
    21 
    22 import org.apache.log4j.Logger;
    23 import org.apache.xmlrpc.WebServer;
    24 
    25 /**
    26  * @author Andreas Griesmayer
    27  */
    28 public class SessionDialog extends UnicastRemoteObject implements SDialog,
    29     Serializable {
    30 
    31 	static Logger logger_ = Logger.getLogger(SessionDialog.class.getName());
    32 
    33 	//private Map dguide_map_ = new HashMap();//WN0503 unused
    34 
    35   private int counter_;
    36 
    37   private static UMHandler um_handler_ = new UMHandler();
    38 
    39   private static WSDHandler dg_handler_ = new WSDHandler();
    40 
    41   private Map sessionmap_ = new HashMap();
    42 
    43   private Map bdialogmap_ = new HashMap();
    44 
    45   private static SessionDialog instance_;
    46 
    47   private SessionDialog() throws RemoteException {
    48     init();
    49     System.out.println("SessionDialog constructor");
    50   }
    51 
    52   /**
    53    * returns the instance of the singleton SessionDialog. The SessionDialog is
    54    * responsible for maintaining all session-relevant informations like Browser-
    55    * and WorksheetDialog and UserModel it provides its functions using RMI and
    56    * XML-RPC
    57    * 
    58    * @return
    59    */
    60   public static SessionDialog getInstance() {
    61     if (instance_ == null) {
    62       try {
    63         instance_ = new SessionDialog();
    64       } catch (RemoteException e) {
    65         e.printStackTrace();
    66       }
    67     }
    68     return instance_;
    69   }
    70 
    71   // LK 04.09.02: user control with the UserManager
    72   /**
    73    * @param username
    74    *          name of the user
    75    * @param pass
    76    *          password
    77    * @return vector with the sessionid as first and only element on success or
    78    *         empty vector on failure
    79    */
    80   public Vector login(String username, String pass) {
    81     Vector return_val = new Vector();
    82     User user = UserManager.getUser(username, pass);
    83     String sessionid = null;
    84     if (user != null) {
    85       //WN050222 String sessionid;
    86       if ((sessionid = (String) sessionmap_.get(username)) == null) {
    87         sessionmap_.put(username, (sessionid = username + counter_++));
    88         um_handler_.newUserModel(username, sessionid);
    89       }
    90       return_val.add(sessionid);
    91     }
    92     logger_.fatal("WA<-SH: login <- sessionid=" + sessionid);
    93     return return_val;
    94   }
    95 
    96   /**
    97    * @param sessionId
    98    * @return the user for the session id
    99    */
   100   public User getUser(String sessionId) {
   101     //TODO: get user
   102     return null;
   103   }
   104 
   105   /**
   106    * returns the name of the user which owns the given session.
   107    * 
   108    * @param session
   109    *          ID of the session for which the owner is requested
   110    * @return the name of the owner of the session
   111    * TODO.WN050311 encapsulate User#getName !!!
   112    */
   113   public String getUsername(String session) {
   114     Iterator keys = sessionmap_.keySet().iterator();
   115     String username;
   116     while (keys.hasNext()) {
   117       if (session.equals(sessionmap_.get((username = (String) keys.next())))) {
   118         return username;
   119       }
   120     }
   121     return "";
   122   }
   123 
   124   /**
   125    * create a new WSDialog and assign it to session <code>session</code>
   126    */
   127   public String openDGuide(String session) {
   128     return dg_handler_.newDGuide(this, session);
   129   }
   130 
   131   public DGuide getDGuide(String dg_id) {
   132     return (DGuide) dg_handler_.getDGuide(dg_id);
   133   }
   134 
   135   /**
   136    * used if the SessionDialog is used in standalone mode for testing-purposes.
   137    * Registers its XML-RPC services on port 1050 of the local machine
   138    * 
   139    * @param args
   140    *          not used
   141    */
   142   public static void main(String[] args) {
   143     init();
   144   }
   145 
   146   private static void init() {
   147     try {
   148       // Invoke me as <http://localhost:8080/RPC2>.
   149       System.out.println("SessionDialog main");
   150       WebServer server = new WebServer(1050);//WN0503 fixed port !
   151       server.setParanoid(false);
   152       //			server.addHandler("sample", new JavaServer());
   153       server.addHandler("SDialog", getInstance());
   154       server.addHandler(" WSDialog", dg_handler_);
   155       // LK 04.09.02: new temorary user management
   156       //server.addHandler("UserModel", um_handler_);
   157 
   158     } catch (Exception exception) {
   159       System.err.println("SessionDialog: " + exception.toString());
   160     }
   161 
   162   }
   163 
   164   public void finalize() {
   165     System.out.println("SessionDialog finalize!");
   166   }
   167 
   168   /**
   169    * returns the BrowserDialog identified by <code>bdialog_id</code> or
   170    * creates a new one.
   171    * 
   172    * @param bdialog_id
   173    *          ID of the BrowserDialog to open.
   174    * @return RMI-Object of the requested BrowserDialog
   175    */
   176   public BDialog getBDialog(String bdialog_id) {
   177     BDialog browser_dialog = (BDialog) bdialogmap_.get(bdialog_id);
   178     if (browser_dialog == null) {
   179       try {
   180         bdialogmap_.put(bdialog_id, (browser_dialog = new BrowserDialog(
   181             bdialog_id)));
   182       } catch (RemoteException e) {
   183         System.err.println("unable to create new BrowserDialog for session "
   184             + bdialog_id);
   185         e.printStackTrace();
   186       }
   187     }
   188     return browser_dialog;
   189   }
   190 }