All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.datasource.strategy.AnnotationDataSourceSwitchStrategyMatcher Maven / Gradle / Ivy

The newest version!
package org.zodiac.datasource.strategy;

import org.zodiac.commons.util.Annotations;
import org.zodiac.datasource.annotation.DataSourceSelector;
import org.zodiac.datasource.annotation.UsePrimaryDataSource;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;

public class AnnotationDataSourceSwitchStrategyMatcher extends CachedDataSourceSwitchStrategyMatcher {

    static final Set ignoreMethod = CollUtil.set(Arrays.asList("toString", "clone", "equals"));

    public AnnotationDataSourceSwitchStrategyMatcher() {
        super();
    }

    @Override
    public Strategy createStrategyIfMatch(Class target, Method method) {
        if (ignoreMethod.contains(method.getName())) {
            return null;
        }
        DataSourceSelector useDataSource = Annotations.findAnnotation(target, method, DataSourceSelector.class);
        UsePrimaryDataSource useDefaultDataSource =
            Annotations.findAnnotation(target, method, UsePrimaryDataSource.class);

        boolean support = useDataSource != null || useDefaultDataSource != null;
        if (support) {
            return new Strategy() {
                @Override
                public boolean isUseDefaultDataSource() {
                    return useDefaultDataSource != null;
                }

                @Override
                public boolean isFallbackDefault() {
                    return useDataSource != null && useDataSource.fallbackPrimary();
                }

                @Override
                public String getDataSourceId() {
                    return useDataSource == null ? null : useDataSource.value();
                }

                @Override
                public String toString() {
                    return "Annotation Strategy(" + (useDataSource != null ? useDataSource : useDefaultDataSource)
                        + ")";
                }

                @Override
                public String getDatabase() {
                    return useDataSource == null ? null
                        : StrUtil.isEmpty(useDataSource.database()) ? null : useDataSource.database();
                }
            };
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy