@Override public Config getConfig() throws IOException { PathMatcher pathMatcher; try { pathMatcher = FileSystems.getDefault().getPathMatcher(inputFilePattern); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Invalid input file pattern: " + inputFilePattern); } try (Stream<Path> pathStream = Files.walk(baseDirectory)) { return pathStream .filter(p -> Files.isRegularFile(p) && pathMatcher.matches(p)) .map(p -> ConfigFactory.parseFile(p.toFile())) .reduce(ConfigFactory.empty(), Config::withFallback) .resolve( ConfigResolveOptions.defaults() .setAllowUnresolved(true) .setUseSystemEnvironment(false) ); } }
/** * Evaluates the {@link ConfigSource}s, {@link #bind(Class) bind}s the * resulting {@link Config}, then evaluates the {@link ConfigSource}s * again. * * <p>The second evaluation takes place to allow sources to utilize * values produced by other config sources.</p> */ public Config load() { ConfigResolveOptions resolveOptions = getResolveOptions(); HashMapBindings bindings = this.bindings; Config config = emptyConfig(); for (int i = 0; i < 2; i++) { config = emptyConfig(); for (ConfigSource source : sources.fromHighestToLowestPrecedence()) { config = config.withFallback(source.load(bindings)); } config = config.resolve(resolveOptions); bindings = bindings.set(Config.class, config); } return config; }
/** * @return All properties that are currently loaded from internal and * external files */ @Override public Map<String, Object> asMap() { return configuration .resolve(ConfigResolveOptions.defaults().setUseSystemEnvironment(true).setAllowUnresolved(true)) .root() .unwrapped(); }
private ConfigResolveOptions getResolveOptions() { Binding<ConfigResolveOptions> resolveOptionsBinding = bindings.get(ConfigResolveOptions.class); if (resolveOptionsBinding.isPresent()) { return resolveOptionsBinding.get(); } else { return ConfigResolveOptions.defaults(); } }
@Override public Config resolve(ConfigResolveOptions options) { return c.resolve(options); }
@Override public Config resolve(ConfigResolveOptions options) { return config.resolve(options); }
@Override public Config resolveWith(Config arg0, ConfigResolveOptions arg1) { return resolveWith(arg0, arg1); }
/** * Some default bindings that most config factories will need. * * <ul> * <li> * {@link ClassLoader} → * {@link Thread#currentThread()}.{@link Thread#getContextClassLoader() * getContextClassLoader()} * </li> * <li> * {@link com.typesafe.config.ConfigParseOptions} → * {@link com.typesafe.config.ConfigParseOptions#defaults()} * </li> * <li> * {@link com.typesafe.config.ConfigResolveOptions} → * {@link com.typesafe.config.ConfigParseOptions#defaults()} * </li> * <li> * {@link com.typesafe.config.Config} → * {@link com.typesafe.config.ConfigFactory#empty() * ConfigFactory.empty()} * </li> * </ul> */ public static Bindings defaultBindings() { return noHashMapBindings() .set( ClassLoader.class, Thread.currentThread().getContextClassLoader() ) .set( ConfigParseOptions.class, ConfigParseOptions.defaults() ) .set( ConfigResolveOptions.class, ConfigResolveOptions.defaults() ) .set( Config.class, emptyConfig() ); }
/** * Binds a {@link ConfigResolveOptions} instance. * * <p>Equivalent to</p> * <pre>{@code * bind(ConfigResolveOptions.class).toInstance(resolveOptions) * }</pre> * * @see #bind(Class) */ public ConfigFactory withResolveOptions(ConfigResolveOptions resolveOptions) { return bind(ConfigResolveOptions.class) .toInstance(checkNotNull(resolveOptions)); }