You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2016/02/16 02:37:31 UTC

svn commit: r1730627 - /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java

Author: lu4242
Date: Tue Feb 16 01:37:31 2016
New Revision: 1730627

URL: http://svn.apache.org/viewvc?rev=1730627&view=rev
Log:
MYFACES-3987 NPE in FlashImpl.isKeepMessages (fix NPE getting view map)

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java?rev=1730627&r1=1730626&r2=1730627&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewScopeProxyMap.java Tue Feb 16 01:37:31 2016
@@ -19,6 +19,7 @@
 package org.apache.myfaces.view;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import javax.faces.component.StateHolder;
@@ -66,19 +67,28 @@ public class ViewScopeProxyMap implement
         {
             FacesContext facesContext = FacesContext.getCurrentInstance();
             
-            ViewScopeProviderFactory factory = ViewScopeProviderFactory.getViewScopeHandlerFactory(
-                facesContext.getExternalContext());
-            
-            ViewScopeProvider handler = factory.getViewScopeHandler(facesContext.getExternalContext());
-            
-            if (_viewScopeId == null)
+            if (facesContext != null)
             {
-                _viewScopeId = handler.generateViewScopeId(facesContext);
-                _delegate = handler.createViewScopeMap(facesContext, _viewScopeId);
+                ViewScopeProviderFactory factory = ViewScopeProviderFactory.getViewScopeHandlerFactory(
+                    facesContext.getExternalContext());
+
+                ViewScopeProvider handler = factory.getViewScopeHandler(facesContext.getExternalContext());
+
+                if (_viewScopeId == null)
+                {
+                    _viewScopeId = handler.generateViewScopeId(facesContext);
+                    _delegate = handler.createViewScopeMap(facesContext, _viewScopeId);
+                }
+                else
+                {
+                    _delegate = handler.restoreViewScopeMap(facesContext, _viewScopeId);
+                }
             }
             else
             {
-                _delegate = handler.restoreViewScopeMap(facesContext, _viewScopeId);
+                // In junit test cases, where there is no facesContext instance, it is enough to
+                // just get a blank instance.
+                _delegate = new ViewScope();
             }
         }
         return _delegate;
@@ -171,5 +181,27 @@ public class ViewScopeProxyMap implement
     public void setTransient(boolean newTransientValue)
     {
     }
+    
+    private static class ViewScope extends HashMap<String, Object>
+    {
+        
+        private static final long serialVersionUID = -1088293802269478164L;
+        
+        @Override
+        public void clear()
+        {
+            /*
+             * The returned Map must be implemented such that calling clear() on the Map causes
+             * Application.publishEvent(java.lang.Class, java.lang.Object) to be called, passing
+             * ViewMapDestroyedEvent.class as the first argument and this UIViewRoot instance as the second argument.
+             */
+            FacesContext facesContext = FacesContext.getCurrentInstance();
+            facesContext.getApplication().publishEvent(facesContext, 
+                    PreDestroyViewMapEvent.class, facesContext.getViewRoot());
+            
+            super.clear();
+        }
+        
+    }
 
 }