You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/10/31 14:09:36 UTC

[isis] branch v2 updated: ISIS-2158: don't keep static reference to ServletContext

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new 45e2e00  ISIS-2158: don't keep static reference to ServletContext
45e2e00 is described below

commit 45e2e00388c86552c70878a71367cb9019bd0ab4
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Oct 31 15:09:22 2019 +0100

    ISIS-2158: don't keep static reference to ServletContext
---
 .../isis/webapp/IsisWebAppContextInitializer.java  | 27 ++++++++++++++++++----
 .../isis/webapp/IsisWebAppContextListener.java     | 19 ++++-----------
 .../isis/webapp/modules/WebModuleContext.java      |  9 ++++----
 3 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextInitializer.java b/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextInitializer.java
index 6d25045..fd55d47 100644
--- a/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextInitializer.java
+++ b/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextInitializer.java
@@ -18,22 +18,41 @@
  */
 package org.apache.isis.webapp;
 
+import javax.inject.Singleton;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
 import org.springframework.boot.web.servlet.ServletContextInitializer;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-import org.apache.isis.commons.internal.context._Context;
-
 import lombok.extern.log4j.Log4j2;
 
 @Configuration @Log4j2
 public class IsisWebAppContextInitializer implements ServletContextInitializer {
 
+    // holder of ServletContext with one-shot access 
+    public static class ServletContextResource {
+        private ServletContext servletContext;
+        public ServletContext getServletContextTheRemoveReference() {
+            try {
+                return servletContext;    
+            } finally {
+                servletContext = null;
+            }
+        }
+    }
+    
+    @Bean @Singleton
+    public ServletContextResource getServletContextResource() {
+        return servletContextResource;
+    }
+    
+    private final ServletContextResource servletContextResource = new ServletContextResource(); 
+    
     @Override
     public void onStartup(ServletContext servletContext) throws ServletException {
-        log.info("Storing the ServletContext on Isis' context.");
-        _Context.putSingleton(ServletContext.class, servletContext);
+        log.info("Memoizing the ServletContext.");
+        servletContextResource.servletContext = servletContext;
     }
 }
diff --git a/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextListener.java b/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextListener.java
index eb7cdd2..f3d0087 100644
--- a/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextListener.java
+++ b/core/runtime-web/src/main/java/org/apache/isis/webapp/IsisWebAppContextListener.java
@@ -19,16 +19,14 @@
 package org.apache.isis.webapp;
 
 import javax.inject.Inject;
-import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
-import org.springframework.context.ApplicationContext;
-
 import org.apache.isis.applib.services.registry.ServiceRegistry;
 import org.apache.isis.commons.internal.context._Context;
 import org.apache.isis.commons.internal.resources._Resources;
 import org.apache.isis.config.IsisConfiguration;
+import org.apache.isis.webapp.IsisWebAppContextInitializer.ServletContextResource;
 import org.apache.isis.webapp.modules.WebModule;
 import org.apache.isis.webapp.modules.WebModuleContext;
 
@@ -55,6 +53,7 @@ public class IsisWebAppContextListener implements ServletContextListener {
     
     @Inject private ServiceRegistry serviceRegistry; // this dependency ensures Isis has been initialized/provisioned
     @Inject private IsisConfiguration isisConfiguration;
+    @Inject private ServletContextResource servletContextResource;
 
     // -- INTERFACE IMPLEMENTATION
 
@@ -74,19 +73,19 @@ public class IsisWebAppContextListener implements ServletContextListener {
 //            return;
 //        }
         
-        val servletContext = event.getServletContext();
+        val preliminaryServletContext = event.getServletContext();
 
         //[ahuber] set the ServletContext initializing thread as preliminary default until overridden by
         // IsisWicketApplication#init() or others that better know what ClassLoader to use as application default.
         _Context.setDefaultClassLoader(Thread.currentThread().getContextClassLoader(), false);
 
-        val contextPath = servletContext.getContextPath();
+        val contextPath = preliminaryServletContext.getContextPath();
 
         log.info("=== PHASE 1 === Setting up ServletContext parameters, contextPath = " + contextPath);
 
         _Resources.putContextPathIfPresent(contextPath);
 
-        final WebModuleContext webModuleContext = new WebModuleContext(isisConfiguration, serviceRegistry);
+        final WebModuleContext webModuleContext = new WebModuleContext(servletContextResource, isisConfiguration, serviceRegistry);
         webModuleContext.prepare();
 
         _Context.putSingleton(WebModuleContext.class, webModuleContext);
@@ -112,12 +111,4 @@ public class IsisWebAppContextListener implements ServletContextListener {
         return serviceRegistry!=null;
     }
 
-//    private static boolean isSpringContextAvailable() {
-//        return _Context.getIfAny(ApplicationContext.class)!=null;
-//    }
-
-//    private static boolean isServletContextAvailable() {
-//        return _Context.getIfAny(ServletContext.class)!=null;
-//    }
-
 }
diff --git a/core/runtime-web/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java b/core/runtime-web/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
index a76b692..ad6dd57 100644
--- a/core/runtime-web/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
+++ b/core/runtime-web/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
@@ -30,8 +30,8 @@ import javax.servlet.ServletException;
 
 import org.apache.isis.applib.services.registry.ServiceRegistry;
 import org.apache.isis.commons.internal.base._Strings;
-import org.apache.isis.commons.internal.context._Context;
 import org.apache.isis.config.IsisConfiguration;
+import org.apache.isis.webapp.IsisWebAppContextInitializer.ServletContextResource;
 
 import static org.apache.isis.commons.internal.base._With.acceptIfPresent;
 
@@ -53,14 +53,15 @@ public class WebModuleContext {
     private final StringBuilder viewers = new StringBuilder();
     private final StringBuilder protectedPath = new StringBuilder();
 
+    @NonNull @Getter private final ServletContextResource servletContextResource;
     @NonNull @Getter private final IsisConfiguration configuration;
     @NonNull @Getter private final ServiceRegistry serviceRegistry;
     
     private List<WebModule> webModules;
     private final List<ServletContextListener> activeListeners = new ArrayList<>();
 
-    public ServletContext getServletContext() {
-        return _Context.getElseFail(ServletContext.class);
+    public ServletContext getServletContextTheRemoveReference() {
+        return servletContextResource.getServletContextTheRemoveReference();
     }
 
     /**
@@ -117,7 +118,7 @@ public class WebModuleContext {
 
     public void init() {
 
-        val event = new ServletContextEvent(getServletContext());
+        val event = new ServletContextEvent(getServletContextTheRemoveReference());
 
         webModules.stream()
         .filter(module->module.isApplicable(this)) // filter those WebModules that are applicable