From 09e2d57e18aaa59af3308da61cba61df8846073f Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Tue, 22 Sep 2020 15:42:58 -0700 Subject: [PATCH 1/9] Split out a helper method for parsing only application overrides --- .../com/typesafe/config/ConfigFactory.java | 59 ++++++++++++++++++- .../config/DefaultConfigLoadingStrategy.java | 48 ++------------- config/src/main/main.iml | 11 ++++ config/src/test/test.iml | 13 ++++ 4 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 config/src/main/main.iml create mode 100644 config/src/test/test.iml diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 12f9aaec0..8236713cc 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -8,7 +8,7 @@ import java.io.File; import java.io.Reader; -import java.net.URL; +import java.net.*; import java.util.Map; import java.util.Properties; import java.util.concurrent.Callable; @@ -1090,6 +1090,63 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults()); } + /** + * Parse only any application overrides (those specified by config.{resource,file,url}), returning + * an empty Config if no overrides were set. + * @param parseOptions parse options + * @return the parsed configuration + */ + public static Config parseApplicationOverride(ConfigParseOptions parseOptions) { + ClassLoader loader = parseOptions.getClassLoader(); + + if (loader == null) + throw new ConfigException.BugOrBroken( + "ClassLoader should have been set here; bug in ConfigFactory. " + + "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)"); + + int specified = 0; + + // override application.conf with config.file, config.resource, + // config.url if requested. + String resource = System.getProperty("config.resource"); + if (resource != null) + specified += 1; + String file = System.getProperty("config.file"); + if (file != null) + specified += 1; + String url = System.getProperty("config.url"); + if (url != null) + specified += 1; + + if (specified == 0) { + return ConfigImpl.emptyConfig("TODO: what to put here? Should something else be returned?"); + } else if (specified > 1) { + throw new ConfigException.Generic("You set more than one of config.file='" + file + + "', config.url='" + url + "', config.resource='" + resource + + "'; don't know which one to use!"); + } else { + // the override file/url/resource MUST be present or it's an error + ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false); + if (resource != null) { + if (resource.startsWith("/")) + resource = resource.substring(1); + // this deliberately does not parseResourcesAnySyntax; if + // people want that they can use an include statement. + return ConfigFactory.parseResources(loader, resource, overrideOptions); + } else if (file != null) { + return ConfigFactory.parseFile(new File(file), overrideOptions); + } else { + try { + return ConfigFactory.parseURL(new URL(url), overrideOptions); + } catch (MalformedURLException e) { + throw new ConfigException.Generic("Bad URL in config.url system property: '" + + url + "': " + e.getMessage(), e); + } + } + } + + } + /** * Parses a string (which should be valid HOCON or JSON by default, or * the syntax specified in the options otherwise). diff --git a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java index 1063af09d..d655f5db7 100644 --- a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java +++ b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java @@ -1,5 +1,7 @@ package com.typesafe.config; +import com.typesafe.config.impl.*; + import java.io.File; import java.net.MalformedURLException; import java.net.URL; @@ -12,51 +14,11 @@ public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy { @Override public Config parseApplicationConfig(ConfigParseOptions parseOptions) { - ClassLoader loader = parseOptions.getClassLoader(); - if (loader == null) - throw new ConfigException.BugOrBroken( - "ClassLoader should have been set here; bug in ConfigFactory. " - + "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)"); - - int specified = 0; - - // override application.conf with config.file, config.resource, - // config.url if requested. - String resource = System.getProperty("config.resource"); - if (resource != null) - specified += 1; - String file = System.getProperty("config.file"); - if (file != null) - specified += 1; - String url = System.getProperty("config.url"); - if (url != null) - specified += 1; - - if (specified == 0) { + Config overrideConfig = ConfigFactory.parseApplicationOverride(parseOptions); + if (overrideConfig.isEmpty()) { return ConfigFactory.parseResourcesAnySyntax("application", parseOptions); - } else if (specified > 1) { - throw new ConfigException.Generic("You set more than one of config.file='" + file - + "', config.url='" + url + "', config.resource='" + resource - + "'; don't know which one to use!"); } else { - // the override file/url/resource MUST be present or it's an error - ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false); - if (resource != null) { - if (resource.startsWith("/")) - resource = resource.substring(1); - // this deliberately does not parseResourcesAnySyntax; if - // people want that they can use an include statement. - return ConfigFactory.parseResources(loader, resource, overrideOptions); - } else if (file != null) { - return ConfigFactory.parseFile(new File(file), overrideOptions); - } else { - try { - return ConfigFactory.parseURL(new URL(url), overrideOptions); - } catch (MalformedURLException e) { - throw new ConfigException.Generic("Bad URL in config.url system property: '" - + url + "': " + e.getMessage(), e); - } - } + return overrideConfig; } } } diff --git a/config/src/main/main.iml b/config/src/main/main.iml new file mode 100644 index 000000000..908ad4f52 --- /dev/null +++ b/config/src/main/main.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/config/src/test/test.iml b/config/src/test/test.iml new file mode 100644 index 000000000..13a67212c --- /dev/null +++ b/config/src/test/test.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file From af6b7b8a6ae0ee328f7639f3d940236430b1f8de Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 08:50:31 -0700 Subject: [PATCH 2/9] remove unused imports --- .../com/typesafe/config/DefaultConfigLoadingStrategy.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java index d655f5db7..182160208 100644 --- a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java +++ b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java @@ -1,11 +1,5 @@ package com.typesafe.config; -import com.typesafe.config.impl.*; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - /** * Default config loading strategy. Able to load resource, file or URL. * Behavior may be altered by defining one of VM properties From 3be726d9189b66618a276b06d9d58f5513154980 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 08:50:41 -0700 Subject: [PATCH 3/9] rename method --- config/src/main/java/com/typesafe/config/ConfigFactory.java | 4 ++-- .../com/typesafe/config/DefaultConfigLoadingStrategy.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 8236713cc..f2fb0ae48 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -1091,12 +1091,12 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { } /** - * Parse only any application overrides (those specified by config.{resource,file,url}), returning + * Parse only any application replacement (specified by one of config.{resource,file,url}), returning * an empty Config if no overrides were set. * @param parseOptions parse options * @return the parsed configuration */ - public static Config parseApplicationOverride(ConfigParseOptions parseOptions) { + public static Config parseApplicationReplacement(ConfigParseOptions parseOptions) { ClassLoader loader = parseOptions.getClassLoader(); if (loader == null) diff --git a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java index 182160208..dd621a2ac 100644 --- a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java +++ b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java @@ -8,11 +8,11 @@ public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy { @Override public Config parseApplicationConfig(ConfigParseOptions parseOptions) { - Config overrideConfig = ConfigFactory.parseApplicationOverride(parseOptions); - if (overrideConfig.isEmpty()) { + Config applicationReplacement = ConfigFactory.parseApplicationReplacement(parseOptions); + if (applicationReplacement.isEmpty()) { return ConfigFactory.parseResourcesAnySyntax("application", parseOptions); } else { - return overrideConfig; + return applicationReplacement; } } } From 7e1c64c9d4d45eb9898fec8a1964f972a078f9d2 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 08:54:00 -0700 Subject: [PATCH 4/9] don't use wildcard imports --- config/src/main/java/com/typesafe/config/ConfigFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index f2fb0ae48..18963b5cc 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -8,7 +8,8 @@ import java.io.File; import java.io.Reader; -import java.net.*; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; import java.util.Properties; import java.util.concurrent.Callable; From bba56f2e987dd333db5c7e8d7f87ce6ee3d4dc1e Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 09:00:03 -0700 Subject: [PATCH 5/9] return an Optional from parseApplicationReplacement --- .../java/com/typesafe/config/ConfigFactory.java | 14 ++++++++------ .../config/DefaultConfigLoadingStrategy.java | 8 ++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 18963b5cc..e0275effb 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -11,6 +11,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.concurrent.Callable; @@ -1095,9 +1096,10 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { * Parse only any application replacement (specified by one of config.{resource,file,url}), returning * an empty Config if no overrides were set. * @param parseOptions parse options - * @return the parsed configuration + * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} + * if none was specified. */ - public static Config parseApplicationReplacement(ConfigParseOptions parseOptions) { + public static java.util.Optional parseApplicationReplacement(ConfigParseOptions parseOptions) { ClassLoader loader = parseOptions.getClassLoader(); if (loader == null) @@ -1120,7 +1122,7 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions specified += 1; if (specified == 0) { - return ConfigImpl.emptyConfig("TODO: what to put here? Should something else be returned?"); + return java.util.Optional.empty(); } else if (specified > 1) { throw new ConfigException.Generic("You set more than one of config.file='" + file + "', config.url='" + url + "', config.resource='" + resource @@ -1133,12 +1135,12 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions resource = resource.substring(1); // this deliberately does not parseResourcesAnySyntax; if // people want that they can use an include statement. - return ConfigFactory.parseResources(loader, resource, overrideOptions); + return java.util.Optional.of(ConfigFactory.parseResources(loader, resource, overrideOptions)); } else if (file != null) { - return ConfigFactory.parseFile(new File(file), overrideOptions); + return java.util.Optional.of(ConfigFactory.parseFile(new File(file), overrideOptions)); } else { try { - return ConfigFactory.parseURL(new URL(url), overrideOptions); + return java.util.Optional.of(ConfigFactory.parseURL(new URL(url), overrideOptions)); } catch (MalformedURLException e) { throw new ConfigException.Generic("Bad URL in config.url system property: '" + url + "': " + e.getMessage(), e); diff --git a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java index dd621a2ac..317cddc5b 100644 --- a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java +++ b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java @@ -8,11 +8,7 @@ public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy { @Override public Config parseApplicationConfig(ConfigParseOptions parseOptions) { - Config applicationReplacement = ConfigFactory.parseApplicationReplacement(parseOptions); - if (applicationReplacement.isEmpty()) { - return ConfigFactory.parseResourcesAnySyntax("application", parseOptions); - } else { - return applicationReplacement; - } + return ConfigFactory.parseApplicationReplacement(parseOptions) + .orElseGet(() -> ConfigFactory.parseResourcesAnySyntax("application", parseOptions)); } } From 1428ebb894f0813425e278d42ddad56fb7a388e6 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 09:00:33 -0700 Subject: [PATCH 6/9] remove accidentally committed files --- config/src/main/main.iml | 11 ----------- config/src/test/test.iml | 13 ------------- 2 files changed, 24 deletions(-) delete mode 100644 config/src/main/main.iml delete mode 100644 config/src/test/test.iml diff --git a/config/src/main/main.iml b/config/src/main/main.iml deleted file mode 100644 index 908ad4f52..000000000 --- a/config/src/main/main.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/config/src/test/test.iml b/config/src/test/test.iml deleted file mode 100644 index 13a67212c..000000000 --- a/config/src/test/test.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file From 83349ecbfa2a5315d85deb90bfc1c96798dbcd50 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 09:09:48 -0700 Subject: [PATCH 7/9] add parseApplicationReplacement overrides --- .../com/typesafe/config/ConfigFactory.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index e0275effb..fa718e640 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -1095,6 +1095,29 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { /** * Parse only any application replacement (specified by one of config.{resource,file,url}), returning * an empty Config if no overrides were set. + * + * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} + * if none was specified. + */ + public static java.util.Optional parseApplicationReplacement() { + return parseApplicationReplacement(ConfigParseOptions.defaults()); + } + + /** + * Like {@link #parseApplicationReplacement()} but allows you to specify a class loader + * ti yse rather than the current context class loader. + * + * @param loader the class loader + * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} + * if none was specified. + */ + public static java.util.Optional parseApplicationReplacement(ClassLoader loader) { + return parseApplicationReplacement(ConfigParseOptions.defaults().setClassLoader(loader)); + } + + /** + * Like {@link #parseApplicationReplacement()} but allows you to specify parse options. + * * @param parseOptions parse options * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} * if none was specified. From f3baf1dfba4bb11c2fac91631dd3933e916195e6 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 09:11:42 -0700 Subject: [PATCH 8/9] use ensureClassLoader --- config/src/main/java/com/typesafe/config/ConfigFactory.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index fa718e640..d519d0c22 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -1123,13 +1123,9 @@ public static java.util.Optional parseApplicationReplacement(ClassLoader * if none was specified. */ public static java.util.Optional parseApplicationReplacement(ConfigParseOptions parseOptions) { + ensureClassLoader(parseOptions, "parseApplicationReplacement"); ClassLoader loader = parseOptions.getClassLoader(); - if (loader == null) - throw new ConfigException.BugOrBroken( - "ClassLoader should have been set here; bug in ConfigFactory. " - + "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)"); - int specified = 0; // override application.conf with config.file, config.resource, From 5029d9325c7a3f0f26d0aaafa42bee49768ca227 Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 12:22:00 -0700 Subject: [PATCH 9/9] add @since tags --- config/src/main/java/com/typesafe/config/ConfigFactory.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index d519d0c22..92e125fd6 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -1096,6 +1096,8 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { * Parse only any application replacement (specified by one of config.{resource,file,url}), returning * an empty Config if no overrides were set. * + * @since 1.4.1 + * * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} * if none was specified. */ @@ -1107,6 +1109,8 @@ public static java.util.Optional parseApplicationReplacement() { * Like {@link #parseApplicationReplacement()} but allows you to specify a class loader * ti yse rather than the current context class loader. * + * @since 1.4.1 + * * @param loader the class loader * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} * if none was specified. @@ -1118,6 +1122,8 @@ public static java.util.Optional parseApplicationReplacement(ClassLoader /** * Like {@link #parseApplicationReplacement()} but allows you to specify parse options. * + * @since 1.4.1 + * * @param parseOptions parse options * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} * if none was specified.