src/core/net/sf/basedb/util/resources/ResourceBundleFactory.java

Code
Comments
Other
Rev Date Author Line
5409 16 Sep 10 nicklas 1 /**
5409 16 Sep 10 nicklas 2   $Id$
5409 16 Sep 10 nicklas 3
5409 16 Sep 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5409 16 Sep 10 nicklas 5
5409 16 Sep 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5409 16 Sep 10 nicklas 7   Available at http://base.thep.lu.se/
5409 16 Sep 10 nicklas 8
5409 16 Sep 10 nicklas 9   BASE is free software; you can redistribute it and/or
5409 16 Sep 10 nicklas 10   modify it under the terms of the GNU General Public License
5409 16 Sep 10 nicklas 11   as published by the Free Software Foundation; either version 3
5409 16 Sep 10 nicklas 12   of the License, or (at your option) any later version.
5409 16 Sep 10 nicklas 13
5409 16 Sep 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5409 16 Sep 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5409 16 Sep 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5409 16 Sep 10 nicklas 17   GNU General Public License for more details.
5409 16 Sep 10 nicklas 18
5409 16 Sep 10 nicklas 19   You should have received a copy of the GNU General Public License
5409 16 Sep 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5409 16 Sep 10 nicklas 21 */
5409 16 Sep 10 nicklas 22 package net.sf.basedb.util.resources;
5409 16 Sep 10 nicklas 23
5409 16 Sep 10 nicklas 24
5409 16 Sep 10 nicklas 25 import java.util.Locale;
5409 16 Sep 10 nicklas 26 import java.util.ResourceBundle;
5409 16 Sep 10 nicklas 27
5409 16 Sep 10 nicklas 28 import net.sf.basedb.core.Config;
5409 16 Sep 10 nicklas 29 import net.sf.basedb.core.SessionControl;
5409 16 Sep 10 nicklas 30
5409 16 Sep 10 nicklas 31 /**
5409 16 Sep 10 nicklas 32   Factory for loading resource bundles. The factory return {@link ResourceBundleWrapper}
5409 16 Sep 10 nicklas 33   objects which are wrappers around regular {@link ResourceBundle}:s that provide us with
5409 16 Sep 10 nicklas 34   some extra functionality.
5409 16 Sep 10 nicklas 35   <p>
5409 16 Sep 10 nicklas 36   NOTE! Plug-ins and extensions that are not part of the BASE core are usually loaded with a 
5409 16 Sep 10 nicklas 37   different class loader than the rest of BASE. If they are including resource in their own 
5409 16 Sep 10 nicklas 38   JAR files, they must use the versions that take a class loader as the last parameter and
5409 16 Sep 10 nicklas 39   set it to the class loader of of their own classes. Eg. 
5409 16 Sep 10 nicklas 40   <code>this.getClass().getClassLoader()</code>.
5409 16 Sep 10 nicklas 41
5409 16 Sep 10 nicklas 42   @author Nicklas
5409 16 Sep 10 nicklas 43   @since 2.16
5409 16 Sep 10 nicklas 44   @base.modified $Date$
5409 16 Sep 10 nicklas 45 */
5409 16 Sep 10 nicklas 46 public class ResourceBundleFactory
5409 16 Sep 10 nicklas 47 {
5409 16 Sep 10 nicklas 48
5409 16 Sep 10 nicklas 49   /**
5409 16 Sep 10 nicklas 50     Same as <code>getResouceBundle(name, sc, null)</code>.
5409 16 Sep 10 nicklas 51     @see #getResourceBundle(String, SessionControl, ClassLoader)
5409 16 Sep 10 nicklas 52   */
5409 16 Sep 10 nicklas 53   public static ResourceBundleWrapper getResourceBundle(String name, SessionControl sc)
5409 16 Sep 10 nicklas 54   {
5409 16 Sep 10 nicklas 55     return getResourceBundle(name, sc, null);
5409 16 Sep 10 nicklas 56   }
5409 16 Sep 10 nicklas 57   
5409 16 Sep 10 nicklas 58   /**
5409 16 Sep 10 nicklas 59     Get a resource bundle for the given session control. If the session control
5409 16 Sep 10 nicklas 60     is not null the {@link SessionControl#getLocale()} is used as the locale,
5409 16 Sep 10 nicklas 61     otherwise the server default locale ({@link Config#getLocale()}) is used.
5409 16 Sep 10 nicklas 62     
5409 16 Sep 10 nicklas 63     @param name The name of the resource bundle
5409 16 Sep 10 nicklas 64     @param sc A session control, or null
5409 16 Sep 10 nicklas 65     @param loader The class loader to use when locating the resource bundle(s).
5409 16 Sep 10 nicklas 66       If null, the context class loader of the current thread is used
5409 16 Sep 10 nicklas 67     @return A resource bundle wrapper
5409 16 Sep 10 nicklas 68     @see Thread#getContextClassLoader()
5409 16 Sep 10 nicklas 69   */
5409 16 Sep 10 nicklas 70   public static ResourceBundleWrapper getResourceBundle(String name, SessionControl sc, ClassLoader loader)
5409 16 Sep 10 nicklas 71   {
5409 16 Sep 10 nicklas 72     Locale locale = sc == null ? Config.getLocale() : sc.getLocale();
5409 16 Sep 10 nicklas 73     return getResourceBundle(name, locale, loader);
5409 16 Sep 10 nicklas 74   }
5409 16 Sep 10 nicklas 75
5409 16 Sep 10 nicklas 76   /**
5409 16 Sep 10 nicklas 77     Same as <code>getResouceBundle(name, locale, null)</code>.
5409 16 Sep 10 nicklas 78     @see #getResourceBundle(String, Locale, ClassLoader)
5409 16 Sep 10 nicklas 79   */
5409 16 Sep 10 nicklas 80   public static ResourceBundleWrapper getResourceBundle(String name, Locale locale)
5409 16 Sep 10 nicklas 81   {
5409 16 Sep 10 nicklas 82     return getResourceBundle(name, locale, null);
5409 16 Sep 10 nicklas 83   }
5409 16 Sep 10 nicklas 84   
5409 16 Sep 10 nicklas 85   /**
5409 16 Sep 10 nicklas 86     Get a resource bundle for the given locale.
5409 16 Sep 10 nicklas 87     
5409 16 Sep 10 nicklas 88     @param name The name of the resource bundle
5409 16 Sep 10 nicklas 89     @param locale A locale object
5409 16 Sep 10 nicklas 90     @param loader The class loader to use when locating the resource bundle(s).
5409 16 Sep 10 nicklas 91       If null, the context class loader of the current thread is used
5409 16 Sep 10 nicklas 92     @return A resource bundle wrapper
5409 16 Sep 10 nicklas 93     @see Thread#getContextClassLoader()
5409 16 Sep 10 nicklas 94   */
5409 16 Sep 10 nicklas 95   public static ResourceBundleWrapper getResourceBundle(String name, Locale locale, ClassLoader loader)
5409 16 Sep 10 nicklas 96   {
5409 16 Sep 10 nicklas 97     ResourceBundle.Control control = Utf8PropertiesResourceBundleControl.INSTANCE;
5409 16 Sep 10 nicklas 98     if (loader == null) loader = Thread.currentThread().getContextClassLoader();
5409 16 Sep 10 nicklas 99     ResourceBundle bundle = loader == null ?
5409 16 Sep 10 nicklas 100         ResourceBundle.getBundle(name, locale, control) : 
5409 16 Sep 10 nicklas 101         ResourceBundle.getBundle(name, locale, loader, control);
5409 16 Sep 10 nicklas 102     return new ResourceBundleWrapper(name, bundle);
5409 16 Sep 10 nicklas 103   }
5409 16 Sep 10 nicklas 104   
5409 16 Sep 10 nicklas 105   private ResourceBundleFactory()
5409 16 Sep 10 nicklas 106   {}
5409 16 Sep 10 nicklas 107   
5409 16 Sep 10 nicklas 108 }