001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.spring.boot;
017
018import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
019import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
020import org.springframework.context.annotation.ConditionContext;
021import org.springframework.context.annotation.Conditional;
022import org.springframework.core.Ordered;
023import org.springframework.core.annotation.Order;
024import org.springframework.core.env.*;
025import org.springframework.core.type.AnnotatedTypeMetadata;
026
027import java.lang.annotation.ElementType;
028import java.lang.annotation.Retention;
029import java.lang.annotation.RetentionPolicy;
030import java.lang.annotation.Target;
031import java.util.Iterator;
032
033/**
034 * <p>判断是否有 MyBatis-Flex 的多数据源配置。
035 * <p>如果配置文件中有 MyBatis-Flex 的多数据源配置,就加载 MyBatis-Flex 多数据源自动配置类。
036 *
037 * @author michael
038 */
039@Target({ElementType.TYPE, ElementType.METHOD})
040@Retention(RetentionPolicy.RUNTIME)
041@Conditional(ConditionalOnMybatisFlexDatasource.OnMybatisFlexDataSourceCondition.class)
042public @interface ConditionalOnMybatisFlexDatasource {
043
044    @Order(Ordered.HIGHEST_PRECEDENCE + 40)
045    class OnMybatisFlexDataSourceCondition extends SpringBootCondition {
046
047        @Override
048        public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
049            Environment env = context.getEnvironment();
050            if (env instanceof AbstractEnvironment) {
051                MutablePropertySources propertySources = ((AbstractEnvironment) env).getPropertySources();
052                Iterator<PropertySource<?>> it = propertySources.stream().iterator();
053                while (it.hasNext()) {
054                    PropertySource<?> ps = it.next();
055                    if (ps instanceof EnumerablePropertySource) {
056                        for (String propertyName : ((EnumerablePropertySource<?>) ps).getPropertyNames()) {
057                            if (propertyName.startsWith("mybatis-flex.datasource.")) {
058                                return ConditionOutcome.match();
059                            }
060                        }
061                    }
062                }
063            }
064            return ConditionOutcome.noMatch("'mybatis-flex.datasource' is necessary.");
065        }
066
067    }
068
069}