olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject olingo-odata4 git commit: [OLINGO-659] Add session invalidation for embedded tomcat
Date Mon, 17 Aug 2015 13:59:58 GMT
Repository: olingo-odata4
Updated Branches:
  refs/heads/master 33478d8cc -> 6f6f5924d


[OLINGO-659] Add session invalidation for embedded tomcat


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/6f6f5924
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/6f6f5924
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/6f6f5924

Branch: refs/heads/master
Commit: 6f6f5924de0e4a457da60c5dc5174794c5c2fbf6
Parents: 33478d8
Author: Michael Bolz <michael.bolz@sap.com>
Authored: Mon Aug 17 15:52:43 2015 +0200
Committer: Michael Bolz <michael.bolz@sap.com>
Committed: Mon Aug 17 15:54:30 2015 +0200

----------------------------------------------------------------------
 .../olingo/fit/server/TomcatTestServer.java     | 51 ++++++++++++++++++++
 .../olingo/fit/AbstractBaseTestITCase.java      | 12 +++--
 2 files changed, 58 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/6f6f5924/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
----------------------------------------------------------------------
diff --git a/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java b/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
index 9fb15f5..69d0314 100644
--- a/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
+++ b/fit/src/main/java/org/apache/olingo/fit/server/TomcatTestServer.java
@@ -23,17 +23,26 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 import java.util.UUID;
 import java.util.logging.FileHandler;
 import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.SimpleFormatter;
 
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
 
 import org.apache.catalina.Context;
 import org.apache.catalina.LifecycleException;
@@ -298,6 +307,7 @@ public class TomcatTestServer {
       if (server != null) {
         return server;
       }
+      baseContext.addApplicationListener(SessionHolder.class.getName());
       tomcat.start();
 
       LOG.info("Started server at endpoint "
@@ -323,4 +333,45 @@ public class TomcatTestServer {
       tomcat.destroy();
     }
   }
+
+  public void invalidateAllSessions() {
+    SessionHolder.invalidateAllSession();
+  }
+
+  public static class SessionHolder implements HttpSessionListener {
+
+    private static final Map<ServletContext, Set<HttpSession>> ALL_SESSIONS =
+            Collections.synchronizedMap(new HashMap<ServletContext, Set<HttpSession>>());
+
+    @Override
+    public void sessionCreated(HttpSessionEvent se) {
+      LOG.info("Created session: {}", se);
+
+      ServletContext c = se.getSession().getServletContext();
+      Set<HttpSession> set = ALL_SESSIONS.get(c);
+      if (set == null) {
+        set = new HashSet<HttpSession>();
+        ALL_SESSIONS.put(c, set);
+      }
+      set.add(se.getSession());
+    }
+
+    @Override
+    public void sessionDestroyed(HttpSessionEvent se) {
+      LOG.info("Destroy session: {}", se);
+    }
+
+    public static void invalidateAllSession() {
+      synchronized (ALL_SESSIONS) {
+        LOG.info("Invalidated sessions...");
+        for (Map.Entry<ServletContext, Set<HttpSession>> e : ALL_SESSIONS.entrySet())
{
+          for (HttpSession s : e.getValue()) {
+            s.invalidate();
+          }
+        }
+        ALL_SESSIONS.clear();
+        LOG.info("...Invalidated all sessions.");
+      }
+    }
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/6f6f5924/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
----------------------------------------------------------------------
diff --git a/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
index 6123bb6..9595573 100644
--- a/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/AbstractBaseTestITCase.java
@@ -40,6 +40,7 @@ import org.apache.olingo.commons.api.format.ContentType;
 import org.apache.olingo.fit.server.TomcatTestServer;
 import org.apache.olingo.server.tecsvc.TechnicalServlet;
 import org.apache.olingo.server.tecsvc.async.TechnicalStatusMonitorServlet;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,11 +53,12 @@ public abstract class AbstractBaseTestITCase {
   protected static final Logger LOG = LoggerFactory.getLogger(AbstractBaseTestITCase.class);
 
   protected abstract ODataClient getClient();
+  private static TomcatTestServer server;
 
   @BeforeClass
   public static void init()
       throws LifecycleException, IOException, InstantiationException, IllegalAccessException,
ClassNotFoundException {
-    TomcatTestServer.init(9080)
+    server = TomcatTestServer.init(9080)
       .addServlet(TechnicalServlet.class, "/odata-server-tecsvc/odata.svc/*")
       .addServlet(TechnicalStatusMonitorServlet.class, "/odata-server-tecsvc/status/*")
       .addServlet(StaticContent.create("org-odata-core-v1.xml"),
@@ -65,10 +67,10 @@ public abstract class AbstractBaseTestITCase {
         .start();
   }
 
-  // @AfterClass
-  // public static void cleanUp() throws LifecycleException {
-  // server.stop();
-  // }
+  @AfterClass
+  public static void cleanUp() throws LifecycleException {
+    server.invalidateAllSessions();
+  }
 
   protected void debugEntity(final Entity entity, final String message) {
     if (LOG.isDebugEnabled()) {


Mime
View raw message