|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dubbo.spring.boot.actuate.endpoint.condition; |
| 18 | + |
| 19 | +import org.springframework.beans.BeanUtils; |
| 20 | +import org.springframework.context.annotation.Condition; |
| 21 | +import org.springframework.context.annotation.ConditionContext; |
| 22 | +import org.springframework.context.annotation.Conditional; |
| 23 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 24 | +import org.springframework.util.ClassUtils; |
| 25 | + |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +/** |
| 29 | + * {@link Conditional} that checks whether or not an endpoint is enabled, which is compatible with |
| 30 | + * org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition |
| 31 | + * and org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnAvailableEndpointCondition |
| 32 | + * |
| 33 | + * @see CompatibleConditionalOnEnabledEndpoint |
| 34 | + * @since 2.7.7 |
| 35 | + */ |
| 36 | +class CompatibleOnEnabledEndpointCondition implements Condition { |
| 37 | + |
| 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 | + }; |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 46 | + 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; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments