You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2022/09/14 09:21:19 UTC

[tomee] branch fault-tolerance updated: CDI Extension to fix SmallRye Fault Tolerance integration

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

jlmonteiro pushed a commit to branch fault-tolerance
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/fault-tolerance by this push:
     new ba1d7ce650 CDI Extension to fix SmallRye Fault Tolerance integration
ba1d7ce650 is described below

commit ba1d7ce65083503bf7e600d8158bab8c0278c51f
Author: Jean-Louis Monteiro <jl...@tomitribe.com>
AuthorDate: Wed Sep 14 11:21:08 2022 +0200

    CDI Extension to fix SmallRye Fault Tolerance integration
---
 .../src/test/resources/arquillian.xml              |  2 +-
 tck/microprofile-tck/fault-tolerance/tck-dev.xml   |  7 +-
 .../MPFaultToleranceCDIExtension.java              | 89 ++++++++++++++++++++++
 .../jakarta.enterprise.inject.spi.Extension        |  2 +
 4 files changed, 98 insertions(+), 2 deletions(-)

diff --git a/tck/microprofile-tck/fault-tolerance/src/test/resources/arquillian.xml b/tck/microprofile-tck/fault-tolerance/src/test/resources/arquillian.xml
index a4f0db9288..da140a628d 100644
--- a/tck/microprofile-tck/fault-tolerance/src/test/resources/arquillian.xml
+++ b/tck/microprofile-tck/fault-tolerance/src/test/resources/arquillian.xml
@@ -26,7 +26,7 @@
       <property name="httpPort">-1</property>
       <property name="ajpPort">-1</property>
       <property name="stopPort">-1</property>
-      <property name="debug">true</property>
+      <property name="debug">false</property>
       <property name="classifier">microprofile</property>
       <property name="conf">src/test/conf</property>
       <property name="dir">target/tomee</property>
diff --git a/tck/microprofile-tck/fault-tolerance/tck-dev.xml b/tck/microprofile-tck/fault-tolerance/tck-dev.xml
index ddd61fb854..2576293803 100644
--- a/tck/microprofile-tck/fault-tolerance/tck-dev.xml
+++ b/tck/microprofile-tck/fault-tolerance/tck-dev.xml
@@ -17,9 +17,14 @@
 <suite name="microprofile-fault-tolerance-TCK" verbose="2" configfailurepolicy="continue" >
 
   <test name="microprofile-fault-tolerance TCK">
+    <packages>
+      <package name="org.eclipse.microprofile.fault.tolerance.tck.*">
+      </package>
+    </packages>
+<!--
     <classes>
       <class name="org.eclipse.microprofile.fault.tolerance.tck.CircuitBreakerLateSuccessTest"></class>
     </classes>
+-->
   </test>
-
 </suite>
\ No newline at end of file
diff --git a/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/faulttolerance/MPFaultToleranceCDIExtension.java b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/faulttolerance/MPFaultToleranceCDIExtension.java
new file mode 100644
index 0000000000..e110a9c584
--- /dev/null
+++ b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/faulttolerance/MPFaultToleranceCDIExtension.java
@@ -0,0 +1,89 @@
+/*
+ * 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.tomee.microprofile.faulttolerance;
+
+import io.smallrye.faulttolerance.FaultToleranceBinding;
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.Annotated;
+import jakarta.enterprise.inject.spi.AnnotatedMethod;
+import jakarta.enterprise.inject.spi.BeanManager;
+import jakarta.enterprise.inject.spi.Extension;
+import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
+import org.eclipse.microprofile.faulttolerance.Asynchronous;
+import org.eclipse.microprofile.faulttolerance.Bulkhead;
+import org.eclipse.microprofile.faulttolerance.CircuitBreaker;
+import org.eclipse.microprofile.faulttolerance.Fallback;
+import org.eclipse.microprofile.faulttolerance.Retry;
+import org.eclipse.microprofile.faulttolerance.Timeout;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * This class is more a hack than an actual peace of integration code for SmallRye Fault Tolerance. It addresses an issue
+ * in SmallRye because it's relying on a Weld behavior as opposed to a specified CDI feature.
+ */
+public class MPFaultToleranceCDIExtension implements Extension {
+
+    private List<Class> faultToleranceAnnotations = Arrays.asList(
+        CircuitBreaker.class,
+        Retry.class,
+        Timeout.class,
+        Asynchronous.class,
+        Fallback.class, // this one can only be on methods
+        Bulkhead.class);
+
+    /**
+     * Observer to a CDI lifecycle event to correctly add the interceptor binding to the actual bean. The SmallRye
+     * extension adds the interceptor binding to the interceptor binding.
+     *
+     * This will go threw classes annotations and add the FaultToleranceBinding on the type if it has one of the known
+     * fault tolerance annotations. In case, fault tolerance annotations are applied on some methods of a class, we
+     * also look for annotations on methods.
+     *
+     * As soon as we find at least one annotation, either on the class or one of the method, we add the interceptor
+     * binding.
+     *
+     * @param pat CDI lifecycle callback payload
+     * @param <X> Type of the Injection to observe
+     */
+    <X> void addFaultToleranceInterceptorBinding(@Observes final ProcessAnnotatedType<X> pat, final BeanManager bm) {
+
+        // check fault tolerance annotations on classes
+        if (hasFaultToleranceAnnotations(pat.getAnnotatedType())) {
+            pat.configureAnnotatedType().add(FaultToleranceBinding.Literal.INSTANCE);
+            return;
+        }
+
+        // if not on the class, it may be per method
+        for (AnnotatedMethod<? super X> m : pat.getAnnotatedType().getMethods()) {
+            if (hasFaultToleranceAnnotations(m)) {
+                pat.configureAnnotatedType().add(FaultToleranceBinding.Literal.INSTANCE);
+                return;
+            }
+        }
+    }
+
+    private boolean hasFaultToleranceAnnotations(final Annotated annotated) {
+        for (Class annotation : faultToleranceAnnotations) {
+            if (annotated.isAnnotationPresent(annotation)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
\ No newline at end of file
diff --git a/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
index aa442ca8da..f8e0f665f8 100644
--- a/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
+++ b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension
@@ -1,4 +1,6 @@
 org.apache.tomee.microprofile.health.MPHealthCDIExtension
 org.apache.tomee.microprofile.metrics.MPMetricsCDIExtension
 org.apache.tomee.microprofile.opentracing.MPOpenTracingCDIExtension
+org.apache.tomee.microprofile.faulttolerance.MPFaultToleranceCDIExtension
+