You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2019/05/15 15:14:50 UTC

[myfaces] branch master updated: deactivatable CDI passivation beans

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2d2e1cf  deactivatable CDI passivation beans
2d2e1cf is described below

commit 2d2e1cf342151e9b21b02044366303a1345c6d3f
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Wed May 15 17:14:41 2019 +0200

    deactivatable CDI passivation beans
---
 .../apache/myfaces/cdi/util/ContextualStorage.java | 10 +++-----
 .../myfaces/cdi/view/ViewScopeBeanHolder.java      | 15 ++++++-----
 .../myfaces/cdi/view/ViewScopeContextImpl.java     | 12 +++------
 .../cdi/view/ViewScopeContextualStorage.java       | 30 ++++++++++++----------
 .../org/apache/myfaces/config/MyfacesConfig.java   | 18 +++++++++++++
 5 files changed, 49 insertions(+), 36 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/cdi/util/ContextualStorage.java b/impl/src/main/java/org/apache/myfaces/cdi/util/ContextualStorage.java
index ba1cb93..7f9503f 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/util/ContextualStorage.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/util/ContextualStorage.java
@@ -62,11 +62,11 @@ public class ContextualStorage implements Serializable
         this.passivationCapable = passivationCapable;
         if (concurrent)
         {
-            contextualInstances = new ConcurrentHashMap<Object, ContextualInstanceInfo<?>>();
+            contextualInstances = new ConcurrentHashMap<>();
         }
         else
         {
-            contextualInstances = new HashMap<Object, ContextualInstanceInfo<?>>();
+            contextualInstances = new HashMap<>();
         }
     }
 
@@ -165,9 +165,7 @@ public class ContextualStorage implements Serializable
         {
             return beanManager.getPassivationCapableBean((String) beanKey);
         }
-        else
-        {
-            return (Contextual<?>) beanKey;
-        }
+
+        return (Contextual<?>) beanKey;
     }
 }
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java
index 7fd1974..49c6054 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java
@@ -32,6 +32,7 @@ import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 import javax.inject.Inject;
 import javax.servlet.ServletContext;
+import org.apache.myfaces.config.MyfacesConfig;
 import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
 import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
 import org.apache.myfaces.context.ExceptionHandlerImpl;
@@ -67,10 +68,9 @@ public class ViewScopeBeanHolder implements Serializable
     @PostConstruct
     public void init()
     {
-        storageMap = new ConcurrentHashMap<String, ViewScopeContextualStorage>();
+        storageMap = new ConcurrentHashMap<>();
         FacesContext facesContext = FacesContext.getCurrentInstance();
-        facesContext.getExternalContext().getSessionMap().put(VIEW_SCOPE_PREFIX_KEY,
-            1);
+        facesContext.getExternalContext().getSessionMap().put(VIEW_SCOPE_PREFIX_KEY, 1);
     }
     
     /**
@@ -81,8 +81,7 @@ public class ViewScopeBeanHolder implements Serializable
      * @param viewScopeId
      * @return 
      */
