From 841a195560577b5ab90c5e496ec7e53bd76649dc Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 31 May 2002 13:39:19 +0000 Subject: [PATCH] Rewrote getActiveUsers() and getSessionsForUser() to return ArrayList instead of an Enumeration. The reason is that this is easier to turn into an array and generally more versatile than an Enumeration. --- src/helma/framework/core/Application.java | 113 +++++++++--------- src/helma/framework/core/ApplicationBean.java | 24 +--- 2 files changed, 62 insertions(+), 75 deletions(-) diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index 707185c2..d7c31964 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -607,68 +607,65 @@ public class Application extends UnicastRemoteObject implements IRemoteApp, IPat sessions.remove (session.getSessionID ()); } - public Enumeration getActiveUsers () { - return new SessionFilter (null); + /** + * Return the whole session map. We return a clone of the table to prevent + * actual changes from the table itself, which is managed by the application. + * It is safe and allowed to manipulate the session objects contained in the table, though. + */ + public Map getSessions () { + return (Map) sessions.clone (); + } + + + /** + * Return a list of Helma nodes (HopObjects - the database object representing the user, + * not the session object) representing currently logged in users. + */ + public List getActiveUsers () { + ArrayList list = new ArrayList(); + // used to keep track of already added users - we only return + // one object per user, and users may have multiple sessions + HashSet usernames = new HashSet (); + for (Enumeration e=sessions.elements(); e.hasMoreElements(); ) { + Session s = (Session) e.nextElement (); + if(s==null) { + continue; + } else if (s.isLoggedIn() && !usernames.contains (s.getUID ()) ) { + // returns a session if it is logged in and has not been + // returned before (so for each logged-in user we get one + // session object, even if this user is logged in several + // times (used to retrieve the active users list). + INode node = s.getUserNode (); + // we check again because user may have been logged out between the first check + if (node != null) { + usernames.add (s.getUID ()); + list.add(node); + } + } + } + return list; } /** - * Return an array of session currently associated with a given Hop user object. + * Return an array of SessionBean objects currently associated with a given + * Helma user. */ - public Enumeration getSessionsForUsername (String username) { - return new SessionFilter (username); + public List getSessionsForUsername (String username) { + ArrayList list = new ArrayList(); + if (username == null) + return list; + for (Enumeration e=sessions.elements(); e.hasMoreElements(); ) { + Session s = (Session) e.nextElement (); + if(s==null) { + continue; + } else if (username.equals (s.getUID ())) { + // append to list if session is logged in and fits the given username + list.add (new SessionBean (s)); + } + } + return list; } - class SessionFilter implements Enumeration { - String username; - Session nextSession; - Enumeration sessionEnum; - Vector usernames; - - SessionFilter (String username) { - this.username = username; - if (username==null) - usernames = new Vector(); - sessionEnum = sessions.elements (); - nextSession = nextValidSession (); - } - - public boolean hasMoreElements () { - if (nextSession==null) - return false; - else - return true; - } - - public Object nextElement () { - Session thisSession = nextSession; - nextSession = nextValidSession (); - return thisSession; - } - - private Session nextValidSession () { - while( sessionEnum.hasMoreElements () ) { - Session s = (Session)sessionEnum.nextElement (); - if(s==null) { - continue; - } else { - if (username!=null && username.equals(s.getUID ())) { - // returns a session if it is logged in and fits - // the given username - return s; - } else if (username==null && s.isLoggedIn()==true && !usernames.contains (s.getUID ()) ) { - // returns a session if it is logged in and has not been - // returned before (so for each logged-in user we get one - // session object, even if this user is logged in several - // times (used to retrieve the active users list). - usernames.add (s.getUID ()); - return s; - } - } - } - return null; - } - } - /** * Return the session currently associated with a given Hop session ID. @@ -1239,6 +1236,10 @@ public class Application extends UnicastRemoteObject implements IRemoteApp, IPat return errorCount; } + public long getStarttime () { + return starttime; + } + /** * Periodically called to log thread stats for this application */ diff --git a/src/helma/framework/core/ApplicationBean.java b/src/helma/framework/core/ApplicationBean.java index 4a37271c..99b28fac 100644 --- a/src/helma/framework/core/ApplicationBean.java +++ b/src/helma/framework/core/ApplicationBean.java @@ -7,6 +7,7 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; +import java.util.List; import helma.objectmodel.INode; @@ -88,16 +89,8 @@ public class ApplicationBean implements Serializable { } public INode[] getActiveUsers () { - Enumeration loggedInSessions = app.getActiveUsers (); - if (loggedInSessions.hasMoreElements ()==false) - return new INode[0]; - ArrayList theArray = new ArrayList(); - while (loggedInSessions.hasMoreElements ()) { - INode usernode = ((Session) loggedInSessions.nextElement ()).getUserNode (); - if (usernode!=null) - theArray.add (usernode); - } - return (INode[]) theArray.toArray (new INode[0]); + List activeUsers = app.getActiveUsers (); + return (INode[]) activeUsers.toArray (new INode[0]); } public SessionBean[] getSessionsForUser (INode usernode) { @@ -110,15 +103,8 @@ public class ApplicationBean implements Serializable { public SessionBean[] getSessionsForUser (String username) { if (username==null || "".equals (username.trim ()) ) return new SessionBean[0]; - Enumeration userSessions = app.getSessionsForUsername (username); - if (userSessions.hasMoreElements()==false ) - return new SessionBean[0]; - ArrayList theArray = new ArrayList(); - while (userSessions.hasMoreElements() ) { - SessionBean sb = new SessionBean ((Session) userSessions.nextElement ()); - theArray.add(sb); - } - return (SessionBean[]) theArray.toArray (new SessionBean[0]); + List userSessions = app.getSessionsForUsername (username); + return (SessionBean[]) userSessions.toArray (new SessionBean[0]); } // getter methods for readonly properties of this application