1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| public class ConfigLoaderServer { public static void load() { ConfigScan.scan(); ServerConfigLoader loader = new ServerConfigLoader(); loader.load(ConfigLoader.Context::new, (context, config) -> { if (config != null) { if (StringUtils.isNotBlank(config.getConfigMode())) { String configMode = config.getConfigMode(); ConfigLoader<?> configLoader = ExtensionLoaderFactory.load(ConfigLoader.class, configMode); log.info("Load the configuration【{}】information...", configMode); configLoader.load(context, (contextAfter, configAfter) -> { log.info("Configuration information: {}", configAfter); }); } } }); } }
public final class ConfigScan { public static void scan() { List<Config> configs = ExtensionLoaderFactory.loadAll(Config.class); for (Config conf : configs) { ConfigEnv.getInstance().registerConfig(conf); } } }
public class ServerConfigLoader implements ConfigLoader<HmilyServer> { private final YamlPropertyLoader propertyLoader = new YamlPropertyLoader();
@Override public void load(final Supplier<Context> context, final LoaderHandler<HmilyServer> handler) { String filePath = System.getProperty("hmily.conf"); File configFile; if (StringUtils.isBlank(filePath)) { String dirPath = getDirGlobal(); configFile = new File(dirPath); if (configFile.exists()) { filePath = dirPath; } else { ClassLoader loader = ConfigLoader.class.getClassLoader(); URL url = loader.getResource("hmily.yml"); if (url != null) { filePath = url.getFile(); configFile = new File(filePath); } else { throw new ConfigException("ConfigLoader:loader config error,error file path:" + filePath); } } } else { configFile = new File(filePath); if (!configFile.exists()) { throw new ConfigException("ConfigLoader:loader config error,error file path:" + filePath); } } try (FileInputStream inputStream = new FileInputStream(configFile)) { List<PropertyKeySource<?>> propertyKeySources = propertyLoader.load(filePath, inputStream); OriginalConfigLoader original = new OriginalConfigLoader(); againLoad(() -> context.get().with(propertyKeySources, original), handler, HmilyServer.class); } catch (IOException e) { throw new ConfigException("ConfigLoader:loader config error,file path:" + filePath); } }
private String getDirGlobal() { String userDir = System.getProperty("user.dir"); String fileName = "hmily.yml"; return String.join(String.valueOf(File.separatorChar), userDir, fileName); } }
public interface ConfigLoader<T extends Config> { default void againLoad(final Supplier<Context> context, final LoaderHandler<T> handler, final Class<T> tClass) { T config = ConfigEnv.getInstance().getConfig(tClass); for (PropertyKeySource<?> propertyKeySource : context.get().getSource()) { ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertyKeySource, PropertyKeyParse.INSTANCE); Binder binder = Binder.of(configPropertySource); T newConfig = binder.bind(config.prefix(), BindData.of(DataType.of(tClass), () -> config)); handler.finish(context, newConfig); } } }
public class OriginalConfigLoader implements ConfigLoader<Config> { @Override public void load(final Supplier<Context> context, final LoaderHandler<Config> handler) { for (PropertyKeySource<?> propertyKeySource : context.get().getSource()) { ConfigPropertySource configPropertySource = new DefaultConfigPropertySource<>(propertyKeySource, PropertyKeyParse.INSTANCE); ConfigEnv.getInstance().stream() .filter(e -> !e.isLoad()) .map(e -> { Config config = getBind(e, configPropertySource); if (config != null) { @SuppressWarnings("unchecked") Map<String, Object> source = (Map<String, Object>) propertyKeySource.getSource(); config.setSource(source); } return config; }).filter(Objects::nonNull).peek(Config::flagLoad) .forEach(e -> handler.finish(context, e)); } }
private Config getBind(final Config config, final ConfigPropertySource configPropertySource) { Binder binder = Binder.of(configPropertySource); return binder.bind(config.prefix(), BindData.of(DataType.of(config.getClass()), () -> config)); } }
|