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 2020/01/10 10:36:07 UTC

[isis] branch master updated: ISIS-2158: removes static reference to WebModuleContext

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

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


The following commit(s) were added to refs/heads/master by this push:
     new c4a6c14  ISIS-2158: removes static reference to WebModuleContext
c4a6c14 is described below

commit c4a6c141ae09f4779c23caeef985a61d6a62e028
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Jan 10 11:35:56 2020 +0100

    ISIS-2158: removes static reference to WebModuleContext
    
    also reinstates the WebModuleContext's contextDestroyed() logic
---
 .../org/apache/isis/webapp/modules/WebModule.java  | 12 +++----
 .../isis/webapp/modules/WebModuleContext.java      | 31 +++++++----------
 .../logonlog/WebModuleLogOnExceptionLogger.java    |  2 +-
 .../webappctx/IsisWebAppContextInitializer.java    | 39 +++++++++++++++-------
 4 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModule.java b/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModule.java
index b7c1f4e..e3caacc 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModule.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModule.java
@@ -19,7 +19,6 @@
 package org.apache.isis.webapp.modules;
 
 import java.util.List;
-import java.util.stream.Stream;
 
 import javax.annotation.Priority;
 import javax.servlet.Filter;
@@ -32,6 +31,7 @@ import javax.servlet.annotation.WebListener;
 import org.springframework.core.annotation.Order;
 
 import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.commons.collections.Can;
 import org.apache.isis.webapp.webappctx.IsisWebAppContextInitializer;
 
 import lombok.val;