-    public ViewScopeContextualStorage getContextualStorage(
-        BeanManager beanManager, String viewScopeId)
+    public ViewScopeContextualStorage getContextualStorage(BeanManager beanManager, String viewScopeId)
     {
         ViewScopeContextualStorage contextualStorage = storageMap.get(viewScopeId);
         if (contextualStorage == null)
@@ -91,8 +90,10 @@ public class ViewScopeBeanHolder implements Serializable
             {            
                 contextualStorage = storageMap.get(viewScopeId);
                 if (contextualStorage == null)
-                {            
-                    contextualStorage = new ViewScopeContextualStorage(beanManager);
+                {
+                    MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance();
+                    contextualStorage = new ViewScopeContextualStorage(beanManager,
+                            myfacesConfig.isCdiPassivationSupported());
                     storageMap.put(viewScopeId, contextualStorage);
                 }
             }
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextImpl.java b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextImpl.java
index 0b386a7..2fb43d3 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextImpl.java
@@ -33,6 +33,7 @@ import javax.faces.view.ViewScoped;
 
 import org.apache.myfaces.cdi.util.BeanProvider;
 import org.apache.myfaces.cdi.util.ContextualInstanceInfo;
+import org.apache.myfaces.config.MyfacesConfig;
 import org.apache.myfaces.view.ViewScopeProxyMap;
 
 /**
@@ -49,7 +50,6 @@ public class ViewScopeContextImpl implements Context
      */
     private BeanManager beanManager;
 
-
     public ViewScopeContextImpl(BeanManager beanManager)
     {
         this.beanManager = beanManager;
@@ -60,13 +60,7 @@ public class ViewScopeContextImpl implements Context
     {
         return beanManager;
     }
-    
-    // SPI
-    protected boolean isCheckPassivationCapable()
-    {
-        return true;
-    }
-    
+
     protected ViewScopeBeanHolder getViewScopeBeanHolder()
     {
         return getViewScopeBeanHolder(FacesContext.getCurrentInstance());
@@ -171,7 +165,7 @@ public class ViewScopeContextImpl implements Context
     {
         checkActive();
 
-        if (isCheckPassivationCapable() && !(bean instanceof PassivationCapable))
+        if (!(bean instanceof PassivationCapable) && MyfacesConfig.getCurrentInstance().isCdiPassivationSupported())
         {
             throw new IllegalStateException(bean.toString() +
                     " doesn't implement " + PassivationCapable.class.getName());
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextualStorage.java b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextualStorage.java
index 5e78b53..894e4da 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextualStorage.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextualStorage.java
@@ -47,18 +47,17 @@ public class ViewScopeContextualStorage implements Serializable
     private final Map<String, Object> nameBeanKeyMap;
     
     private transient BeanManager beanManager;
+    private final boolean passivationCapable;
     
     private transient volatile boolean deactivated;
 
-    /**
-     * @param beanManager is needed for serialisation
-     */
-    public ViewScopeContextualStorage(BeanManager beanManager)
+    public ViewScopeContextualStorage(BeanManager beanManager, boolean passivationCapable)
     {
         this.beanManager = beanManager;
-        contextualInstances = new HashMap<Object, ContextualInstanceInfo<?>>();
-        nameBeanKeyMap = new HashMap<String, Object>();
-        deactivated = false;
+        this.contextualInstances = new HashMap<>();
+        this.nameBeanKeyMap = new HashMap<>();
+        this.deactivated = false;
+        this.passivationCapable = passivationCapable;
     }
 
     /**
@@ -111,13 +110,11 @@ public class ViewScopeContextualStorage implements Serializable
      */
     public <T> Object getBeanKey(Contextual<T> bean)
     {
-        // actually a ViewScoped bean MUST implemented PassivationCapable
-        // but it's deactivatable via ViewScopedContextImpl#isCheckPassivationCapable
-        if (bean instanceof PassivationCapable)
+        if (passivationCapable)
         {
             return ((PassivationCapable) bean).getId();
         }
-        return bean.getClass().getName();
+        return bean;
     }
 
     /**
@@ -126,11 +123,16 @@ public class ViewScopeContextualStorage implements Serializable
      */
     public Contextual<?> getBean(FacesContext context, Object beanKey)
     {
-        if (beanManager == null)
+        if (passivationCapable)
         {
-            beanManager = CDIUtils.getBeanManager(context.getExternalContext());
+            if (beanManager == null)
+            {
+                beanManager = CDIUtils.getBeanManager(context.getExternalContext());
+            }
+            return beanManager.getPassivationCapableBean((String) beanKey);
         }
-        return beanManager.getPassivationCapableBean((String) beanKey);
+        
+        return (Contextual<?>) beanKey;
     }
     
     public boolean isActive()
diff --git a/impl/src/main/java/org/apache/myfaces/config/MyfacesConfig.java b/impl/src/main/java/org/apache/myfaces/config/MyfacesConfig.java
index 4b3c2ae..fe9fcae 100755
--- a/impl/src/main/java/org/apache/myfaces/config/MyfacesConfig.java
+++ b/impl/src/main/java/org/apache/myfaces/config/MyfacesConfig.java
@@ -769,6 +769,15 @@ public class MyfacesConfig
             = "org.apache.myfaces.WRAP_TAG_EXCEPTIONS_AS_CONTEXT_AWARE";
     public static final boolean WRAP_TAG_EXCEPTIONS_AS_CONTEXT_AWARE_DEFAULT = true;
     
+    /**
+     * Deactivates the passivation capables of MyFaces CDI scopes.
+     * This is e.g. required on Quarkus.
+     */
+    @JSFWebConfigParam(since="3.0.0" , defaultValue="true", expectedValues="true, false")
+    public static final String CDI_PASSIVATION_SUPPORTED
+            = "org.apache.myfaces.CDI_PASSIVATION_SUPPORTED";
+    public static final boolean CDI_PASSIVATION_SUPPORTED_DEFAULT = true;
+    
     // we need it, applicationImpl not ready probably
     private ProjectStage projectStage = ProjectStage.Production;
     private boolean strictJsf2AllowSlashLibraryName;
@@ -846,6 +855,7 @@ public class MyfacesConfig
     private String faceletsViewSuffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
     private ELExpressionCacheMode elExpressionCacheMode;
     private boolean wrapTagExceptionsAsContextAware = WRAP_TAG_EXCEPTIONS_AS_CONTEXT_AWARE_DEFAULT;
+    private boolean cdiPassivationSupported = CDI_PASSIVATION_SUPPORTED_DEFAULT;
     
     
     private static final boolean MYFACES_IMPL_AVAILABLE;
@@ -1241,6 +1251,9 @@ public class MyfacesConfig
         cfg.wrapTagExceptionsAsContextAware = getBoolean(extCtx, WRAP_TAG_EXCEPTIONS_AS_CONTEXT_AWARE,
                 WRAP_TAG_EXCEPTIONS_AS_CONTEXT_AWARE_DEFAULT);
         
+        cfg.cdiPassivationSupported = getBoolean(extCtx, CDI_PASSIVATION_SUPPORTED,
+                CDI_PASSIVATION_SUPPORTED_DEFAULT);
+        
         return cfg;
     }
 
@@ -1691,5 +1704,10 @@ public class MyfacesConfig
         return wrapTagExceptionsAsContextAware;
     }
 
+    public boolean isCdiPassivationSupported()
+    {
+        return cdiPassivationSupported;
+    }
+    
 }