Again, with patches as attachment.

--Shai

-----Original Message-----
From: Shai Fultheim (BRM IL) 
Sent: Sunday, December 24, 2000 23:27
To: [EMAIL PROTECTED]
Subject: [PATCH] Tomcat session replicator - Pathces for phase I

Hi,


These patches will allow you to add serialize="true" to you context and
having your context saved to disk.

Index: src/share/org/apache/tomcat/core/Context.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/core/Context.java
,v
retrieving revision 1.129
diff -w -u -r1.129 Context.java
--- src/share/org/apache/tomcat/core/Context.java       2000/12/13 19:58:01
1.129
+++ src/share/org/apache/tomcat/core/Context.java       2000/12/24 21:20:34
@@ -81,6 +81,9 @@
  * You need to set at least "path" and "base" before adding a
  * context to a server. You can also set any other properties.
  *
+ * Setting context's serialize attribute to true will cause it dump its
+ * session information to file on request end, startup and shutdown.
+ *
  * At addContext() stage log and paths will be "fixed" based on
  * context manager settings.
  *
@@ -94,6 +97,7 @@
  * @author Harish Prabandham
  * @author [EMAIL PROTECTED]
  * @author Gal Shachor [EMAIL PROTECTED]
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class Context implements LogAware {
     // -------------------- Constants --------------------
@@ -172,6 +176,9 @@
     // enable reloading
     private boolean reloadable=true;

+       // enable serialization
+       private boolean serialize = false;
+
     // XXX Use a better repository
     private Hashtable attributes = new Hashtable();

@@ -558,6 +565,16 @@
        return reloadable;
     }

+       public void setSerialize( boolean b ) {
+                  serialize=b;
+       }
+
+       /** Should we serialize sessions ?
+        */
+       public boolean getSerialize() {
+                  return serialize;
+       }
+
     // -------------------- API level --------------------
    
     /** The servlet API variant that will be used for requests in this
@@ -1075,4 +1092,15 @@
         defaultContainer.addInterceptor(ri);
     }

+       public final String getFileName() {
+               return System.getProperty("tomcat.home") + "/bin/Sessions" +
cleanupFileName(getPath()) + ".ses";
+       }
+
+       private String cleanupFileName(String fn) {
+               String f = fn;
+               f = f.replace('/','_');
+               f = f.replace('\\','_');
+
+               return f;
+       }
 }
Index: src/share/org/apache/tomcat/core/ContextManager.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManag
er.java,v
retrieving revision 1.157
diff -w -u -r1.157 ContextManager.java
--- src/share/org/apache/tomcat/core/ContextManager.java        2000/12/08
23:18:43        1.157
+++ src/share/org/apache/tomcat/core/ContextManager.java        2000/12/24
21:20:37
@@ -62,6 +62,7 @@

 import org.apache.tomcat.util.*;
 import org.apache.tomcat.util.log.*;
+import org.apache.tomcat.session.ServerSession;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -180,6 +181,7 @@
   @author Harish Prabandham
   @author [EMAIL PROTECTED]
   @author Hans Bergsten [[EMAIL PROTECTED]]
+  @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class ContextManager implements LogAware{
     /** Official name and version
@@ -713,6 +715,23 @@
            for( int i=0; i< reqI.length; i++ ) {
                reqI[i].postRequest( req, res );
            }
+
+               if ((req.getSession(false) != null) &&
req.getContext().getSerialize()) {
+                       Context ctx = req.getContext();
+
+                       ServerSession ses = req.getSession(false);
+
+                       if (debug > 0 ) {
+                               log("Serializing session " + ses.getId() +
+                                       ", lasted  " +
(ses.getTimeStamp().getLastAccessedTime() -
+
ses.getTimeStamp().getCreationTime())/1000 +
+                                       " Secs.");
+                       }
+
+                       File f = new File(ctx.getFileName());
+                       ses.getSessionManager().serializeSessions(f);
+               }
+
            req.recycle();
            res.recycle();
        }
Index: src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/modules/session/S
impleSessionStore.java,v
retrieving revision 1.4
diff -w -u -r1.4 SimpleSessionStore.java
--- src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java
2000/11/30 06:17:12     1.4
+++ src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java
2000/12/24 21:20:39
@@ -249,6 +249,16 @@
        }
        sm.setMaxActiveSessions( maxActiveSessions );

+               if (ctx.getSerialize()) {
+                       String fileName = ctx.getFileName();
+
+                       File f = new File(fileName);
+                       if (f.exists()) {
+                               if( ctx.getDebug() > 0 )
ctx.log("deSerializing sessions from " + fileName);
+                               sm.deSerializeSessions(f);
+                       }
+               }
+
        Expirer expirer=new Expirer();
        expirer.setCheckInterval( checkInterval );
        expirer.setExpireCallback( new Expirer.ExpireCallback() {
@@ -272,6 +282,14 @@
        if( debug > 0 ) ctx.log("Removing sessions from " + ctx );
        ServerSessionManager sm=getManager(ctx);
        sm.getExpirer().stop();
+
+               if (ctx.getSerialize()) {
+                       String fileName = ctx.getFileName();
+
+                       File f = new File(fileName);
+                       if( debug > 0 ) ctx.log("Serializing sessions from "
+ fileName);
+                       sm.serializeSessions(f);
+               }
        sm.removeAllSessions();
     }

@@ -279,7 +297,4 @@
     private ServerSessionManager getManager( Context ctx ) {
        return
(ServerSessionManager)ctx.getContainer().getNote(manager_note);
     }
-
-
-
 }
Index: src/share/org/apache/tomcat/session/ServerSession.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSes
sion.java,v
retrieving revision 1.8
diff -w -u -r1.8 ServerSession.java
--- src/share/org/apache/tomcat/session/ServerSession.java      2000/11/22
00:09:59        1.8
+++ src/share/org/apache/tomcat/session/ServerSession.java      2000/12/24
21:20:40
@@ -82,6 +82,7 @@
  * @author Craig R. McClanahan
  * @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
  * @author Costin Manolache
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class ServerSession  implements  Serializable {
     private static StringManager sm =
@@ -161,5 +162,12 @@
        ts.recycle();
        id.recycle();
     }
+
+       private void writeObject(ObjectOutputStream s) throws IOException {
+               ServerSessionManager orig  = ssm;
+               ssm = null;
+               s.defaultWriteObject();
+               ssm = orig;
+       }
 }
Index: src/share/org/apache/tomcat/session/ServerSessionManager.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSes
sionManager.java,v
retrieving revision 1.14
diff -w -u -r1.14 ServerSessionManager.java
--- src/share/org/apache/tomcat/session/ServerSessionManager.java
2000/12/07 19:52:46     1.14
+++ src/share/org/apache/tomcat/session/ServerSessionManager.java
2000/12/24 21:20:41
@@ -61,6 +61,7 @@
 package org.apache.tomcat.session;

 import java.io.IOException;
+import java.io.*;
 import java.util.*;
 import org.apache.tomcat.util.*;
 import org.apache.tomcat.util.collections.SimplePool;
@@ -70,8 +71,8 @@
 import org.apache.tomcat.util.log.*;

 /**
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  *
- *
  */
 public final class ServerSessionManager 
 {
@@ -226,4 +227,58 @@
        }
     }

