You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ar...@apache.org on 2021/10/10 11:55:08 UTC

[openwebbeans] 01/01: [OWB-1393] Don't fire ProcessObserverMethod during extension registration

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

arne pushed a commit to branch OWB-1393
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git

commit 910224e8807f27b1957f3a066dc12bc5bd47a9b9
Author: arne <ar...@apache.org>
AuthorDate: Sun Oct 10 12:23:52 2021 +0200

    [OWB-1393] Don't fire ProcessObserverMethod during extension
    registration
---
 .../apache/webbeans/event/NotificationManager.java | 15 +-----
 .../org/apache/webbeans/util/WebBeansUtil.java     |  7 ---
 .../portable/events/ProcessObserverMethodTest.java | 58 ++++++++++++++++++++++
 .../extensions/ProcessObserverMethodExtension.java | 42 ++++++++++++++++
 4 files changed, 102 insertions(+), 20 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java b/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
index 8c22c64..a69d124 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
@@ -51,7 +51,6 @@ import javax.enterprise.event.NotificationOptions;
 import javax.enterprise.event.ObserverException;
 import javax.enterprise.event.TransactionPhase;
 import javax.enterprise.inject.spi.AfterDeploymentValidation;
-import javax.enterprise.inject.spi.AnnotatedCallable;
 import javax.enterprise.inject.spi.AnnotatedConstructor;
 import javax.enterprise.inject.spi.AnnotatedField;
 import javax.enterprise.inject.spi.AnnotatedMethod;
@@ -74,6 +73,7 @@ import javax.enterprise.inject.spi.ProcessSyntheticBean;
 import javax.enterprise.inject.spi.ProcessSyntheticObserverMethod;
 
 import org.apache.webbeans.component.AbstractOwbBean;
+import org.apache.webbeans.component.ExtensionBean;
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
@@ -1162,7 +1162,7 @@ public class NotificationManager
 
         ObserverMethodImpl<T> observer = null;
         // Observer creation from annotated method
-        if (isContainerEvent(annotatedParameter))
+        if (ExtensionBean.class.isInstance(ownerBean))
         {
             observer = new ContainerEventObserverMethodImpl(ownerBean, annotatedMethod, annotatedParameter);
             addObserver(observer);
@@ -1194,17 +1194,6 @@ public class NotificationManager
         return observer;
     }
 
-    public boolean isContainerEvent(AnnotatedParameter<?> annotatedParameter)
-    {
-        AnnotatedCallable<?> method = annotatedParameter.getDeclaringCallable();
-        if (!AnnotatedMethod.class.isInstance(method) || method.getParameters().isEmpty())
-        {
-            return false;
-        }
-        Class<?> paramType = AnnotatedMethod.class.cast(method).getJavaMember().getParameterTypes()[0];
-        return webBeansContext.getWebBeansUtil().isContainerEventType(paramType);
-    }
-
     // for lifecycle parameterized events for now
     private Map<Type, Set<ObserverMethod<?>>> findObservers(final Class<?> type)
     {
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java b/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
index 4ab8c20..991e32c 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
@@ -1632,13 +1632,6 @@ public final class WebBeansUtil
             ProcessSyntheticAnnotatedType.class,
             ProcessSyntheticObserverMethod.class
         };
-    
-    private static final Set<Class> CONTAINER_EVENT_CLASSES_SET = new HashSet<>(Arrays.asList(CONTAINER_EVENT_CLASSES));
-    
-    public boolean isContainerEventType(Class<?> type)
-    {
-        return CONTAINER_EVENT_CLASSES_SET.contains(type);
-    }
             
     public boolean isContainerEventType(Object event)
     {
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/ProcessObserverMethodTest.java b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/ProcessObserverMethodTest.java
new file mode 100644
index 0000000..90ca557
--- /dev/null
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/ProcessObserverMethodTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.webbeans.test.portable.events;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.enterprise.context.spi.Context;
+import javax.enterprise.event.Observes;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.apache.webbeans.test.portable.events.extensions.ProcessObserverMethodExtension;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ProcessObserverMethodTest extends AbstractUnitTest
+{
+
+    @Test
+    public void testProcessObserverMethodIsInvoked()
+    {
+        Collection<String> beanXmls = new ArrayList<String>();
+        
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(MyObserver.class);
+
+        addExtension(new ProcessObserverMethodExtension.BrokenExtension());  
+        addExtension(new ProcessObserverMethodExtension());
+        
+        startContainer(beanClasses, beanXmls);
+        
+        Assert.assertTrue(ProcessObserverMethodExtension.processObserverMethodInvoked);
+        
+        shutDownContainer();
+    }
+
+    public static class MyObserver {
+        public void observe(@Observes Context context) {
+            // just any observer
+        }
+    }
+}
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/extensions/ProcessObserverMethodExtension.java b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/extensions/ProcessObserverMethodExtension.java
new file mode 100644
index 0000000..c0c7eb3
--- /dev/null
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/extensions/ProcessObserverMethodExtension.java
@@ -0,0 +1,42 @@
+/*
+ * 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.webbeans.test.portable.events.extensions;
+
+import javax.enterprise.context.spi.Context;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessObserverMethod;
+
+public class ProcessObserverMethodExtension implements Extension
+{
+    public static boolean processObserverMethodInvoked = false;
+    
+    public void processObserverMethod(@Observes ProcessObserverMethod<?, ?> event)
+    {
+        processObserverMethodInvoked = true;
+    }
+
+    public static class BrokenExtension implements Extension
+    {
+        public void listenToNonLifecycleEvent(@Observes Context context)
+        {
+            // do nothing
+        }
+    }
+}