Author: remm
Date: Mon Sep 25 05:53:14 2006
New Revision: 449669

URL: http://svn.apache.org/viewvc?view=rev&rev=449669
Log:
- Fix a design issue: when stop is run, the servlet will have been deallocated 
already (so only basic cleanup may be
  done at this point).

Modified:
    
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/CometConnectionManagerValve.java

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/CometConnectionManagerValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/CometConnectionManagerValve.java?view=diff&rev=449669&r1=449668&r2=449669
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/CometConnectionManagerValve.java
 (original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/CometConnectionManagerValve.java
 Mon Sep 25 05:53:14 2006
@@ -28,7 +28,9 @@
 import javax.servlet.http.HttpSessionListener;
 
 import org.apache.catalina.CometEvent;
+import org.apache.catalina.Context;
 import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.connector.CometEventImpl;
@@ -42,8 +44,7 @@
  * <p>Implementation of a Valve that tracks Comet connections, and closes them
  * when the associated session expires or the webapp is reloaded.</p>
  *
- * <p>This Valve may be attached to any Container, depending on the granularity
- * of the concurrency control you wish to perform.</p>
+ * <p>This Valve should be attached to a Context.</p>
  *
  * @author Remy Maucherat
  * @version $Revision: 386404 $ $Date: 2006-03-16 18:50:37 +0100 (jeu., 16 
mars 2006) $
@@ -51,7 +52,7 @@
 
 public class CometConnectionManagerValve
     extends ValveBase
-    implements Lifecycle, HttpSessionListener {
+    implements Lifecycle, HttpSessionListener, LifecycleListener {
 
 
     // ----------------------------------------------------- Instance Variables
@@ -148,6 +149,10 @@
         lifecycle.fireLifecycleEvent(START_EVENT, null);
         started = true;
 
+        if (container instanceof Context) {
+            ((Lifecycle) container).addLifecycleListener(this);
+        }
+        
     }
 
 
@@ -168,9 +173,16 @@
         lifecycle.fireLifecycleEvent(STOP_EVENT, null);
         started = false;
 
+        if (container instanceof Context) {
+            ((Lifecycle) container).removeLifecycleListener(this);
+        }
+
         // The webapp is getting stopped, so all current connections 
         // should be closed
         // Close all Comet connections associated with this session
+        // Note: this will only be done if the container was not a Context
+        // (otherwise, this needs to be done before stop, as the servlet would
+        // be deallocated already)
         Iterator<ConnectionInfo[]> iterator = connections.values().iterator();
         while (iterator.hasNext()) {
             ConnectionInfo[] connectionInfos = iterator.next();
@@ -178,9 +190,6 @@
                 for (int i = 0; i < connectionInfos.length; i++) {
                     ConnectionInfo connectionInfo = connectionInfos[i];
                     try {
-                        ((CometEventImpl) 
connectionInfo.event).setEventType(CometEvent.EventType.END);
-                        ((CometEventImpl) 
connectionInfo.event).setEventSubType(CometEvent.EventSubType.WEBAPP_RELOAD);
-                        getNext().event(connectionInfo.request, 
connectionInfo.response, connectionInfo.event);
                         connectionInfo.event.close();
                     } catch (Exception e) {
                         
container.getLogger().warn(sm.getString("cometConnectionManagerValve.event"), 
e);
@@ -188,10 +197,38 @@
                 }
             }
         }
+        connections.clear();
 
     }
 
     
+    public void lifecycleEvent(LifecycleEvent event) {
+        if (event.getType() == Lifecycle.BEFORE_STOP_EVENT) {
+            // The webapp is getting stopped, so all current connections 
+            // should be closed
+            // Close all Comet connections associated with this session
+            Iterator<ConnectionInfo[]> iterator = 
connections.values().iterator();
+            while (iterator.hasNext()) {
+                ConnectionInfo[] connectionInfos = iterator.next();
+                if (connectionInfos != null) {
+                    for (int i = 0; i < connectionInfos.length; i++) {
+                        ConnectionInfo connectionInfo = connectionInfos[i];
+                        try {
+                            ((CometEventImpl) 
connectionInfo.event).setEventType(CometEvent.EventType.END);
+                            ((CometEventImpl) 
connectionInfo.event).setEventSubType(CometEvent.EventSubType.WEBAPP_RELOAD);
+                            getNext().event(connectionInfo.request, 
connectionInfo.response, connectionInfo.event);
+                            connectionInfo.event.close();
+                        } catch (Exception e) {
+                            
container.getLogger().warn(sm.getString("cometConnectionManagerValve.event"), 
e);
+                        }
+                    }
+                }
+            }
+            connections.clear();
+        }
+    }
+
+
     // --------------------------------------------------------- Public Methods
 
 
@@ -328,5 +365,6 @@
         public Request request;
         public Response response;
     }
+
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to