You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/12/16 09:22:05 UTC

[GitHub] [dubbo] gitchenjh commented on a change in pull request #9426: [3.0] dubbo-spring-boot-actuator compatible with Spring Boot Actuator 2.6.x

gitchenjh commented on a change in pull request #9426:
URL: https://github.com/apache/dubbo/pull/9426#discussion_r770356790



##########
File path: dubbo-spring-boot/dubbo-spring-boot-actuator/src/main/java/org/apache/dubbo/spring/boot/actuate/endpoint/condition/CompatibleOnEnabledEndpointCondition.java
##########
@@ -35,35 +36,33 @@
  */
 class CompatibleOnEnabledEndpointCondition implements Condition {
 
-    static String[] CONDITION_CLASS_NAMES = {
-            "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnAvailableEndpointCondition", // 2.2.0+
-            "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition" // [2.0.0 , 2.2.x]
-    };
+    private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleOnEnabledEndpointCondition.class);
+
+    // Spring Boot [2.0.0 , 2.2.x]
+    static String CONDITION_CLASS_NAME_OLD =
+        "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition";
+
+    // Spring Boot 2.2.0 +
+    static String CONDITION_CLASS_NAME_NEW =
+        "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnAvailableEndpointCondition";
 
 
     @Override
     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
         ClassLoader classLoader = context.getClassLoader();
-
-        Condition condition = Stream.of(CONDITION_CLASS_NAMES)                         // Iterate class names
-                .filter(className -> ClassUtils.isPresent(className, classLoader))     // Search class existing or not by name
-                .findFirst()                                                           // Find the first candidate
-                .map(className -> ClassUtils.resolveClassName(className, classLoader)) // Resolve class name to Class
-                .filter(Condition.class::isAssignableFrom)                             // Accept the Condition implementation
-                .map(BeanUtils::instantiateClass)                                      // Instantiate Class to be instance
-                .map(Condition.class::cast)                                            // Cast the instance to be Condition one
-                .orElse(NegativeCondition.INSTANCE);                                   // Or else get a negative condition
-
-        return condition.matches(context, metadata);
-    }
-
-    private static class NegativeCondition implements Condition {
-
-        static final NegativeCondition INSTANCE = new NegativeCondition();
-
-        @Override
-        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
-            return false;
+        if (ClassUtils.isPresent(CONDITION_CLASS_NAME_OLD, classLoader)) {
+            Class<?> cls = ClassUtils.resolveClassName(CONDITION_CLASS_NAME_OLD, classLoader);
+            if (Condition.class.isAssignableFrom(cls)) {
+                Condition condition = Condition.class.cast(BeanUtils.instantiateClass(cls));
+                return condition.matches(context, metadata);
+            }
+        }
+        // Check by org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint
+        if (ClassUtils.isPresent(CONDITION_CLASS_NAME_NEW, classLoader)) {
+            return true;

Review comment:
       That is CompatibleOnEnabledEndpointCondition#matches(context, metadata) return true if OnEnabledEndpointCondition class is not found. 
   But I'm not sure is there a possibility that both OnEnabledEndpointCondition and OnAvailableEndpointCondition class were not found(eg. Spring Boot 1) .  So I let this method return false by default, if OnAvailableEndpointCondition class were found, then return true, and @ConditionalOnAvailableEndpoint should work




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org