From 5a1bd5aa58163b9eae1fe4d658fddda042b31cc6 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Sun, 27 Nov 2011 01:59:04 -0500 Subject: [PATCH] use "SingletonHolder" pattern rather than synchronized in ConfigImpl --- .../com/typesafe/config/impl/ConfigImpl.java | 57 +++++++++---------- .../config/impl/ConfigSubstitutionTest.scala | 2 +- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java index cb0551ab..7c34b0d9 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java @@ -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(); diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala index a051b5a4..8014d06c 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala @@ -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)