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/09 20:56:54 UTC

[isis] branch master updated: ISIS-2158: simplifies servlet context initialization

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 d4290ef  ISIS-2158: simplifies servlet context initialization
d4290ef is described below

commit d4290efee819b91a01d02d17b175a641ed66101a
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jan 9 21:56:40 2020 +0100

    ISIS-2158: simplifies servlet context initialization
---
 .../org/apache/isis/webapp/IsisModuleWebapp.java   |   6 +-
 .../org/apache/isis/webapp/modules/WebModule.java  |   4 +-
 .../isis/webapp/modules/WebModuleContext.java      |   5 +-
 .../webappctx/IsisWebAppContextInitializer.java    |  96 ++++++++++++-----
 .../webappctx/IsisWebAppContextListener.java       | 116 ---------------------
 5 files changed, 77 insertions(+), 150 deletions(-)

diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/IsisModuleWebapp.java b/core/webapp/src/main/java/org/apache/isis/webapp/IsisModuleWebapp.java
index 3300374..d2aebed 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/IsisModuleWebapp.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/IsisModuleWebapp.java
@@ -26,16 +26,12 @@ import org.apache.isis.webapp.health.HealthIndicatorUsingHealthCheckService;
 import org.apache.isis.webapp.modules.logonlog.WebModuleLogOnExceptionLogger;
 import org.apache.isis.webapp.modules.templresources.WebModuleTemplateResources;
 import org.apache.isis.webapp.webappctx.IsisWebAppContextInitializer;
