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 2018/08/31 15:31:37 UTC

[isis] 02/09: ISIS-1895: Remove ISIS_SESSION_FACTORY from ServletContext

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

commit 866351031ecd746b5c372200d5085c20bf7b67cb
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Aug 31 15:56:48 2018 +0200

    ISIS-1895: Remove ISIS_SESSION_FACTORY from ServletContext
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-1895
---
 .../isis/core/webapp/IsisWebAppBootstrapper.java   | 28 ++--------------------
 .../isis/core/webapp/IsisWebAppConfigProvider.java | 10 +++++++-
 .../apache/isis/core/webapp/WebAppConstants.java   |  9 +------
 .../auth/AuthenticationSessionStrategyDefault.java |  9 +++----
 .../restfulobjects/server/ResourceContext.java     | 10 ++++----
 .../wicket/viewer/IsisWicketApplication.java       |  5 ----
 6 files changed, 21 insertions(+), 50 deletions(-)

diff --git a/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppBootstrapper.java b/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppBootstrapper.java
index f0f2c67..e93ef38 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppBootstrapper.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppBootstrapper.java
@@ -31,6 +31,7 @@ import com.google.inject.Injector;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import org.apache.isis.commons.internal.context._Context;
 import org.apache.isis.core.commons.config.IsisConfigurationDefault;
 import org.apache.isis.core.commons.config.NotFoundPolicy;
 import org.apache.isis.core.commons.configbuilder.IsisConfigurationBuilder;
@@ -39,7 +40,6 @@ import org.apache.isis.core.runtime.logging.IsisLoggingConfigurer;
 import org.apache.isis.core.runtime.runner.IsisInjectModule;
 import org.apache.isis.core.runtime.system.DeploymentType;
 import org.apache.isis.core.runtime.system.SystemConstants;
-import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactoryBuilder;
 
 /**
@@ -56,18 +56,6 @@ public class IsisWebAppBootstrapper implements ServletContextListener {
 
     private final IsisLoggingConfigurer loggingConfigurer = new IsisLoggingConfigurer();
 
-    @com.google.inject.Inject
-    private IsisSessionFactory isisSessionFactory;
-
-
-    /**
-     * Convenience for servlets that need to obtain the {@link IsisSessionFactoryBuilder}.
-     */
-    public static IsisSessionFactoryBuilder getSystemBoundTo(final ServletContext servletContext) {
-        final Object system = servletContext.getAttribute(WebAppConstants.ISIS_SESSION_FACTORY);
-        return (IsisSessionFactoryBuilder) system;
-    }
-
     // /////////////////////////////////////////////////////
     // Initialization
     // /////////////////////////////////////////////////////
