Improved fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=47774
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 12 Jan 2010 22:36:15 +0000 (22:36 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 12 Jan 2010 22:36:15 +0000 (22:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@898555 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/session/StandardSession.java

index e69ce68..19f0612 100644 (file)
@@ -702,37 +702,55 @@ public class StandardSession
             // Notify interested application event listeners
             // FIXME - Assumes we call listeners in reverse order
             Context context = (Context) manager.getContainer();
-            Object listeners[] = context.getApplicationLifecycleListeners();
-            if (notify && (listeners != null)) {
-                HttpSessionEvent event =
-                    new HttpSessionEvent(getSession());
-                for (int i = 0; i < listeners.length; i++) {
-                    int j = (listeners.length - 1) - i;
-                    if (!(listeners[j] instanceof HttpSessionListener))
-                        continue;
-                    HttpSessionListener listener =
-                        (HttpSessionListener) listeners[j];
-                    try {
-                        fireContainerEvent(context,
-                                           "beforeSessionDestroyed",
-                                           listener);
-                        listener.sessionDestroyed(event);
-                        fireContainerEvent(context,
-                                           "afterSessionDestroyed",
-                                           listener);
-                    } catch (Throwable t) {
+            
+            // The call to expire() may not have been triggered by the webapp.
+            // Make sure the webapp's class loader is set when calling the
+            // listeners
+            ClassLoader oldTccl = null;
+            if (context.getLoader() != null &&
+                    context.getLoader().getClassLoader() != null) {
+                oldTccl = Thread.currentThread().getContextClassLoader();
+                Thread.currentThread().setContextClassLoader(
+                        context.getLoader().getClassLoader());
+            }
+            try {
+                Object listeners[] = context.getApplicationLifecycleListeners();
+                if (notify && (listeners != null)) {
+                    HttpSessionEvent event =
+                        new HttpSessionEvent(getSession());
+                    for (int i = 0; i < listeners.length; i++) {
+                        int j = (listeners.length - 1) - i;
+                        if (!(listeners[j] instanceof HttpSessionListener))
+                            continue;
+                        HttpSessionListener listener =
+                            (HttpSessionListener) listeners[j];
                         try {
                             fireContainerEvent(context,
+                                               "beforeSessionDestroyed",
+                                               listener);
+                            listener.sessionDestroyed(event);
+                            fireContainerEvent(context,
                                                "afterSessionDestroyed",
                                                listener);
-                        } catch (Exception e) {
-                            // Ignore
+                        } catch (Throwable t) {
+                            try {
+                                fireContainerEvent(context,
+                                                   "afterSessionDestroyed",
+                                                   listener);
+                            } catch (Exception e) {
+                                // Ignore
+                            }
+                            manager.getContainer().getLogger().error
+                                (sm.getString("standardSession.sessionEvent"), t);
                         }
-                        manager.getContainer().getLogger().error
-                            (sm.getString("standardSession.sessionEvent"), t);
                     }
                 }
+            } finally {
+                if (oldTccl != null) {
+                    Thread.currentThread().setContextClassLoader(oldTccl);
+                }
             }
+
             if (ACTIVITY_CHECK) {
                 accessCount.set(0);
             }