shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1468749 - in /shiro/branches/1.2.x/core/src: main/java/org/apache/shiro/session/mgt/ test/java/org/apache/shiro/session/mgt/
Date Wed, 17 Apr 2013 04:48:48 GMT
Author: lhazlewood
Date: Wed Apr 17 04:48:48 2013
New Revision: 1468749

URL: http://svn.apache.org/r1468749
Log:
SHIRO-399: applied patch.  Good test case!

Modified:
    shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractNativeSessionManager.java
    shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManager.java
    shiro/branches/1.2.x/core/src/test/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManagerTest.java

Modified: shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractNativeSessionManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractNativeSessionManager.java?rev=1468749&r1=1468748&r2=1468749&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractNativeSessionManager.java
(original)
+++ shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractNativeSessionManager.java
Wed Apr 17 04:48:48 2013
@@ -1,17 +1,20 @@
 /*
- * Copyright 2008 Les Hazlewood
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 package org.apache.shiro.session.mgt;
 
@@ -236,13 +239,16 @@ public abstract class AbstractNativeSess
 
     public void stop(SessionKey key) throws InvalidSessionException {
         Session session = lookupRequiredSession(key);
-        if (log.isDebugEnabled()) {
-            log.debug("Stopping session with id [" + session.getId() + "]");
+        try {
+            if (log.isDebugEnabled()) {
+                log.debug("Stopping session with id [" + session.getId() + "]");
+            }
+            session.stop();
+            onStop(session, key);
+            notifyStop(session);
+        } finally {
+            afterStopped(session);
         }
-        session.stop();
-        onStop(session, key);
-        notifyStop(session);
-        afterStopped(session);
     }
 
     protected void onStop(Session session, SessionKey key) {

Modified: shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManager.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManager.java?rev=1468749&r1=1468748&r2=1468749&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManager.java
(original)
+++ shiro/branches/1.2.x/core/src/main/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManager.java
Wed Apr 17 04:48:48 2013
@@ -152,9 +152,12 @@ public abstract class AbstractValidating
 
     protected void onExpiration(Session s, ExpiredSessionException ese, SessionKey key) {
         log.trace("Session with id [{}] has expired.", s.getId());
-        onExpiration(s);
-        notifyExpiration(s);
-        afterExpired(s);
+        try {
+            onExpiration(s);
+            notifyExpiration(s);
+        } finally {
+            afterExpired(s);
+        }
     }
 
     protected void onExpiration(Session session) {
@@ -170,9 +173,12 @@ public abstract class AbstractValidating
             return;
         }
         log.trace("Session with id [{}] is invalid.", s.getId());
-        onStop(s);
-        notifyStop(s);
-        afterStopped(s);
+        try {
+            onStop(s);
+            notifyStop(s);
+        } finally {
+            afterStopped(s);
+        }
     }
 
     protected void doValidate(Session session) throws InvalidSessionException {

Modified: shiro/branches/1.2.x/core/src/test/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManagerTest.java
URL: http://svn.apache.org/viewvc/shiro/branches/1.2.x/core/src/test/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManagerTest.java?rev=1468749&r1=1468748&r2=1468749&view=diff
==============================================================================
--- shiro/branches/1.2.x/core/src/test/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManagerTest.java
(original)
+++ shiro/branches/1.2.x/core/src/test/java/org/apache/shiro/session/mgt/AbstractValidatingSessionManagerTest.java
Wed Apr 17 04:48:48 2013
@@ -19,17 +19,24 @@
 package org.apache.shiro.session.mgt;
 
 import org.apache.shiro.authz.AuthorizationException;
+import org.apache.shiro.mgt.DefaultSecurityManager;
+import org.apache.shiro.mgt.SecurityManager;
 import org.apache.shiro.session.Session;
 import org.apache.shiro.session.SessionListener;
 import org.apache.shiro.session.SessionListenerAdapter;
 import org.apache.shiro.session.UnknownSessionException;
-
+import org.apache.shiro.util.ThreadContext;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import static org.junit.Assert.assertEquals;
+
 /**
  * Unit tests for the {@link org.apache.shiro.session.mgt.AbstractValidatingSessionManager}
class.
  */
@@ -88,4 +95,49 @@ public class AbstractValidatingSessionMa
         
         assertEquals(1, expirationCount.intValue());
     }
+
+
+    /**
+     * Tests that no memory leak exists on invalid sessions: expired or stopped
+     * Verifies <a href="https://issues.apache.org/jira/browse/SHIRO-399">SHIRO-399</a>.
+     */
+    @Test
+    public void testNoMemoryLeakOnInvalidSessions() throws Exception {
+        ThreadContext.remove();
+        SecurityManager sm = new DefaultSecurityManager();
+        ThreadContext.bind(sm);
+
+        SessionListener sessionListener = new SessionListener() {
+            public void onStart(Session session) {
+                session.setAttribute("I love", "Romania");
+            }
+
+            public void onStop(Session session) {
+                tryToCleanSession(session);
+            }
+
+            public void onExpiration(Session session) {
+                tryToCleanSession(session);
+            }
+
+            private void tryToCleanSession(Session session) {
+                Collection<Object> keys = session.getAttributeKeys();
+                for (Object key : keys) {
+                    session.removeAttribute(key);
+                }
+            }
+        };
+
+        DefaultSessionManager sessionManager = new DefaultSessionManager();
+        sessionManager.setSessionListeners(Arrays.asList(sessionListener));
+
+        Session session = sessionManager.start(null);
+        session.setTimeout(0L);
+
+        sessionManager.getSessionDAO().update(session);
+        assertEquals(1, sessionManager.getActiveSessions().size());
+
+        sessionManager.validateSessions();
+        assertEquals(0, sessionManager.getActiveSessions().size());
+    }
 }



Mime
View raw message