|
16 | 16 | */ |
17 | 17 | package org.apache.dubbo.spring.boot.actuate.endpoint.condition; |
18 | 18 |
|
| 19 | +import org.apache.dubbo.common.logger.Logger; |
| 20 | +import org.apache.dubbo.common.logger.LoggerFactory; |
| 21 | + |
19 | 22 | import org.springframework.beans.BeanUtils; |
20 | 23 | import org.springframework.context.annotation.Condition; |
21 | 24 | import org.springframework.context.annotation.ConditionContext; |
22 | 25 | import org.springframework.context.annotation.Conditional; |
23 | 26 | import org.springframework.core.type.AnnotatedTypeMetadata; |
24 | 27 | import org.springframework.util.ClassUtils; |
25 | 28 |
|
26 | | -import java.util.stream.Stream; |
27 | | - |
28 | 29 | /** |
29 | 30 | * {@link Conditional} that checks whether or not an endpoint is enabled, which is compatible with |
30 | 31 | * org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition |
|
35 | 36 | */ |
36 | 37 | class CompatibleOnEnabledEndpointCondition implements Condition { |
37 | 38 |
|
38 | | - static String[] CONDITION_CLASS_NAMES = { |
39 | | - "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnAvailableEndpointCondition", // 2.2.0+ |
40 | | - "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition" // [2.0.0 , 2.2.x] |
41 | | - }; |
| 39 | + private static final Logger LOGGER = LoggerFactory.getLogger(CompatibleOnEnabledEndpointCondition.class); |
| 40 | + |
| 41 | + // Spring Boot [2.0.0 , 2.2.x] |
| 42 | + static String CONDITION_CLASS_NAME_OLD = |
| 43 | + "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition"; |
| 44 | + |
| 45 | + // Spring Boot 2.2.0 + |
| 46 | + static String CONDITION_CLASS_NAME_NEW = |
| 47 | + "org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnAvailableEndpointCondition"; |
42 | 48 |
|
43 | 49 |
|
44 | 50 | @Override |
45 | 51 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
46 | 52 | ClassLoader classLoader = context.getClassLoader(); |
47 | | - |
48 | | - Condition condition = Stream.of(CONDITION_CLASS_NAMES) // Iterate class names |
49 | | - .filter(className -> ClassUtils.isPresent(className, classLoader)) // Search class existing or not by name |
50 | | - .findFirst() // Find the first candidate |
51 | | - .map(className -> ClassUtils.resolveClassName(className, classLoader)) // Resolve class name to Class |
52 | | - .filter(Condition.class::isAssignableFrom) // Accept the Condition implementation |
53 | | - .map(BeanUtils::instantiateClass) // Instantiate Class to be instance |
54 | | - .map(Condition.class::cast) // Cast the instance to be Condition one |
55 | | - .orElse(NegativeCondition.INSTANCE); // Or else get a negative condition |
56 | | - |
57 | | - return condition.matches(context, metadata); |
58 | | - } |
59 | | - |
60 | | - private static class NegativeCondition implements Condition { |
61 | | - |
62 | | - static final NegativeCondition INSTANCE = new NegativeCondition(); |
63 | | - |
64 | | - @Override |
65 | | - public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
66 | | - return false; |
| 53 | + if (ClassUtils.isPresent(CONDITION_CLASS_NAME_OLD, classLoader)) { |
| 54 | + Class<?> cls = ClassUtils.resolveClassName(CONDITION_CLASS_NAME_OLD, classLoader); |
| 55 | + if (Condition.class.isAssignableFrom(cls)) { |
| 56 | + Condition condition = Condition.class.cast(BeanUtils.instantiateClass(cls)); |
| 57 | + return condition.matches(context, metadata); |
| 58 | + } |
| 59 | + } |
| 60 | + // Check by org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint |
| 61 | + if (ClassUtils.isPresent(CONDITION_CLASS_NAME_NEW, classLoader)) { |
| 62 | + return true; |
67 | 63 | } |
| 64 | + // No condition class found |
| 65 | + LOGGER.warn(String.format("No condition class found, Dubbo Health Endpoint [%s] will not expose", metadata)); |
| 66 | + return false; |
68 | 67 | } |
69 | 68 | } |
0 commit comments