001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (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 io.jboot.utils;
017
018
019import io.jboot.app.config.JbootConfigKit;
020import io.jboot.app.config.JbootConfigManager;
021import io.jboot.app.config.annotation.ConfigModel;
022import org.apache.commons.lang3.StringUtils;
023
024import java.util.*;
025
026
027/**
028 * @author michael yang (fuhai999@gmail.com)
029 * @Date: 2020/3/19
030 */
031public class ConfigUtil {
032
033    public static <T> Map<String, T> getConfigModels(Class<T> tClass) {
034        ConfigModel configModel = tClass.getAnnotation(ConfigModel.class);
035        if (configModel == null) {
036            throw new IllegalStateException("The Class \"" + tClass.getName() + "\" is not have @ConfigModel annotation");
037        }
038        return getConfigModels(tClass, configModel.prefix());
039    }
040
041
042    public static <T> Map<String, T> getConfigModels(Class<T> tClass, String prefix) {
043        return getConfigModels(JbootConfigManager.me(), tClass, prefix);
044    }
045
046
047    public static <T> Map<String, T> getConfigModels(JbootConfigManager configManager, Class<T> tClass) {
048        ConfigModel configModel = tClass.getAnnotation(ConfigModel.class);
049        if (configModel == null) {
050            throw new IllegalStateException("The Class \"" + tClass.getName() + "\" is not have @ConfigModel annotation");
051        }
052        return getConfigModels(configManager, tClass, configModel.prefix());
053    }
054
055
056    public static <T> Map<String, T> getConfigModels(JbootConfigManager configManager, Class<T> tClass, String prefix) {
057        Map<String, T> objMap = new HashMap<>();
058
059        boolean initDefault = false;
060
061        Set<String> objNames = new HashSet<>();
062
063        String objPrefix = prefix + ".";
064        int pointCount = StringUtils.countMatches(prefix, '.');
065
066        Properties prop = configManager.getProperties();
067
068        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
069            if (entry.getKey() == null || JbootConfigKit.isBlank(entry.getKey().toString())) {
070                continue;
071            }
072
073            String key = entry.getKey().toString().trim();
074
075            //配置来源于 Docker 的环境变量配置
076            if (key.contains("_") && Character.isUpperCase(key.charAt(0))) {
077                key = key.toLowerCase().replace('_', '.');
078            }
079
080            //初始化默认的配置
081            if (!initDefault && key.startsWith(prefix) && StringUtils.countMatches(key, '.') == pointCount + 1) {
082                initDefault = true;
083                T defaultObj = configManager.get(tClass, prefix, null);
084                objMap.put("default", defaultObj);
085
086            }
087
088            if (key.startsWith(objPrefix) && entry.getValue() != null) {
089                String[] keySplits = key.split("\\.");
090                if (keySplits.length == pointCount + 3) {
091                    objNames.add(keySplits[pointCount + 1]);
092                }
093            }
094        }
095
096        for (String name : objNames) {
097            T obj = configManager.get(tClass, objPrefix + name, null);
098            objMap.put(name, obj);
099        }
100
101        return objMap;
102    }
103}