-import org.apache.isis.webapp.webappctx.IsisWebAppContextListener;
 
 @Configuration
 @Import({
         // modules
         IsisModuleRuntime.class,
 
-        // @Configuration's
-        IsisWebAppContextInitializer.class,
-
         // @Service's
         WebModuleLogOnExceptionLogger.class,
         WebModuleTemplateResources.class,
@@ -44,7 +40,7 @@ import org.apache.isis.webapp.webappctx.IsisWebAppContextListener;
         HealthIndicatorUsingHealthCheckService.class,
 
         // (not annotated)
-        IsisWebAppContextListener.class,
+        IsisWebAppContextInitializer.class,
 
 })
 public class IsisModuleWebapp {
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 68a72ce..b7c1f4e 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
@@ -32,14 +32,14 @@ import javax.servlet.annotation.WebListener;
 import org.springframework.core.annotation.Order;
 
 import org.apache.isis.applib.services.registry.ServiceRegistry;
-import org.apache.isis.webapp.webappctx.IsisWebAppContextListener;
+import org.apache.isis.webapp.webappctx.IsisWebAppContextInitializer;
 
 import lombok.val;
 
 /**
  * Introduced to render web.xml Filter/Listener/Servlet configurations obsolete.
  * <p>
- * WebModule instances are used by the {@link IsisWebAppContextListener} to setup 
+ * WebModule instances are used by the {@link IsisWebAppContextInitializer} to setup 
  * the ServletContext programmatically.
  * </p>
  * <p>
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 618bd68..6a6a87f 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
@@ -32,7 +32,6 @@ import org.apache.isis.applib.services.registry.ServiceRegistry;
 import org.apache.isis.commons.internal.base._Strings;
 import org.apache.isis.commons.internal.collections._Arrays;
 import org.apache.isis.config.IsisConfiguration;
-import org.apache.isis.webapp.webappctx.IsisWebAppContextInitializer.ServletContextResource;
 
 import lombok.Getter;
 import lombok.NonNull;
@@ -52,7 +51,7 @@ 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 ServletContext servletContext;
     @NonNull @Getter private final IsisConfiguration configuration;
     @NonNull @Getter private final ServiceRegistry serviceRegistry;
     
@@ -115,7 +114,7 @@ public class WebModuleContext {
 
     public void init() {
 
-        val event = new ServletContextEvent(servletContextResource.getServletContextOneShot());
+        val event = new ServletContextEvent(servletContext);
 
         webModules.stream()
         .filter(module->module.isApplicable(this)) // filter those WebModules that are applicable
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 6f61012..66a70db 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,41 +18,89 @@
  */
 package org.apache.isis.webapp.webappctx;
 
-import javax.inject.Singleton;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletException;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.web.servlet.ServletContextInitializer;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
 
+import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.commons.internal.context._Context;
+import org.apache.isis.config.IsisConfiguration;
+import org.apache.isis.config.viewer.wicket.WebAppContextPath;
+import org.apache.isis.webapp.modules.WebModule;
+import org.apache.isis.webapp.modules.WebModuleContext;
+
+import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
-@Configuration @Log4j2
+/**
+ * 
+ * Introduced to render web.xml Filter/Listener/Servlet configurations obsolete.
+ * <p> 
+ * Acts as the single application entry-point for setting up the 
+ * ServletContext programmatically.
+ * </p><p> 
+ * Installs {@link WebModule}s on the ServletContext. 
+ * </p>   
+ *  
+ * @since 2.0
+ *
+ */
+@Log4j2
 public class IsisWebAppContextInitializer implements ServletContextInitializer {
-
-    // holder of ServletContext with one-shot access 
-    public static class ServletContextResource {
-        private ServletContext servletContext;
-        public ServletContext getServletContextOneShot() {
-            try {
-                return servletContext;    
-            } finally {
-                servletContext = null;
-            }
-        }
-    }
-    
-    @Bean @Singleton
-    public ServletContextResource getServletContextResource() {
-        return servletContextResource;
-    }
     
-    private final ServletContextResource servletContextResource = new ServletContextResource(); 
+    @Autowired private ServiceRegistry serviceRegistry; // this dependency ensures Isis has been initialized/provisioned
+    @Autowired private IsisConfiguration isisConfiguration;
+    @Autowired private WebAppContextPath webAppContextPath;
+
+    // -- INTERFACE IMPLEMENTATION
     
     @Override
     public void onStartup(ServletContext servletContext) throws ServletException {
-        log.info("Memoizing the ServletContext.");
-        servletContextResource.servletContext = servletContext;
+
+        if(!isIsisProvisioned()) {
+            log.error("skipping initialization, Spring should already have provisioned all configured Beans");
+            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.
+        _Context.setDefaultClassLoader(Thread.currentThread().getContextClassLoader(), false);
+
+        val contextPath = servletContext.getContextPath();
+
+        log.info("=== PHASE 1 === Setting up ServletContext parameters, contextPath = " + contextPath);
+
+        webAppContextPath.setContextPath(contextPath);
+
+        val webModuleContext = new WebModuleContext(servletContext, isisConfiguration, serviceRegistry);
+        webModuleContext.prepare();
+
+        _Context.putSingleton(WebModuleContext.class, webModuleContext);
+
+        log.info("=== PHASE 2 === Initializing the ServletContext");
+
+        webModuleContext.init();	
+        log.info("=== DONE === ServletContext initialized.");
+
+    }
+
+    //@Override
+    public void contextDestroyed(ServletContextEvent event) {
+        val webModuleContext = _Context.getIfAny(WebModuleContext.class);
+        if(webModuleContext!=null) {
+            webModuleContext.shutdown(event);
+        }
+    }
+
+    // -- HELPER
+
+    private boolean isIsisProvisioned() {
+        return serviceRegistry!=null;
     }
+
+
+
 }
diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextListener.java b/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextListener.java
deleted file mode 100644
index e40d472..0000000
--- a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextListener.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- *  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.
- */
-package org.apache.isis.webapp.webappctx;
-
-import javax.inject.Inject;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-
-import org.apache.isis.applib.services.registry.ServiceRegistry;
-import org.apache.isis.commons.internal.context._Context;
-import org.apache.isis.config.IsisConfiguration;
-import org.apache.isis.config.viewer.wicket.WebAppContextPath;
-import org.apache.isis.webapp.modules.WebModule;
-import org.apache.isis.webapp.modules.WebModuleContext;
-import org.apache.isis.webapp.webappctx.IsisWebAppContextInitializer.ServletContextResource;
-
-import lombok.val;
-import lombok.extern.log4j.Log4j2;
-
-/**
- * 
- * Introduced to render web.xml Filter/Listener/Servlet configurations obsolete.
- * <p> 
- * Acts as the single application entry-point for setting up the 
- * ServletContext programmatically.
- * </p><p> 
- * Installs {@link WebModule}s on the ServletContext. 
- * </p>   
- *  
- * @since 2.0
- *
- */
-//@WebListener //[ahuber] to support Servlet 3.0 annotations @WebFilter, @WebListener or others 
-//with skinny war deployment requires additional configuration, so for now we disable this annotation
-@Log4j2 //@Singleton
-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;
-    @Inject private WebAppContextPath webAppContextPath;
-
-
-    // -- INTERFACE IMPLEMENTATION
-
-    @Override
-    public void contextInitialized(ServletContextEvent event) {
-
-        if(!isIsisProvisioned()) {
-            log.error("skipping initialization, Spring should already have provisioned all configured Beans");
-            return;
-        }
-//        if(!isSpringContextAvailable()) {
-//            log.error("skipping initialization, SpringContext is required to be initialzed already");
-//            return;
-//        }
-//        if(!isServletContextAvailable()) {
-//            log.error("skipping initialization, a ServletContext is required on the _Context prior to this");
-//            return;
-//        }
-        
-        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 = preliminaryServletContext.getContextPath();
-
-        log.info("=== PHASE 1 === Setting up ServletContext parameters, contextPath = " + contextPath);
-
-        webAppContextPath.setContextPath(contextPath);
-
-        final WebModuleContext webModuleContext = new WebModuleContext(servletContextResource, isisConfiguration, serviceRegistry);
-        webModuleContext.prepare();
-
-        _Context.putSingleton(WebModuleContext.class, webModuleContext);
-
-        log.info("=== PHASE 2 === Initializing the ServletContext");
-
-        webModuleContext.init();	
-        log.info("=== DONE === ServletContext initialized.");
-
-    }
-
-    @Override
-    public void contextDestroyed(ServletContextEvent event) {
-        val webModuleContext = _Context.getIfAny(WebModuleContext.class);
-        if(webModuleContext!=null) {
-            webModuleContext.shutdown(event);
-        }
-    }
-
-    // -- HELPER
-
-    private boolean isIsisProvisioned() {
-        return serviceRegistry!=null;
-    }
-
-}