@@ -87,14 +87,12 @@ public interface WebModule {
     // -- DISCOVERY 
 
     /**
-     * @return Stream of 'discovered' WebModules, whether applicable or not is not decided here 
+     * @return Stream of 'discovered' WebModules, whether applicable or not is not decided here
+     * @apiNote order of filters is relevant/critical 
      */
-    static Stream<WebModule> discoverWebModules(ServiceRegistry serviceRegistry) {
-
-        // modules are discovered by Spring, order of filters is relevant/critical 
-
+    static Can<WebModule> discoverWebModules(ServiceRegistry serviceRegistry) {
         val webModules = serviceRegistry.select(WebModule.class);
-        return webModules.stream();
+        return webModules;
     }
 
 
diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java b/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
index 6a6a87f..7170172 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/modules/WebModuleContext.java
@@ -20,8 +20,6 @@ package org.apache.isis.webapp.modules;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
@@ -29,8 +27,8 @@ import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
 
 import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.commons.collections.Can;
 import org.apache.isis.commons.internal.base._Strings;
-import org.apache.isis.commons.internal.collections._Arrays;
 import org.apache.isis.config.IsisConfiguration;
 
 import lombok.Getter;
@@ -55,7 +53,7 @@ public class WebModuleContext {
     @NonNull @Getter private final IsisConfiguration configuration;
     @NonNull @Getter private final ServiceRegistry serviceRegistry;
     
-    private List<WebModule> webModules;
+    private Can<WebModule> webModules;
     private final List<ServletContextListener> activeListeners = new ArrayList<>();
 
     /**
@@ -95,21 +93,16 @@ public class WebModuleContext {
     }
 
     /**
-     * Streams the protected path names (<tt>isis.protected</tt> context param)
+     * immutable list of protected path names (<tt>isis.protected</tt> context param)
      */
-    public Stream<String> streamProtectedPaths() {
-        final String list = protectedPath.toString();
-        return _Strings.splitThenStream(list, ",");
-    }
-    public String[] getProtectedPaths() {
-        return streamProtectedPaths().collect(_Arrays.toArray(String.class));
+    public Can<String> getProtectedPaths() {
+        final String listLiteral = protectedPath.toString();
+        return Can.<String>ofStream(_Strings.splitThenStream(listLiteral, ","));
     }
 
     public void prepare() {
-        webModules =
-                WebModule.discoverWebModules(serviceRegistry)
-                .peek(module->module.prepare(this)) // prepare context
-                .collect(Collectors.toList());
+        webModules = WebModule.discoverWebModules(serviceRegistry);
+        webModules.forEach(module->module.prepare(this));
     }
 
     public void init() {
@@ -130,15 +123,15 @@ public class WebModuleContext {
 
     // -- HELPER
 
-    private void addListener(ServletContext context, WebModule module) {
-        log.info(String.format("Setup ServletContext, adding WebModule '%s'", module.getName()));
+    private void addListener(ServletContext servletContext, WebModule webModule) {
+        log.info(String.format("Setup ServletContext, adding WebModule '%s'", webModule.getName()));
         try {
-            final List<ServletContextListener> listeners = module.init(context);
+            final List<ServletContextListener> listeners = webModule.init(servletContext);
             if(listeners != null) {
                 activeListeners.addAll(listeners);
             }
         } catch (ServletException e) {
-            log.error(String.format("Failed to add WebModule '%s' to the ServletContext.", module.getName()), e);
+            log.error(String.format("Failed to add WebModule '%s' to the ServletContext.", webModule.getName()), e);
         }  
     }
 
diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/modules/logonlog/WebModuleLogOnExceptionLogger.java b/core/webapp/src/main/java/org/apache/isis/webapp/modules/logonlog/WebModuleLogOnExceptionLogger.java
index 1db0e0f..d0a4bea 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/modules/logonlog/WebModuleLogOnExceptionLogger.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/modules/logonlog/WebModuleLogOnExceptionLogger.java
@@ -68,7 +68,7 @@ public final class WebModuleLogOnExceptionLogger extends WebModuleAbstract {
                 filterReg.addMappingForUrlPatterns(
                         null,
                         true, // filterReg is forced last
-                        webModuleContext.getProtectedPaths());
+                        webModuleContext.getProtectedPaths().toArray(String.class));
             });
 
         return null; // does not provide a listener
diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java b/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
index 66a70db..c4c504a 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
@@ -18,11 +18,14 @@
  */
 package org.apache.isis.webapp.webappctx;
 
+import java.util.EventListener;
+
+import javax.inject.Inject;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
 
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.web.servlet.ServletContextInitializer;
 
 import org.apache.isis.applib.services.registry.ServiceRegistry;
@@ -32,6 +35,8 @@ import org.apache.isis.config.viewer.wicket.WebAppContextPath;
 import org.apache.isis.webapp.modules.WebModule;
 import org.apache.isis.webapp.modules.WebModuleContext;
 
+import lombok.NonNull;
+import lombok.Value;
 import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
@@ -51,9 +56,9 @@ import lombok.extern.log4j.Log4j2;
 @Log4j2
 public class IsisWebAppContextInitializer implements ServletContextInitializer {
     
-    @Autowired private ServiceRegistry serviceRegistry; // this dependency ensures Isis has been initialized/provisioned
-    @Autowired private IsisConfiguration isisConfiguration;
-    @Autowired private WebAppContextPath webAppContextPath;
+    @Inject private ServiceRegistry serviceRegistry; // this dependency ensures Isis has been initialized/provisioned
+    @Inject private IsisConfiguration isisConfiguration;
+    @Inject private WebAppContextPath webAppContextPath;
 
     // -- INTERFACE IMPLEMENTATION
     
@@ -65,8 +70,8 @@ public class IsisWebAppContextInitializer implements ServletContextInitializer {
             return;
         }
         
-        //[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.
+        // 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();
@@ -78,24 +83,34 @@ public class IsisWebAppContextInitializer implements ServletContextInitializer {
         val webModuleContext = new WebModuleContext(servletContext, isisConfiguration, serviceRegistry);
         webModuleContext.prepare();
 
-        _Context.putSingleton(WebModuleContext.class, webModuleContext);
-
         log.info("=== PHASE 2 === Initializing the ServletContext");
 
-        webModuleContext.init();	
+        webModuleContext.init();
+        servletContext.addListener(new ShutdownHook(webModuleContext));
+        
         log.info("=== DONE === ServletContext initialized.");
 
     }
 
-    //@Override
-    public void contextDestroyed(ServletContextEvent event) {
-        val webModuleContext = _Context.getIfAny(WebModuleContext.class);
+    public void contextDestroyed(WebModuleContext webModuleContext, ServletContextEvent event) {
         if(webModuleContext!=null) {
+            log.info("about to destroy the context");
             webModuleContext.shutdown(event);
         }
+        log.info("context destroyed");
     }
 
     // -- HELPER
+    
+    @Value
+    private class ShutdownHook implements EventListener, ServletContextListener {
+        @NonNull WebModuleContext webModuleContext;
+        
+        @Override
+        public void contextDestroyed(ServletContextEvent sce) {
+            IsisWebAppContextInitializer.this.contextDestroyed(webModuleContext, sce);
+        }
+    }
 
     private boolean isIsisProvisioned() {
         return serviceRegistry!=null;