+
+       public void serializeSessions(File f) {
+               if (sessions.size() == 0)
+                  return;
+
+               FileOutputStream fStream = null;
+               ObjectOutputStream oStream = null;
+
+               try {
+                       fStream = new FileOutputStream(f);
+                       oStream = new ObjectOutputStream(fStream);
+                       // Serialize to file all active session
+                       Enumeration ids = sessions.keys();
+                       ServerSession session = null;
+                       while (ids.hasMoreElements()) {
+                               session =
findSession(ids.nextElement().toString());
+                               if (!session.getTimeStamp().isValid())
+                                  continue;
+                               oStream.writeObject(session);
+                       }
+                       oStream.flush();
+                       fStream.close();
+               } catch (Exception e) {
+                       System.out.println(e);
+               }
+       }
+
+       public void deSerializeSessions(File f)
+       {
+               FileInputStream fStream = null;
+               try {
+                       fStream = new FileInputStream(f);
+                       ObjectInputStream oStream = new
ObjectInputStream(fStream);
+                       ServerSession session = null;
+                       do {
+                               session =
(ServerSession)oStream.readObject();
+                               if (session != null) {
+                                       session.ssm = this;
+                                       sessions.put(session.getId(),
session);
+                               }
+                       } while (session != null);
+               } catch (java.io.FileNotFoundException n) {
+                       ;
+               } catch (java.io.EOFException eof) {
+                       try {
+                               fStream.close();
+                       } catch (Exception e) {
+                               System.out.println(e);
+                       }
+               } catch (Exception e) {
+                       System.out.println(e);
+               }
+               f.delete();
+       }
 }



