From 6b0cc08317c9e2901af91542c0177182eaaf8c38 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 18 Aug 2011 11:13:14 +0200 Subject: [PATCH] SONAR-75 implement negative cache of rule descriptions --- .../java/org/sonar/core/i18n/I18nManager.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java index d1a98db5599..7ec38095fc9 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java @@ -45,7 +45,7 @@ public class I18nManager implements I18n, ServerExtension { private Map bundleToClassloaders; private Map propertyToBundles; private ClassLoader languagePackClassLoader; - private Map> fileContentCache = Maps.newHashMap(); + private Map> fileContentCache = Maps.newHashMap(); public I18nManager(PluginRepository pluginRepository) { this.pluginRepository = pluginRepository; @@ -108,11 +108,11 @@ public class I18nManager implements I18n, ServerExtension { /** * Only the given locale is searched. Contrary to java.util.ResourceBundle, no strategy for locating the bundle is implemented in - * this method. + * this method. */ String messageFromFile(Locale locale, String filename, String relatedProperty, boolean keepInCache) { - Map fileCache = fileContentCache.get(filename); - if (fileCache!=null && fileCache.containsKey(locale)) { + Map fileCache = fileContentCache.get(filename); + if (fileCache != null && fileCache.containsKey(locale)) { return fileCache.get(locale); } @@ -129,19 +129,22 @@ public class I18nManager implements I18n, ServerExtension { if (input != null) { try { result = IOUtils.toString(input, "UTF-8"); - if (keepInCache && result!=null) { - if (fileCache==null) { - fileCache = Maps.newHashMap(); - fileContentCache.put(filename, fileCache); - } - fileCache.put(locale, result); - } + } catch (IOException e) { throw new SonarException("Fail to load file: " + filePath, e); } finally { IOUtils.closeQuietly(input); } } + + if (keepInCache) { + if (fileCache == null) { + fileCache = Maps.newHashMap(); + fileContentCache.put(filename, fileCache); + } + // put null value for negative caching. + fileCache.put(locale, result); + } } return result; } -- 2.39.5