]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make sure AtmosphereFramework is always properly destroyed (#17581)
authorArtur Signell <artur@vaadin.com>
Fri, 17 Jun 2016 11:21:57 +0000 (14:21 +0300)
committerMarko Gronroos <magi@vaadin.com>
Wed, 13 Jul 2016 15:52:02 +0000 (18:52 +0300)
If servlets have never been initialized, then destroy must happen in the
context destroy method

Change-Id: I9c5188884637be8d1bd2cdf11167e3bdd6081bea

server/src/main/java/com/vaadin/server/communication/JSR356WebsocketInitializer.java

index 6d2843a4fc87055c3afbee1cd2c106d6bebda5b7..fc4dc53714f5abe070989ec0a342916a56ce1234 100644 (file)
@@ -170,6 +170,20 @@ public class JSR356WebsocketInitializer implements ServletContextListener {
         return JSR356WebsocketInitializer.class.getName() + "." + servletName;
     }
 
+    /**
+     * Checks if the given attribute name matches the convention used for
+     * storing AtmosphereFramework references.
+     *
+     * @param attributeName
+     *            the attribute name to check
+     * @return <code>true</code> if the attribute name matches the convention,
+     *         <code>false</code> otherwise
+     */
+    private static boolean isAtmosphereFrameworkAttribute(String attributeName) {
+        return attributeName.startsWith(JSR356WebsocketInitializer.class
+                .getName() + ".");
+    }
+
     /**
      * Tries to determine if the given servlet registration refers to a Vaadin
      * servlet.
@@ -202,7 +216,23 @@ public class JSR356WebsocketInitializer implements ServletContextListener {
 
     @Override
     public void contextDestroyed(ServletContextEvent sce) {
-        // Nothing to do here
+        // Destroy any AtmosphereFramework instance we have initialized.
+        // This must be done here to ensure that we cleanup Atmosphere instances
+        // related to servlets which are never initialized
+        ServletContext servletContext = sce.getServletContext();
+        Enumeration<String> attributeNames = servletContext.getAttributeNames();
+        while (attributeNames.hasMoreElements()) {
+            String attributeName = attributeNames.nextElement();
+            if (isAtmosphereFrameworkAttribute(attributeName)) {
+                Object value = servletContext.getAttribute(attributeName);
+                if (value instanceof AtmosphereFramework) {
+                    // This might result in calling destroy() twice, once from
+                    // here and once from PushRequestHandler but
+                    // AtmosphereFramework.destroy() deals with that
+                    ((AtmosphereFramework) value).destroy();
+                }
+            }
+        }
     }
 
 }