________________________
Shai Fultheim
Chief Technology Officer
BRM Seed

E-Mail: [EMAIL PROTECTED]
Mobile: 972-53-866-459
Office: 972-2-5891-459

Index: src/share/org/apache/tomcat/core/Context.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/core/Context.java,v
retrieving revision 1.129
diff -w -u -r1.129 Context.java
--- src/share/org/apache/tomcat/core/Context.java       2000/12/13 19:58:01     1.129
+++ src/share/org/apache/tomcat/core/Context.java       2000/12/24 21:39:45
@@ -81,6 +81,9 @@
  * You need to set at least "path" and "base" before adding a
  * context to a server. You can also set any other properties.
  *
+ * Setting context's serialize attribute to true will cause it dump its
+ * session information to file on request end, startup and shutdown.
+ *
  * At addContext() stage log and paths will be "fixed" based on
  * context manager settings.
  *
@@ -94,6 +97,7 @@
  * @author Harish Prabandham
  * @author [EMAIL PROTECTED]
  * @author Gal Shachor [EMAIL PROTECTED]
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class Context implements LogAware {
     // -------------------- Constants --------------------
@@ -172,6 +176,9 @@
     // enable reloading
     private boolean reloadable=true; 
 
+       // enable serialization
+       private boolean serialize = false;
+
     // XXX Use a better repository
     private Hashtable attributes = new Hashtable();
 
@@ -558,6 +565,16 @@
        return reloadable;
     }
 
+       public void setSerialize( boolean b ) {
+                  serialize=b;
+       }
+
+       /** Should we serialize sessions ?
+        */
+       public boolean getSerialize() {
+                  return serialize;
+       }
+
     // -------------------- API level --------------------
     
     /** The servlet API variant that will be used for requests in this
@@ -1075,4 +1092,15 @@
         defaultContainer.addInterceptor(ri);
     }
 
+       public final String getFileName() {
+               return System.getProperty("tomcat.home") + "/bin/Sessions" + 
+cleanupFileName(getPath()) + ".ses";
+       }
+
+       private String cleanupFileName(String fn) {
+               String f = fn;
+               f = f.replace('/','_');
+               f = f.replace('\\','_');
+
+               return f;
+       }
 }
Index: src/share/org/apache/tomcat/core/ContextManager.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManager.java,v
retrieving revision 1.157
diff -w -u -r1.157 ContextManager.java
--- src/share/org/apache/tomcat/core/ContextManager.java        2000/12/08 23:18:43    
 1.157
+++ src/share/org/apache/tomcat/core/ContextManager.java        2000/12/24 21:39:47
@@ -62,6 +62,7 @@
 
 import org.apache.tomcat.util.*;
 import org.apache.tomcat.util.log.*;
+import org.apache.tomcat.session.ServerSession;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -180,6 +181,7 @@
   @author Harish Prabandham
   @author [EMAIL PROTECTED]
   @author Hans Bergsten [[EMAIL PROTECTED]]
+  @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class ContextManager implements LogAware{
     /** Official name and version
@@ -713,6 +715,23 @@
            for( int i=0; i< reqI.length; i++ ) {
                reqI[i].postRequest( req, res );
            }
+
+               if ((req.getSession(false) != null) && 
+req.getContext().getSerialize()) {
+                       Context ctx = req.getContext();
+
+                       ServerSession ses = req.getSession(false);
+
+                       if (debug > 0 ) {
+                               log("Serializing session " + ses.getId() +
+                                       ", lasted  " + 
+(ses.getTimeStamp().getLastAccessedTime() -
+                                          ses.getTimeStamp().getCreationTime())/1000 +
+                                       " Secs.");
+                       }
+
+                       File f = new File(ctx.getFileName());
+                       ses.getSessionManager().serializeSessions(f);
+               }
+
            req.recycle();
            res.recycle();
        }
Index: src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java,v
retrieving revision 1.4
diff -w -u -r1.4 SimpleSessionStore.java
--- src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java 2000/11/30 
06:17:12     1.4
+++ src/share/org/apache/tomcat/modules/session/SimpleSessionStore.java 2000/12/24 
+21:39:48
@@ -249,6 +249,16 @@
        }
        sm.setMaxActiveSessions( maxActiveSessions );
 
+               if (ctx.getSerialize()) {
+                       String fileName = ctx.getFileName();
+
+                       File f = new File(fileName);
+                       if (f.exists()) {
+                               if( ctx.getDebug() > 0 ) ctx.log("deSerializing 
+sessions from " + fileName);
+                               sm.deSerializeSessions(f);
+                       }
+               }
+
        Expirer expirer=new Expirer();
        expirer.setCheckInterval( checkInterval );
        expirer.setExpireCallback( new Expirer.ExpireCallback() {
@@ -272,6 +282,14 @@
        if( debug > 0 ) ctx.log("Removing sessions from " + ctx );
        ServerSessionManager sm=getManager(ctx);
        sm.getExpirer().stop();
+
+               if (ctx.getSerialize()) {
+                       String fileName = ctx.getFileName();
+
+                       File f = new File(fileName);
+                       if( debug > 0 ) ctx.log("Serializing sessions from " + 
+fileName);
+                       sm.serializeSessions(f);
+               }
        sm.removeAllSessions();
     }
 
@@ -279,7 +297,4 @@
     private ServerSessionManager getManager( Context ctx ) {
        return (ServerSessionManager)ctx.getContainer().getNote(manager_note);
     }
-
-
-
 }
Index: src/share/org/apache/tomcat/session/ServerSession.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSession.java,v
retrieving revision 1.8
diff -w -u -r1.8 ServerSession.java
--- src/share/org/apache/tomcat/session/ServerSession.java      2000/11/22 00:09:59    
 1.8
+++ src/share/org/apache/tomcat/session/ServerSession.java      2000/12/24 21:39:48
@@ -82,6 +82,7 @@
  * @author Craig R. McClanahan
  * @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
  * @author Costin Manolache
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  */
 public final class ServerSession  implements  Serializable {
     private static StringManager sm =
@@ -161,5 +162,12 @@
        ts.recycle();
        id.recycle();
     }
