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 2024/07/05 13:50:45 UTC

(myfaces) branch main updated: Faces Spec #1501 - CDI: Allow non-component Faces events observeable by CDI

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 60ae7eba0 Faces Spec #1501 - CDI: Allow non-component Faces events observeable by CDI
60ae7eba0 is described below

commit 60ae7eba0004d0b93391445197c7daafa8340f13
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 5 15:50:36 2024 +0200

    Faces Spec #1501 - CDI: Allow non-component Faces events observeable by CDI
---
 .../conf/META-INF/standard-faces-config-base.xml   | 26 +++++++++
 .../apache/myfaces/cdi/SystemEventBroadcaster.java | 61 ++++++++++++++++++++
 .../apache/myfaces/cdi/ViewEventBroadcaster.java   | 67 ++++++++++++++++++++++
 .../java/org/apache/myfaces/cdi/ViewExtension.java | 33 +++++++++++
 .../jakarta.enterprise.inject.spi.Extension        |  1 +
 5 files changed, 188 insertions(+)

diff --git a/impl/src/main/conf/META-INF/standard-faces-config-base.xml b/impl/src/main/conf/META-INF/standard-faces-config-base.xml
index f3cfc3946..fe561f387 100644
--- a/impl/src/main/conf/META-INF/standard-faces-config-base.xml
+++ b/impl/src/main/conf/META-INF/standard-faces-config-base.xml
@@ -36,6 +36,32 @@
           NOTE that the BeanValidator will be registered programmatically in
           FacesConfigurator.configureApplication() if bean validation is available.
       -->
+
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.SystemEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.ExceptionQueuedEvent</system-event-class>
+      </system-event-listener>
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.SystemEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.PostConstructApplicationEvent</system-event-class>
+      </system-event-listener>
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.SystemEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.PreDestroyApplicationEvent</system-event-class>
+      </system-event-listener>
+
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.ViewEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.PreRenderViewEvent</system-event-class>
+      </system-event-listener>
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.ViewEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.PostConstructViewMapEvent</system-event-class>
+      </system-event-listener>
+      <system-event-listener>
+         <system-event-listener-class>org.apache.myfaces.cdi.ViewEventBroadcaster</system-event-listener-class>
+         <system-event-class>jakarta.faces.event.PreDestroyViewMapEvent</system-event-class>
+      </system-event-listener>
    </application>
    <factory>
       <application-factory>org.apache.myfaces.application.ApplicationFactoryImpl</application-factory>
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/SystemEventBroadcaster.java b/impl/src/main/java/org/apache/myfaces/cdi/SystemEventBroadcaster.java
new file mode 100644
index 000000000..8903896d7
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/cdi/SystemEventBroadcaster.java
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+import jakarta.enterprise.event.Event;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.event.AbortProcessingException;
+import jakarta.faces.event.SystemEvent;
+import jakarta.faces.event.SystemEventListener;
+import org.apache.myfaces.cdi.util.CDIUtils;
+import org.apache.myfaces.util.lang.Lazy;
+
+/**
+ * Broadcasts Faces system events to CDI.
+ */
+public class SystemEventBroadcaster implements SystemEventListener
+{
+    private Lazy<Event<Object>> event = new Lazy<>(() ->
+    {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        BeanManager beanManager = CDIUtils.getBeanManager(facesContext);
+        return beanManager == null ? null : beanManager.getEvent();
+    });
+
+    public SystemEventBroadcaster()
+    {
+    }
+
+    @Override
+    public boolean isListenerForSource(Object source)
+    {
+        return true;
+    }
+
+    @Override
+    public void processEvent(SystemEvent e) throws AbortProcessingException
+    {
+        Event<Object> event = this.event.get();
+        if (event != null)
+        {
+            event.fire(e);
+        }
+    }
+}
\ No newline at end of file
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/ViewEventBroadcaster.java b/impl/src/main/java/org/apache/myfaces/cdi/ViewEventBroadcaster.java
new file mode 100644
index 000000000..7ab27b98c
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/cdi/ViewEventBroadcaster.java
@@ -0,0 +1,67 @@
+/*
+ * 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;
+
+import jakarta.enterprise.event.Event;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.faces.annotation.View;
+import jakarta.faces.component.UIViewRoot;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.event.AbortProcessingException;
+import jakarta.faces.event.SystemEvent;
+import jakarta.faces.event.SystemEventListener;
+import org.apache.myfaces.cdi.util.CDIUtils;
+import org.apache.myfaces.util.lang.Lazy;
+
+public class ViewEventBroadcaster implements SystemEventListener
+{
+    private Lazy<Event<Object>> globalEvent = new Lazy<>(() ->
+    {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        BeanManager beanManager = CDIUtils.getBeanManager(facesContext);
+        return beanManager == null ? null : beanManager.getEvent();
+    });
+
+    public ViewEventBroadcaster()
+    {
+    }
+
+    @Override
+    public boolean isListenerForSource(Object source)
+    {
+        return source instanceof UIViewRoot;
+    }
+
+    @Override
+    public void processEvent(SystemEvent e) throws AbortProcessingException
+    {
+        Event<Object> globalEvent = this.globalEvent.get();
+        if (globalEvent == null)
+        {
+            return;
+        }
+
+        // view-bound event
+        UIViewRoot view = (UIViewRoot) e.getSource();
+        globalEvent.select(View.Literal.of(view.getViewId())).fire(e);
+
+        // this is not needed as the CDI impl automatically handles this above
+        // globalEvent.fire(e);
+    }
+}
\ No newline at end of file
diff --git a/impl/src/main/java/org/apache/myfaces/cdi/ViewExtension.java b/impl/src/main/java/org/apache/myfaces/cdi/ViewExtension.java
new file mode 100644
index 000000000..5a5571133
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/cdi/ViewExtension.java
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
+import jakarta.enterprise.inject.spi.Extension;
+import jakarta.faces.annotation.View;
+
+public class ViewExtension implements Extension
+{
+    void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager beanManager)
+    {
+        event.addQualifier(View.class);
+    }
+}
diff --git a/impl/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension b/impl/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
index 982649d00..e4b1160a9 100644
--- a/impl/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
+++ b/impl/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
@@ -6,6 +6,7 @@ org.apache.myfaces.cdi.managedproperty.ManagedPropertyExtension
 org.apache.myfaces.cdi.model.FacesDataModelExtension
 org.apache.myfaces.cdi.view.ViewScopeExtension
 org.apache.myfaces.cdi.view.ViewTransientScopeExtension
+org.apache.myfaces.cdi.ViewExtension
 org.apache.myfaces.config.annotation.CdiAnnotationProviderExtension
 org.apache.myfaces.push.cdi.PushCDIExtension
 org.apache.myfaces.flow.cdi.FlowScopeExtension