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 2022/12/08 09:50:39 UTC

[myfaces] branch main updated: MYFACES-4506

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 039e0c35a MYFACES-4506
039e0c35a is described below

commit 039e0c35a39035570e861b85932a528a370fc464
Author: tandraschko <ta...@apache.org>
AuthorDate: Thu Dec 8 10:50:33 2022 +0100

    MYFACES-4506
---
 .../myfaces/cdi/view/ViewScopeEventListener.java   | 91 ++++++++++++++++++++++
 .../myfaces/cdi/view/ViewScopeExtension.java       |  3 +
 .../apache/myfaces/config/FacesConfigurator.java   |  8 ++
 .../flow/cdi/FlowScopeContextualStorageHolder.java | 17 ++++
 4 files changed, 119 insertions(+)

diff --git a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeEventListener.java b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeEventListener.java
new file mode 100644
index 000000000..cde0b7eab
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeEventListener.java
@@ -0,0 +1,91 @@
+/*
+ * 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.myfaces.cdi.view;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.context.Destroyed;
+import jakarta.enterprise.context.Initialized;
+import jakarta.enterprise.event.Event;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.faces.component.UIViewRoot;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.event.AbortProcessingException;
+import jakarta.faces.event.PostConstructViewMapEvent;
+import jakarta.faces.event.PreDestroyViewMapEvent;
+import jakarta.faces.event.SystemEvent;
+import jakarta.faces.event.ViewMapListener;
+import jakarta.faces.view.ViewScoped;
+import jakarta.inject.Inject;
+import org.apache.myfaces.cdi.util.CDIUtils;
+import org.apache.myfaces.util.ExternalSpecifications;
+import org.apache.myfaces.util.lang.Lazy;
+
+public class ViewScopeEventListener implements ViewMapListener
+{
+    private Lazy<Bridge> bridge = new Lazy<>(() ->
+    {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        if (ExternalSpecifications.isCDIAvailable(facesContext.getExternalContext()))
+        {
+            BeanManager beanManager = CDIUtils.getBeanManager(facesContext);
+            return CDIUtils.get(beanManager, Bridge.class);
+        }
+        return null;
+    });
+
+    @Override
+    public boolean isListenerForSource(Object source)
+    {
+        return source instanceof UIViewRoot;
+    }
+
+    @Override
+    public void processEvent(SystemEvent event) throws AbortProcessingException
+    {
+        if (bridge.get() != null)
+        {
+            bridge.get().processEvent(event);
+        }
+    }
+
+    @ApplicationScoped
+    public static class Bridge
+    {
+        @Inject
+        @Initialized(ViewScoped.class)
+        private Event<UIViewRoot> viewScopeInitializedEvent;
+
+        @Inject
+        @Destroyed(ViewScoped.class)
+        private Event<UIViewRoot> viewScopeDestroyedEvent;
+
+        public void processEvent(SystemEvent event) throws AbortProcessingException
+        {
+            if (event instanceof PostConstructViewMapEvent)
+            {
+                viewScopeInitializedEvent.fire((UIViewRoot) ((PostConstructViewMapEvent) event).getSource());
+            }
+
+            if (event instanceof PreDestroyViewMapEvent)
+            {
+                viewScopeDestroyedEvent.fire((UIViewRoot) ((PostConstructViewMapEvent) event).getSource());
+            }
+        }
+    }
+}
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeExtension.java b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeExtension.java
index 78e7ab52f..242efada2 100644
--- a/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeExtension.java
+++ b/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeExtension.java
@@ -42,6 +42,9 @@ public class ViewScopeExtension implements Extension
         // can take it into account, and use it later when necessary.
         AnnotatedType bean = beanManager.createAnnotatedType(ViewScopeContextualStorageHolder.class);
         event.addAnnotatedType(bean, bean.getJavaClass().getName());
+
+        bean = beanManager.createAnnotatedType(ViewScopeEventListener.Bridge.class);
+        event.addAnnotatedType(bean, bean.getJavaClass().getName());
     }
     
     void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
diff --git a/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java b/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
index 8fb620bbd..07e3ac506 100755
--- a/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
+++ b/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
@@ -50,6 +50,7 @@ import jakarta.faces.application.ProjectStage;
 import jakarta.faces.application.ResourceHandler;
 import jakarta.faces.application.StateManager;
 import jakarta.faces.application.ViewHandler;
+import jakarta.faces.component.UIViewRoot;
 import jakarta.faces.component.search.SearchExpressionHandler;
 import jakarta.faces.component.search.SearchKeywordResolver;
 import jakarta.faces.context.ExternalContext;
@@ -58,6 +59,8 @@ import jakarta.faces.event.ActionListener;
 import jakarta.faces.event.ComponentSystemEvent;
 import jakarta.faces.event.PhaseListener;
 import jakarta.faces.event.PostConstructApplicationEvent;
+import jakarta.faces.event.PostConstructViewMapEvent;
+import jakarta.faces.event.PreDestroyViewMapEvent;
 import jakarta.faces.event.SystemEvent;
 import jakarta.faces.flow.Flow;
 import jakarta.faces.flow.FlowHandler;
@@ -120,6 +123,7 @@ import org.apache.myfaces.application.viewstate.StateUtils;
 import org.apache.myfaces.util.lang.StringUtils;
 import org.apache.myfaces.util.WebConfigParamUtils;
 import org.apache.myfaces.cdi.util.BeanEntry;
+import org.apache.myfaces.cdi.view.ViewScopeEventListener;
 import org.apache.myfaces.component.search.SearchExpressionContextFactoryImpl;
 import org.apache.myfaces.config.element.Component;
 import org.apache.myfaces.config.element.FaceletsTemplateMapping;
@@ -697,6 +701,10 @@ public class FacesConfigurator
                 log.log(Level.SEVERE, "System event listener could not be initialized, reason:", e);
             }
         }
+        
+        ViewScopeEventListener viewScopeEventListener = new ViewScopeEventListener();
+        application.subscribeToEvent(PostConstructViewMapEvent.class, UIViewRoot.class, viewScopeEventListener);
+        application.subscribeToEvent(PreDestroyViewMapEvent.class, UIViewRoot.class, viewScopeEventListener);
 
         for (Map.Entry<String, Component> entry : dispenser.getComponentsByType().entrySet())
         {
diff --git a/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeContextualStorageHolder.java b/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeContextualStorageHolder.java
index 188bb9a59..dfc1ebfe1 100644
--- a/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeContextualStorageHolder.java
+++ b/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowScopeContextualStorageHolder.java
@@ -26,13 +26,18 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import jakarta.annotation.PostConstruct;
+import jakarta.enterprise.context.Destroyed;
+import jakarta.enterprise.context.Initialized;
 import jakarta.enterprise.context.SessionScoped;
+import jakarta.enterprise.event.Event;
 import jakarta.enterprise.inject.Typed;
 import jakarta.enterprise.inject.spi.BeanManager;
 import jakarta.faces.context.FacesContext;
 import jakarta.faces.flow.Flow;
 import jakarta.faces.flow.FlowHandler;
+import jakarta.faces.flow.FlowScoped;
 import jakarta.faces.lifecycle.ClientWindow;
+import jakarta.inject.Inject;
 import org.apache.myfaces.cdi.util.ContextualInstanceInfo;
 import org.apache.myfaces.cdi.util.ContextualStorage;
 import org.apache.myfaces.config.webparameters.MyfacesConfig;
@@ -53,6 +58,14 @@ public class FlowScopeContextualStorageHolder
         extends AbstractContextualStorageHolder<ContextualStorage>
         implements Serializable
 {
+    @Inject
+    @Initialized(FlowScoped.class)
+    private Event<Flow> flowInitializedEvent;
+
+    @Inject
+    @Destroyed(FlowScoped.class)
+    private Event<Flow> flowDestroyedEvent;
+
     /**
      * key: client window id + flow id
      * value: the {@link ContextualStorage} which holds all the
@@ -173,6 +186,8 @@ public class FlowScopeContextualStorageHolder
         activeFlowKeys.add(0, flowMapKey);
         activeFlowMapKeys.put(baseKey, activeFlowKeys);
         refreshClientWindow(facesContext);
+
+        flowInitializedEvent.fire(flow);
     }
     
     public void destroyCurrentFlowScope(FacesContext facesContext)
@@ -195,6 +210,8 @@ public class FlowScopeContextualStorageHolder
         {
             activeFlowKeys.remove(flowMapKey);
         }
+
+        flowDestroyedEvent.fire(flow);
     }
 
     @Override