+
+       private void writeObject(ObjectOutputStream s) throws IOException {
+               ServerSessionManager orig  = ssm;
+               ssm = null;
+               s.defaultWriteObject();
+               ssm = orig;
+       }
 }
 
Index: src/share/org/apache/tomcat/session/ServerSessionManager.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/session/ServerSessionManager.java,v
retrieving revision 1.14
diff -w -u -r1.14 ServerSessionManager.java
--- src/share/org/apache/tomcat/session/ServerSessionManager.java       2000/12/07 
19:52:46     1.14
+++ src/share/org/apache/tomcat/session/ServerSessionManager.java       2000/12/24 
+21:39:49
@@ -61,6 +61,7 @@
 package org.apache.tomcat.session;
 
 import java.io.IOException;
+import java.io.*;
 import java.util.*;
 import org.apache.tomcat.util.*;
 import org.apache.tomcat.util.collections.SimplePool;
@@ -70,8 +71,8 @@
 import org.apache.tomcat.util.log.*;
 
 /**
+ * @author Shai Fultheim [[EMAIL PROTECTED]]
  *
- * 
  */
 public final class ServerSessionManager  
 {
@@ -226,4 +227,58 @@
        }
     }
 
+
+       public void serializeSessions(File f) {
+               if (sessions.size() == 0)
+                  return;
+
+               FileOutputStream fStream = null;
+               ObjectOutputStream oStream = null;
+
+               try {
+                       fStream = new FileOutputStream(f);
+                       oStream = new ObjectOutputStream(fStream);
+                       // Serialize to file all active session
+                       Enumeration ids = sessions.keys();
+                       ServerSession session = null;
+                       while (ids.hasMoreElements()) {
+                               session = findSession(ids.nextElement().toString());
+                               if (!session.getTimeStamp().isValid())
+                                  continue;
+                               oStream.writeObject(session);
+                       }
+                       oStream.flush();
+                       fStream.close();
+               } catch (Exception e) {
+                       System.out.println(e);
+               }
+       }
+
+       public void deSerializeSessions(File f)
+       {
+               FileInputStream fStream = null;
+               try {
+                       fStream = new FileInputStream(f);
+                       ObjectInputStream oStream = new ObjectInputStream(fStream);
+                       ServerSession session = null;
+                       do {
+                               session = (ServerSession)oStream.readObject();
+                               if (session != null) {
+                                       session.ssm = this;
+                                       sessions.put(session.getId(), session);
+                               }
+                       } while (session != null);
+               } catch (java.io.FileNotFoundException n) {
+                       ;
+               } catch (java.io.EOFException eof) {
+                       try {
+                               fStream.close();
+                       } catch (Exception e) {
+                               System.out.println(e);
+                       }
+               } catch (Exception e) {
+                       System.out.println(e);
+               }
+               f.delete();
+       }
 }

Reply via email to