Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out a helper method for parsing only application overrides #708

Merged
merged 9 commits into from
Sep 23, 2020
Merged
85 changes: 85 additions & 0 deletions config/src/main/java/com/typesafe/config/ConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import java.io.File;
import java.io.Reader;
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;

Expand Down Expand Up @@ -1090,6 +1092,89 @@ public static Config parseResourcesAnySyntax(String resourceBasename) {
return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults());
}

/**
* 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.
*/
public static java.util.Optional<Config> 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.
*
* @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.
*/
public static java.util.Optional<Config> parseApplicationReplacement(ClassLoader loader) {
return parseApplicationReplacement(ConfigParseOptions.defaults().setClassLoader(loader));
}

/**
* 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.
*/
public static java.util.Optional<Config> parseApplicationReplacement(ConfigParseOptions parseOptions) {
ensureClassLoader(parseOptions, "parseApplicationReplacement");
ClassLoader loader = parseOptions.getClassLoader();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, oops - I didn't catch this before merging, but ensureClassLoader returns the new options, doesn't modify the original options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I can open a follow up fix PR today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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 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
+ "'; 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 java.util.Optional.of(ConfigFactory.parseResources(loader, resource, overrideOptions));
} else if (file != null) {
return java.util.Optional.of(ConfigFactory.parseFile(new File(file), overrideOptions));
} else {
try {
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);
}
}
}

}

/**
* Parses a string (which should be valid HOCON or JSON by default, or
* the syntax specified in the options otherwise).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.typesafe.config;

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
Expand All @@ -12,51 +8,7 @@
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) {
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 ConfigFactory.parseApplicationReplacement(parseOptions)
.orElseGet(() -> ConfigFactory.parseResourcesAnySyntax("application", parseOptions));
}
}