diff --git a/scm-core/src/main/java/sonia/scm/i18n/Bundle.java b/scm-core/src/main/java/sonia/scm/i18n/Bundle.java index 11429133d8..515ced9ef8 100644 --- a/scm-core/src/main/java/sonia/scm/i18n/Bundle.java +++ b/scm-core/src/main/java/sonia/scm/i18n/Bundle.java @@ -35,6 +35,7 @@ package sonia.scm.i18n; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.util.ClassLoaders; import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -83,7 +84,7 @@ public class Bundle */ public static Bundle getBundle(String path) { - return new Bundle(ResourceBundle.getBundle(path)); + return getBundle(path, null, null); } /** @@ -97,7 +98,35 @@ public class Bundle */ public static Bundle getBundle(String path, Locale locale) { - return new Bundle(ResourceBundle.getBundle(path, locale)); + return getBundle(path, locale, null); + } + + /** + * Creates a new bundle instance + * + * + * @param path path to the properties file + * @param locale locale for the properties file + * @param classLoader classLoader to load + * + * @return new bundle instance + * + * @since 1.37 + */ + public static Bundle getBundle(String path, Locale locale, + ClassLoader classLoader) + { + if (locale == null) + { + locale = Locale.ENGLISH; + } + + if (classLoader == null) + { + classLoader = ClassLoaders.getContextClassLoader(Bundle.class); + } + + return new Bundle(ResourceBundle.getBundle(path, locale, classLoader)); } /** @@ -137,7 +166,7 @@ public class Bundle } /** - * Returns the value of the key, formatted with {@link MessageFormat} or null + * Returns the value of the key, formatted with {@link MessageFormat} or null * if the key is not present in the bundle. * * @@ -145,7 +174,7 @@ public class Bundle * @param args format arguments * * @return formated message or null - * + * * @since 1.37 */ public String getStringIfPresent(String key, Object... args) @@ -163,6 +192,6 @@ public class Bundle //~--- fields --------------------------------------------------------------- - /** Field description */ + /** resource bundle */ private final ResourceBundle bundle; } diff --git a/scm-core/src/main/java/sonia/scm/i18n/I18nMessages.java b/scm-core/src/main/java/sonia/scm/i18n/I18nMessages.java index a0d8adff3c..0c11066f82 100644 --- a/scm-core/src/main/java/sonia/scm/i18n/I18nMessages.java +++ b/scm-core/src/main/java/sonia/scm/i18n/I18nMessages.java @@ -37,6 +37,8 @@ import com.google.common.base.Objects; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import sonia.scm.util.ClassLoaders; + //~--- JDK imports ------------------------------------------------------------ import java.lang.reflect.Field; @@ -142,7 +144,8 @@ public final class I18nMessages */ private static T createInstance(Class msgClass, Locale locale) { - Bundle bundle = Bundle.getBundle(msgClass.getName(), locale); + Bundle bundle = Bundle.getBundle(msgClass.getName(), locale, + ClassLoaders.getContextClassLoader(msgClass)); T instance = null; try diff --git a/scm-core/src/main/java/sonia/scm/util/ClassLoaders.java b/scm-core/src/main/java/sonia/scm/util/ClassLoaders.java new file mode 100644 index 0000000000..80998ef693 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/util/ClassLoaders.java @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.util; + +/** + * Util methods for {@link ClassLoader}s. + * + * @author Sebastian Sdorra + * @since 1.37 + */ +public final class ClassLoaders +{ + + /** + * Constructs ... + * + */ + private ClassLoaders() {} + + //~--- get methods ---------------------------------------------------------- + + /** + * Returns the context {@link ClassLoader} from the current {@link Thread}, if + * the context {@link ClassLoader} is not available the {@link ClassLoader}, + * which has load the given context class, is used. + * + * + * @param contextClass context class + * + * @return context class loader + */ + public static ClassLoader getContextClassLoader(Class contextClass) + { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + if (classLoader == null) + { + classLoader = contextClass.getClassLoader(); + } + + return classLoader; + } +}