From b6d37c5e6ce53dd38b122aa496f5857d29ead5c8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 28 Jul 2011 14:43:51 +0200 Subject: [PATCH] SONAR-75 refactor I18nManager --- .../org/sonar/plugins/core/CorePlugin.java | 18 ++-- .../java/org/sonar/core/i18n/I18nManager.java | 82 +++++++++++++++++++ .../org/sonar/core/i18n/I18nManagerTest.java | 44 ++++++++++ 3 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java create mode 100644 sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index c9ac989598e..bf045ccbcc7 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -21,9 +21,9 @@ package org.sonar.plugins.core; import com.google.common.collect.Lists; import org.sonar.api.CoreProperties; -import org.sonar.api.SonarPlugin; import org.sonar.api.Properties; import org.sonar.api.Property; +import org.sonar.api.SonarPlugin; import org.sonar.api.checks.NoSonarFilter; import org.sonar.api.resources.Java; import org.sonar.plugins.core.batch.ExcludedResourceFilter; @@ -44,7 +44,7 @@ import org.sonar.plugins.core.widgets.*; import java.util.List; -@Properties({ +@Properties({ @Property( key = CoreProperties.CORE_COVERAGE_PLUGIN_PROPERTY, defaultValue = "cobertura", @@ -118,12 +118,12 @@ import java.util.List; global = true ), @Property( - key = CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, - defaultValue = "en", - name = "Locale used for violation messages", - description = "Locale to be used when generating violation messages. It's up to each rule engine to support this global internationalization property", - project = true, - global = true), + key = CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, + defaultValue = "en", + name = "Locale used for violation messages", + description = "Locale to be used when generating violation messages. It's up to each rule engine to support this global internationalization property", + project = true, + global = true), @Property( key = "sonar.timemachine.period1", name = "Period 1", @@ -243,7 +243,7 @@ public class CorePlugin extends SonarPlugin { // i18n extensions.add(I18nManager.class); - +// extensions.add(org.sonar.core.i18n.I18nManager.class); return extensions; } } 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 new file mode 100644 index 00000000000..a99ba3397bf --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java @@ -0,0 +1,82 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.i18n; + +import com.google.common.collect.Maps; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.ServerExtension; +import org.sonar.api.i18n.I18n; +import org.sonar.api.platform.PluginMetadata; +import org.sonar.api.platform.PluginRepository; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; + +public class I18nManager implements I18n, ServerExtension { + + private PluginRepository pluginRepository; + private Map bundleToClassloader; + + public I18nManager(PluginRepository pluginRepository) { + this.pluginRepository = pluginRepository; + } + + I18nManager(Map bundleToClassloader) { + this.bundleToClassloader = bundleToClassloader; + } + + public void start() { + ClassLoader coreClassLoader = pluginRepository.getPlugin("i18nen").getClass().getClassLoader(); + + bundleToClassloader = Maps.newHashMap(); + for (PluginMetadata metadata : pluginRepository.getMetadata()) { + if (!metadata.isCore() && !"i18nen".equals(metadata.getBasePlugin())) { + ClassLoader classLoader = pluginRepository.getPlugin(metadata.getKey()).getClass().getClassLoader(); + bundleToClassloader.put(metadata.getKey(), classLoader); + + } else if (metadata.isCore()) { + bundleToClassloader.put(metadata.getKey(), coreClassLoader); + } + } + } + + public String message(Locale locale, String key, String defaultValue, Object... parameters) { + String bundle = keyToBundle(key); + ResourceBundle resourceBundle = ResourceBundle.getBundle(bundle, locale, bundleToClassloader.get(bundle)); + String value = resourceBundle.getString(key); + if (value==null) { + value = defaultValue; + } + if (value != null && parameters.length > 0) { + return MessageFormat.format(value, parameters); + } + return value; + } + + String keyToBundle(String key) { + String pluginKey = StringUtils.substringBefore(key, "."); + if (bundleToClassloader.containsKey(pluginKey)) { + return pluginKey; + } + return "core"; + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java new file mode 100644 index 00000000000..af57b768738 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.i18n; + +import com.google.common.collect.Maps; +import org.hamcrest.core.Is; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertThat; + +public class I18nManagerTest { + @Test + public void shouldExtractBundleKey() { + Map bundleToClassLoaders = Maps.newHashMap(); + bundleToClassLoaders.put("core", getClass().getClassLoader()); + bundleToClassLoaders.put("checkstyle", getClass().getClassLoader()); + bundleToClassLoaders.put("sqale", getClass().getClassLoader()); + I18nManager i18n = new I18nManager(bundleToClassLoaders); + + assertThat(i18n.keyToBundle("by"), Is.is("core")); + assertThat(i18n.keyToBundle("violations_drilldown.page"), Is.is("core")); + assertThat(i18n.keyToBundle("checkstyle.rule1.name"), Is.is("checkstyle")); + assertThat(i18n.keyToBundle("sqale.console.page"), Is.is("sqale")); + } +} -- 2.39.5