use "SingletonHolder" pattern rather than synchronized in ConfigImpl

This commit is contained in:
Havoc Pennington 2011-11-27 01:59:04 -05:00
parent f9276c27b5
commit 5a1bd5aa58
2 changed files with 28 additions and 31 deletions

View File

@ -336,29 +336,26 @@ public class ConfigImpl {
}
}
private static ConfigIncluder defaultIncluder = null;
synchronized static ConfigIncluder defaultIncluder() {
if (defaultIncluder == null) {
defaultIncluder = new SimpleIncluder(null);
}
return defaultIncluder;
private static class DefaultIncluderHolder {
static final ConfigIncluder defaultIncluder = new SimpleIncluder(null);
}
private static AbstractConfigObject systemProperties = null;
synchronized static AbstractConfigObject systemPropertiesAsConfigObject() {
if (systemProperties == null) {
systemProperties = loadSystemProperties();
}
return systemProperties;
static ConfigIncluder defaultIncluder() {
return DefaultIncluderHolder.defaultIncluder;
}
private static AbstractConfigObject loadSystemProperties() {
return (AbstractConfigObject) Parseable.newProperties(
System.getProperties(),
ConfigParseOptions.defaults().setOriginDescription(
"system properties")).parse();
return (AbstractConfigObject) Parseable.newProperties(System.getProperties(),
ConfigParseOptions.defaults().setOriginDescription("system properties")).parse();
}
private static class SystemPropertiesHolder {
// this isn't final due to the reloadSystemPropertiesConfig() hack below
static AbstractConfigObject systemProperties = loadSystemProperties();
}
static AbstractConfigObject systemPropertiesAsConfigObject() {
return SystemPropertiesHolder.systemProperties;
}
/** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */
@ -366,18 +363,10 @@ public class ConfigImpl {
return systemPropertiesAsConfigObject().toConfig();
}
// this is a hack to let us set system props in the test suite
synchronized static void dropSystemPropertiesConfig() {
systemProperties = null;
}
private static AbstractConfigObject envVariables = null;
synchronized static AbstractConfigObject envVariablesAsConfigObject() {
if (envVariables == null) {
envVariables = loadEnvVariables();
}
return envVariables;
// this is a hack to let us set system props in the test suite.
// obviously not thread-safe.
static void reloadSystemPropertiesConfig() {
SystemPropertiesHolder.systemProperties = loadSystemProperties();
}
private static AbstractConfigObject loadEnvVariables() {
@ -393,6 +382,14 @@ public class ConfigImpl {
m, ResolveStatus.RESOLVED, false /* ignoresFallbacks */);
}
private static class EnvVariablesHolder {
static final AbstractConfigObject envVariables = loadEnvVariables();
}
static AbstractConfigObject envVariablesAsConfigObject() {
return EnvVariablesHolder.envVariables;
}
/** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */
public static Config envVariablesAsConfig() {
return envVariablesAsConfigObject().toConfig();

View File

@ -257,7 +257,7 @@ class ConfigSubstitutionTest extends TestUtils {
def fallbackToSystemProps() {
System.setProperty("configtest.a", "1234")
System.setProperty("configtest.b", "5678")
ConfigImpl.dropSystemPropertiesConfig()
ConfigImpl.reloadSystemPropertiesConfig()
val resolved = resolve(substSystemPropsObject)