diff options
author | Andreas Gampe <agampe@google.com> | 2015-03-16 20:21:42 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-03-16 20:21:43 +0000 |
commit | 79c17dc2b392201f55c700781408d4af625c82a5 (patch) | |
tree | 2f79aec7e2344b69de155a8facd0b43953cc1409 /luni/src/main/java | |
parent | 464b90443d0496c4d191d36f5266bb6a2b21e383 (diff) | |
parent | a7c789d57250a6ba95541e0037ad3be797ee27ea (diff) | |
download | libcore-79c17dc2b392201f55c700781408d4af625c82a5.zip libcore-79c17dc2b392201f55c700781408d4af625c82a5.tar.gz libcore-79c17dc2b392201f55c700781408d4af625c82a5.tar.bz2 |
Merge "Libcore: Avoid double-checked locking"
Diffstat (limited to 'luni/src/main/java')
3 files changed, 68 insertions, 70 deletions
diff --git a/luni/src/main/java/javax/xml/datatype/FactoryFinder.java b/luni/src/main/java/javax/xml/datatype/FactoryFinder.java index b65f412..1fbca2f 100644 --- a/luni/src/main/java/javax/xml/datatype/FactoryFinder.java +++ b/luni/src/main/java/javax/xml/datatype/FactoryFinder.java @@ -47,11 +47,30 @@ final class FactoryFinder { /** <p>Debug flag to trace loading process.</p> */ private static boolean debug = false; - /** <p>Cache properties for performance.</p> */ - private static Properties cacheProps = new Properties(); + /** + * <p>Cache properties for performance. Use a static class to avoid double-checked + * locking.</p> + */ + private static class CacheHolder { - /** <p>First time requires initialization overhead.</p> */ - private static boolean firstTime = true; + private static Properties cacheProps = new Properties(); + + static { + String javah = System.getProperty("java.home"); + String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; + File f = new File(configFile); + if (f.exists()) { + if (debug) debugPrintln("Read properties file " + f); + try { + cacheProps.load(new FileInputStream(f)); + } catch (Exception ex) { + if (debug) { + ex.printStackTrace(); + } + } + } + } + } /** Default columns per line. */ private static final int DEFAULT_LINE_LENGTH = 80; @@ -177,22 +196,7 @@ final class FactoryFinder { // try to read from $java.home/lib/jaxp.properties try { - String javah = System.getProperty("java.home"); - String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; - String factoryClassName = null; - if (firstTime) { - synchronized (cacheProps) { - if (firstTime) { - File f = new File(configFile); - firstTime = false; - if (f.exists()) { - if (debug) debugPrintln("Read properties file " + f); - cacheProps.load(new FileInputStream(f)); - } - } - } - } - factoryClassName = cacheProps.getProperty(factoryId); + String factoryClassName = CacheHolder.cacheProps.getProperty(factoryId); if (debug) debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { diff --git a/luni/src/main/java/javax/xml/validation/SchemaFactoryFinder.java b/luni/src/main/java/javax/xml/validation/SchemaFactoryFinder.java index 636777c..0060612 100644 --- a/luni/src/main/java/javax/xml/validation/SchemaFactoryFinder.java +++ b/luni/src/main/java/javax/xml/validation/SchemaFactoryFinder.java @@ -49,14 +49,29 @@ final class SchemaFactoryFinder { private static boolean debug = false; /** - * <p>Cache properties for performance.</p> + * <p>Cache properties for performance. Use a static class to avoid double-checked + * locking.</p> */ - private static Properties cacheProps = new Properties(); + private static class CacheHolder { - /** - * <p>First time requires initialization overhead.</p> - */ - private static boolean firstTime = true; + private static Properties cacheProps = new Properties(); + + static { + String javah = System.getProperty("java.home"); + String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; + File f = new File(configFile); + if (f.exists()) { + if (debug) debugPrintln("Read properties file " + f); + try { + cacheProps.load(new FileInputStream(f)); + } catch (Exception ex) { + if (debug) { + ex.printStackTrace(); + } + } + } + } + } /** * Default columns per line. @@ -184,27 +199,9 @@ final class SchemaFactoryFinder { } } - String javah = System.getProperty("java.home"); - String configFile = javah + File.separator + - "lib" + File.separator + "jaxp.properties"; - - String factoryClassName = null ; - // try to read from $java.home/lib/jaxp.properties try { - if(firstTime){ - synchronized(cacheProps){ - if(firstTime){ - File f=new File( configFile ); - firstTime = false; - if(f.exists()){ - if (debug) debugPrintln("Read properties file " + f); - cacheProps.load(new FileInputStream(f)); - } - } - } - } - factoryClassName = cacheProps.getProperty(propertyName); + String factoryClassName = CacheHolder.cacheProps.getProperty(propertyName); if (debug) debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { diff --git a/luni/src/main/java/javax/xml/xpath/XPathFactoryFinder.java b/luni/src/main/java/javax/xml/xpath/XPathFactoryFinder.java index 0113e7d..5a7663c 100644 --- a/luni/src/main/java/javax/xml/xpath/XPathFactoryFinder.java +++ b/luni/src/main/java/javax/xml/xpath/XPathFactoryFinder.java @@ -56,14 +56,29 @@ final class XPathFactoryFinder { } /** - * <p>Cache properties for performance.</p> + * <p>Cache properties for performance. Use a static class to avoid double-checked + * locking.</p> */ - private static Properties cacheProps = new Properties(); + private static class CacheHolder { - /** - * <p>First time requires initialization overhead.</p> - */ - private static boolean firstTime = true; + private static Properties cacheProps = new Properties(); + + static { + String javah = System.getProperty("java.home"); + String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; + File f = new File(configFile); + if (f.exists()) { + if (debug) debugPrintln("Read properties file " + f); + try { + cacheProps.load(new FileInputStream(f)); + } catch (Exception ex) { + if (debug) { + ex.printStackTrace(); + } + } + } + } + } /** * <p>Conditional debug printing.</p> @@ -164,27 +179,9 @@ final class XPathFactoryFinder { e.printStackTrace(); } - String javah = System.getProperty("java.home"); - String configFile = javah + File.separator + - "lib" + File.separator + "jaxp.properties"; - - String factoryClassName = null ; - // try to read from $java.home/lib/jaxp.properties try { - if(firstTime){ - synchronized(cacheProps){ - if(firstTime){ - File f=new File( configFile ); - firstTime = false; - if (f.exists()) { - if (debug) debugPrintln("Read properties file " + f); - cacheProps.load(new FileInputStream(f)); - } - } - } - } - factoryClassName = cacheProps.getProperty(propertyName); + String factoryClassName = CacheHolder.cacheProps.getProperty(propertyName); if (debug) debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { |