r9863 - helma/helma/trunk/src/helma/framework/core

[email protected] Thu, 10 Sep 2009 01:27:04 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090909232704.667AA3D0E3@mia>
Author: hannes
Date: 2009-09-10 01:27:04 +0200 (Thu, 10 Sep 2009)
New Revision: 9863

Modified:
   helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java
   helma/helma/trunk/src/helma/framework/core/Session.java
   helma/helma/trunk/src/helma/framework/core/SessionBean.java
   helma/helma/trunk/src/helma/framework/core/SessionManager.java
Log:
Redesign session management to only register sessions with the session manager that have been changed.

Details at http://dev.helma.org/trac/helma/changeset/9863

Modified: helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java	2009-09-09 23:25:22 UTC (rev 9862)
+++ helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java	2009-09-09 23:27:04 UTC (rev 9863)
@@ -784,7 +784,7 @@
             res.reportError("Request timed out");
         }
 
-        session.commit(this);
+        session.commit(this, app.sessionMgr);
         return res;
     }
 

Modified: helma/helma/trunk/src/helma/framework/core/Session.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/Session.java	2009-09-09 23:25:22 UTC (rev 9862)
+++ helma/helma/trunk/src/helma/framework/core/Session.java	2009-09-09 23:27:04 UTC (rev 9863)
@@ -46,9 +46,12 @@
     // the transient cache node that is exposed to javascript
     // this stays the same across logins and logouts.
     protected INode cacheNode;
+
+    // timestamps for creation, last request, last modification
     protected long onSince;
     protected long lastTouched;
     protected long lastModified;
+    protected long cacheLastModified;
 
     // used to remember messages to the user between requests, mainly between redirects.
     protected String message;
@@ -56,6 +59,9 @@
 
     protected HashMap uploads = null;
 
+    protected transient boolean modifiedInRequest = false;
+    protected transient boolean registered = false;
+
     /**
      * Creates a new Session object.
      *
@@ -68,7 +74,10 @@
         this.uid = null;
         this.userHandle = null;
         cacheNode = new TransientNode("session");
-        onSince = System.currentTimeMillis();
+        cacheLastModified = cacheNode.lastModified();
+        // HACK - decrease timestamp by 1 to notice modifications
+        // taking place immediately after object creation
+        onSince = System.currentTimeMillis() - 1;
         lastTouched = lastModified = onSince;
     }
 
@@ -85,17 +94,22 @@
         }
 
         lastModified = System.currentTimeMillis();
+        modifiedInRequest = true;
     }
 
     /**
      * Try logging in this session given the userName and password.
      *
-     * @param userName
-     * @param password
+     * @param userName the user name
+     * @param password the password
      * @return true if session was logged in.
      */
     public boolean login(String userName, String password) {
-        return app.loginSession(userName, password, this);
+        if (app.loginSession(userName, password, this)) {
+            lastModified = System.currentTimeMillis();
+            modifiedInRequest = true;
+        }
+        return false;
     }
 
     /**
@@ -122,6 +136,8 @@
                 userHandle = null;
                 uid = null;
                 lastModified = System.currentTimeMillis();
+                modifiedInRequest = true;
+
             }
         }
     }
@@ -132,7 +148,7 @@
      * @return ...
      */
     public boolean isLoggedIn() {
-        return (userHandle != null) && (uid != null);
+        return userHandle != null;
     }
 
     /**
@@ -164,7 +180,11 @@
      * Set the cache node for this session.
      */
     public void setCacheNode(INode node) {
+        if (node == null) {
+            throw new NullPointerException("cache node is null");
+        }
         this.cacheNode = node;
+        this.cacheLastModified = cacheNode.lastModified();
     }
 
     /**
@@ -214,8 +234,15 @@
      *
      * @param reval the request evaluator that handled the request
      */
-    public void commit(RequestEvaluator reval) {
-        // nothing to do
+    public void commit(RequestEvaluator reval, SessionManager smgr) {
+        if (modifiedInRequest || cacheLastModified != cacheNode.lastModified()) {
+            if (!registered) {
+                smgr.registerSession(this);
+                registered = true;
+            }
+            modifiedInRequest = false;
+            cacheLastModified = cacheNode.lastModified();
+        }
     }
 
     /**
@@ -240,12 +267,10 @@
     /**
      * Set the last modified time on this session.
      *
-     * @param date ...
+     * @param l the timestamp
      */
-    public void setLastModified(Date date) {
-        if (date != null) {
-            lastModified = date.getTime();
-        }
+    public void setLastModified(long l) {
+        lastModified = l;
     }
 
     /**
@@ -278,6 +303,13 @@
         return uid;
     }
 
+    /**
+     * Set the persistent user id of a registered user.
+     * @param uid the user name, or null if the user is not logged in.
+     */
+    public void setUID(String uid) {
+        this.uid = uid;
+    }
 
     /**
      * Set the user and debug messages over from a previous response.
@@ -290,6 +322,7 @@
             res.setDebugBuffer(debugBuffer);
             message = null;
             debugBuffer = null;
+            modifiedInRequest = true;
         }
     }
 
@@ -301,6 +334,9 @@
     public synchronized void storeResponseMessages(ResponseTrans res) {
         message = res.getMessage();
         debugBuffer = res.getDebugBuffer();
+        if (message != null || debugBuffer != null) {
+            modifiedInRequest = true;
+        }
     }
 
     /**

Modified: helma/helma/trunk/src/helma/framework/core/SessionBean.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/SessionBean.java	2009-09-09 23:25:22 UTC (rev 9862)
+++ helma/helma/trunk/src/helma/framework/core/SessionBean.java	2009-09-09 23:27:04 UTC (rev 9863)
@@ -181,7 +181,9 @@
      * @param date ...
      */
     public void setLastModified(Date date) {
-        session.setLastModified(date);
+        if (date != null) {
+            session.setLastModified(date.getTime());
+        }
     }
 
     /**

Modified: helma/helma/trunk/src/helma/framework/core/SessionManager.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/SessionManager.java	2009-09-09 23:25:22 UTC (rev 9862)
+++ helma/helma/trunk/src/helma/framework/core/SessionManager.java	2009-09-09 23:27:04 UTC (rev 9863)
@@ -43,22 +43,23 @@
 
     public Session createSession(String sessionId) {
         Session session = getSession(sessionId);
-
         if (session == null) {
             session = new Session(sessionId, app);
-            sessions.put(sessionId, session);
         }
-
         return session;
     }
 
     public Session getSession(String sessionId) {
-        if (sessionId == null)
+        if (sessionId == null) {
             return null;
-
+        }
         return (Session) sessions.get(sessionId);
     }
 
+    public void registerSession(Session session) {
+        sessions.put(session.getSessionId(), session);        
+    }
+
     /**
      *  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.
@@ -83,24 +84,8 @@
         sessions.remove(session.getSessionId());
     }
 
-    /**
-     * Log in a user given his or her user name and password.
-     * @deprecated
-     */
-    public boolean loginSession(String uname, String password, Session session) {
-        return app.loginSession(uname, password, session);
-    }
 
     /**
-     * Log out a session from this application.
-     * @deprecated
-     */
-    public void logoutSession(Session session) {
-        app.logoutSession(session);
-    }
-
-
-    /**
      * Return an array of <code>SessionBean</code> objects currently associated with a given
      * Helma user.
      */