myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wtl...@apache.org
Subject [myfaces] branch 2.2.x updated: MYFACES-4353 save HttpSession for ViewScope cleanup
Date Mon, 17 Aug 2020 14:43:56 GMT
This is an automated email from the ASF dual-hosted git repository.

wtlucy pushed a commit to branch 2.2.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/2.2.x by this push:
     new fd94377  MYFACES-4353 save HttpSession for ViewScope cleanup
     new 14fd662  Merge pull request #116 from wtlucy/4353_viewscope_predestroy_2.2
fd94377 is described below

commit fd943779641e3d8290f0b421932f31db7fe6e113
Author: Bill Lucy <wtlucy@gmail.com>
AuthorDate: Wed Jul 15 13:53:03 2020 -0400

    MYFACES-4353 save HttpSession for ViewScope cleanup
---
 .../servlet/StartupServletExternalContextImpl.java | 48 +++++++++++++++++++---
 .../webapp/ManagedBeanDestroyerListener.java       |  1 +
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
b/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
index 1ea1bec..9132f1b 100644
--- a/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
@@ -23,6 +23,8 @@ import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.security.Principal;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -30,6 +32,7 @@ import java.util.Map;
 
 import javax.faces.context.Flash;
 import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
 
 /**
  * An ExternalContext implementation for Servlet environments, which is used
@@ -45,14 +48,26 @@ public class StartupServletExternalContextImpl extends ServletExternalContextImp
     public static final String EXCEPTION_TEXT = "This method is not supported during ";
     
     private boolean _startup;
-    
+    private ServletContext _servletContext;
+    private HttpSession _session;
+
     public StartupServletExternalContextImpl(final ServletContext servletContext,
             boolean startup)
     {
         super(servletContext);
         _startup = startup;
     }
-    
+
+    /**
+     * Allow an HttpSession to be saved for later calls to getSession*
+     *
+     * @param session
+     */
+    public void setSession(HttpSession session)
+    {
+        _session = session;
+    }
+
     // ~ Methods which are valid to be called during startup and shutdown------
     
     // Note that all methods, which are valid to be called during startup and
@@ -182,7 +197,11 @@ public class StartupServletExternalContextImpl extends ServletExternalContextImp
     @Override
     public Object getSession(boolean create)
     {
-        if (create)
+        if (_session != null)
+        {
+            return _session;
+        }
+        else if (create)
         {
             throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
         }
@@ -192,7 +211,11 @@ public class StartupServletExternalContextImpl extends ServletExternalContextImp
     @Override
     public String getSessionId(boolean create)
     {
-        if (create)
+        if (_session != null)
+        {
+            return _session.getId();
+        }
+        else if (create)
         {
             throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
         }
@@ -202,7 +225,22 @@ public class StartupServletExternalContextImpl extends ServletExternalContextImp
     @Override
     public Map<String, Object> getSessionMap()
     {
-        throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
+        if (_session != null)
+        {
+            Map<String, Object> sessionMap = new HashMap<String, Object>();
+            Enumeration<String> attributes = _session.getAttributeNames();
+            while (attributes.hasMoreElements())
+            {
+                String name = attributes.nextElement();
+                Object value = _session.getAttribute(name);
+                sessionMap.put(name, value);
+            }
+            return sessionMap;
+        }
+        else
+        {
+            throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
+        }
     }
 
     @Override
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/ManagedBeanDestroyerListener.java
b/impl/src/main/java/org/apache/myfaces/webapp/ManagedBeanDestroyerListener.java
index 7a1e0dd..b4521ef 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/ManagedBeanDestroyerListener.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/ManagedBeanDestroyerListener.java
@@ -166,6 +166,7 @@ public class ManagedBeanDestroyerListener implements
                 {
                     ServletContext servletContext = event.getSession().getServletContext();
                     ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext,
false);
+                    ((StartupServletExternalContextImpl)externalContext).setSession(event.getSession());
                     ExceptionHandler exceptionHandler = new ExceptionHandlerImpl();
                     facesContext = new StartupFacesContextImpl(externalContext, 
                             (ReleaseableExternalContext) externalContext, exceptionHandler,
false);


Mime
View raw message