@@ -87,17 +75,12 @@ public class IsisWebAppBootstrapper implements ServletContextListener {
             final DeploymentType deploymentType = determineDeploymentType(servletContext, isisConfigurationBuilder);
             addConfigurationResourcesForDeploymentType(isisConfigurationBuilder, deploymentType);
 
-            final String webappDir = servletContext.getRealPath("/");
-            isisConfigurationBuilder.add(WebAppConstants.WEB_APP_DIR, webappDir);
-
             final IsisConfigurationDefault isisConfiguration = isisConfigurationBuilder.getConfiguration();
             final DeploymentCategory deploymentCategory = deploymentType.getDeploymentCategory();
             final IsisInjectModule isisModule = new IsisInjectModule(deploymentCategory, isisConfiguration);
             final Injector injector = Guice.createInjector(isisModule);
             injector.injectMembers(this);
 
-            servletContext.setAttribute(WebAppConstants.ISIS_SESSION_FACTORY, isisSessionFactory);
-
         } catch (final RuntimeException e) {
             LOG.error("startup failed", e);
             throw e;
@@ -160,16 +143,9 @@ public class IsisWebAppBootstrapper implements ServletContextListener {
     @Override
     public void contextDestroyed(final ServletContextEvent ev) {
         LOG.info("server shutting down");
-        final ServletContext servletContext = ev.getServletContext();
-
         try {
-            final IsisSessionFactory isisSessionFactory = (IsisSessionFactory) servletContext.getAttribute(WebAppConstants.ISIS_SESSION_FACTORY);
-            if (isisSessionFactory != null) {
-                LOG.info("calling system shutdown");
-                isisSessionFactory.destroyServicesAndShutdown();
-            }
+            _Context.clear();
         } finally {
-            servletContext.removeAttribute(WebAppConstants.ISIS_SESSION_FACTORY);
             LOG.info("server shut down");
         }
     }
diff --git a/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppConfigProvider.java b/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppConfigProvider.java
index 5b22087..0e7b8bc 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppConfigProvider.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/webapp/IsisWebAppConfigProvider.java
@@ -77,6 +77,7 @@ public class IsisWebAppConfigProvider {
     protected IsisConfigurationBuilder newIsisConfigurationBuilder(final ServletContext servletContext) {
         IsisConfigurationBuilder isisConfigurationBuilder = new IsisConfigurationBuilder();
         isisConfigurationBuilder.primeWith(new OptionHandlerInitParameters(servletContext));
+        addServletContextConstants(servletContext, isisConfigurationBuilder);
         addResourceStreamSources(servletContext, isisConfigurationBuilder);
         return isisConfigurationBuilder;
     }
@@ -120,11 +121,18 @@ public class IsisWebAppConfigProvider {
     
     // -- HELPER
 
+    private static void addServletContextConstants(
+            final ServletContext servletContext,
+            final IsisConfigurationBuilder isisConfigurationBuilder) {
+        
+        final String webappDir = servletContext.getRealPath("/");
+        isisConfigurationBuilder.add(WebAppConstants.WEB_APP_DIR, webappDir);
+    }
+    
     private static void addResourceStreamSources(
             final ServletContext servletContext,
             final IsisConfigurationBuilder isisConfigurationBuilder) {
 
-
         // will load either from WEB-INF, from the class-path or from config directory.
         final String configLocation = servletContext.getInitParameter(WebAppConstants.CONFIG_DIR_PARAM);
         if ( configLocation != null ) {
diff --git a/core/runtime/src/main/java/org/apache/isis/core/webapp/WebAppConstants.java b/core/runtime/src/main/java/org/apache/isis/core/webapp/WebAppConstants.java
index 3e24bc0..c22d4f4 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/webapp/WebAppConstants.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/webapp/WebAppConstants.java
@@ -22,7 +22,6 @@ package org.apache.isis.core.webapp;
 import org.apache.isis.applib.fixtures.LogonFixture;
 import org.apache.isis.core.commons.authentication.AuthenticationSession;
 import org.apache.isis.core.runtime.system.DeploymentType;
-import org.apache.isis.core.runtime.system.session.IsisSessionFactoryBuilder;
 
 public final class WebAppConstants {
 
@@ -44,12 +43,6 @@ public final class WebAppConstants {
     public static final String CONFIGURATION_BUILDER_KEY = "isis.configurationBuilder";
 
     /**
-     * Key under which the {@link IsisSessionFactoryBuilder} is bound as a servlet context
-     * attribute ( <tt>ServletContext#getAttribute(String)</tt>).
-     */
-    public final static String ISIS_SESSION_FACTORY = WebAppConstants.class.getPackage().getName() + ".isisSessionFactory";
-
-    /**
      * Key under which the {@link AuthenticationSession} is bound as a session
      * attribute ( <tt>HttpSession#getAttribute(String)</tt>).
      */
@@ -69,7 +62,7 @@ public final class WebAppConstants {
 
     /**
      * Servlet context parameter name used to specify the location for property file
-     * overloads. Property files in this location take precendence over property files
+     * overloads. Property files in this location take precedence over property files
      * in other locations. Since the ResourceStreamSourceComposite is being used
      * property files from various locations are merged together. So only overloaded
      * properties (entries in the file) need to present.
diff --git a/core/runtime/src/main/java/org/apache/isis/core/webapp/auth/AuthenticationSessionStrategyDefault.java b/core/runtime/src/main/java/org/apache/isis/core/webapp/auth/AuthenticationSessionStrategyDefault.java
index 9f57a16..1569530 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/webapp/auth/AuthenticationSessionStrategyDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/webapp/auth/AuthenticationSessionStrategyDefault.java
@@ -19,7 +19,6 @@
 
 package org.apache.isis.core.webapp.auth;
 
-import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
@@ -29,6 +28,7 @@ import org.apache.isis.core.commons.authentication.AuthenticationSession;
 import org.apache.isis.core.runtime.authentication.AuthenticationManager;
 import org.apache.isis.core.runtime.authentication.exploration.AuthenticationRequestExploration;
 import org.apache.isis.core.runtime.fixtures.authentication.AuthenticationRequestLogonFixture;
+import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
 import org.apache.isis.core.webapp.WebAppConstants;
 
@@ -70,9 +70,10 @@ public class AuthenticationSessionStrategyDefault extends AuthenticationSessionS
         }
 
         // otherwise, look for LogonFixture and try to authenticate
-        final ServletContext servletContext = getServletContext(httpServletRequest);
-        final IsisSessionFactory sessionFactory = (IsisSessionFactory) servletContext.getAttribute(WebAppConstants.ISIS_SESSION_FACTORY);
-        if (sessionFactory == null) {
+        final IsisSessionFactory sessionFactory;
+        try {
+            sessionFactory = IsisContext.getSessionFactory();
+        } catch (Exception e) {
             // not expected to happen (is set up either by IsisWebAppBootstrapper or in IsisWicketApplication).
             return null;
         }
diff --git a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/ResourceContext.java b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/ResourceContext.java
index a55b45d..b7ae186 100644
--- a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/ResourceContext.java
+++ b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/ResourceContext.java
@@ -23,7 +23,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.HttpHeaders;
@@ -46,9 +45,9 @@ import org.apache.isis.core.metamodel.consent.InteractionInitiatedBy;
 import org.apache.isis.core.metamodel.deployment.DeploymentCategory;
 import org.apache.isis.core.metamodel.services.ServicesInjector;
 import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
+import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
-import org.apache.isis.core.webapp.WebAppConstants;
 import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
 import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
 import org.apache.isis.viewer.restfulobjects.applib.client.RestfulRequest.DomainModel;
@@ -64,7 +63,7 @@ public class ResourceContext implements RendererContext6 {
     private final HttpHeaders httpHeaders;
     private final UriInfo uriInfo;
     private final Request request;
-    private final Providers providers;
+    //not used ... private final Providers providers;
     private final HttpServletRequest httpServletRequest;
     private final HttpServletResponse httpServletResponse;
     private final SecurityContext securityContext;
@@ -102,7 +101,7 @@ public class ResourceContext implements RendererContext6 {
             final InteractionInitiatedBy interactionInitiatedBy) {
 
         this.httpHeaders = httpHeaders;
-        this.providers = providers;
+        //not used ... this.providers = providers;
         this.uriInfo = uriInfo;
         this.request = request;
         this.where = where;
@@ -113,8 +112,7 @@ public class ResourceContext implements RendererContext6 {
 
         this.securityContext = securityContext;
 
-        final ServletContext servletContext = httpServletRequest.getServletContext();
-        final IsisSessionFactory isisSessionFactory = (IsisSessionFactory)servletContext.getAttribute(WebAppConstants.ISIS_SESSION_FACTORY);
+        final IsisSessionFactory isisSessionFactory = IsisContext.getSessionFactory(); 
 
         this.servicesInjector = isisSessionFactory.getServicesInjector();
         this.configuration = isisSessionFactory.getConfiguration();
diff --git a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
index 8a0c9a0..66de39a 100644
--- a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
+++ b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
@@ -89,7 +89,6 @@ import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
 import org.apache.isis.core.runtime.threadpool.ThreadPoolSupport;
 import org.apache.isis.core.webapp.IsisWebAppConfigProvider;
-import org.apache.isis.core.webapp.WebAppConstants;
 import org.apache.isis.schema.utils.ChangesDtoUtils;
 import org.apache.isis.schema.utils.CommandDtoUtils;
 import org.apache.isis.schema.utils.InteractionDtoUtils;
@@ -330,9 +329,6 @@ implements ComponentFactoryRegistryAccessor, PageClassRegistryAccessor, WicketVi
 
             injector.injectMembers(this); // populates this.isisSessionFactory
 
-            getServletContext().setAttribute(WebAppConstants.ISIS_SESSION_FACTORY, this.isisSessionFactory);
-
-
             if (requestCycleListenerForIsis instanceof WebRequestCycleForIsis) {
                 WebRequestCycleForIsis webRequestCycleForIsis = (WebRequestCycleForIsis) requestCycleListenerForIsis;
                 webRequestCycleForIsis.setPageClassRegistry(pageClassRegistry);
@@ -748,7 +744,6 @@ implements ComponentFactoryRegistryAccessor, PageClassRegistryAccessor, WicketVi
             if (isisSessionFactory != null) {
                 isisSessionFactory.destroyServicesAndShutdown();
             }
-            getServletContext().setAttribute(WebAppConstants.ISIS_SESSION_FACTORY, null);
             super.onDestroy();
             IsisContext.clear();
         } catch(final RuntimeException ex) {