5409 |
16 Sep 10 |
nicklas |
1 |
/** |
5409 |
16 Sep 10 |
nicklas |
$Id$ |
5409 |
16 Sep 10 |
nicklas |
3 |
|
5409 |
16 Sep 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5409 |
16 Sep 10 |
nicklas |
5 |
|
5409 |
16 Sep 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5409 |
16 Sep 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5409 |
16 Sep 10 |
nicklas |
8 |
|
5409 |
16 Sep 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5409 |
16 Sep 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5409 |
16 Sep 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5409 |
16 Sep 10 |
nicklas |
of the License, or (at your option) any later version. |
5409 |
16 Sep 10 |
nicklas |
13 |
|
5409 |
16 Sep 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5409 |
16 Sep 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5409 |
16 Sep 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5409 |
16 Sep 10 |
nicklas |
GNU General Public License for more details. |
5409 |
16 Sep 10 |
nicklas |
18 |
|
5409 |
16 Sep 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5409 |
16 Sep 10 |
nicklas |
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 |
Factory for loading resource bundles. The factory return {@link ResourceBundleWrapper} |
5409 |
16 Sep 10 |
nicklas |
objects which are wrappers around regular {@link ResourceBundle}:s that provide us with |
5409 |
16 Sep 10 |
nicklas |
some extra functionality. |
5409 |
16 Sep 10 |
nicklas |
<p> |
5409 |
16 Sep 10 |
nicklas |
NOTE! Plug-ins and extensions that are not part of the BASE core are usually loaded with a |
5409 |
16 Sep 10 |
nicklas |
different class loader than the rest of BASE. If they are including resource in their own |
5409 |
16 Sep 10 |
nicklas |
JAR files, they must use the versions that take a class loader as the last parameter and |
5409 |
16 Sep 10 |
nicklas |
set it to the class loader of of their own classes. Eg. |
5409 |
16 Sep 10 |
nicklas |
<code>this.getClass().getClassLoader()</code>. |
5409 |
16 Sep 10 |
nicklas |
41 |
|
5409 |
16 Sep 10 |
nicklas |
@author Nicklas |
5409 |
16 Sep 10 |
nicklas |
@since 2.16 |
5409 |
16 Sep 10 |
nicklas |
@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 |
Same as <code>getResouceBundle(name, sc, null)</code>. |
5409 |
16 Sep 10 |
nicklas |
@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 |
Get a resource bundle for the given session control. If the session control |
5409 |
16 Sep 10 |
nicklas |
is not null the {@link SessionControl#getLocale()} is used as the locale, |
5409 |
16 Sep 10 |
nicklas |
otherwise the server default locale ({@link Config#getLocale()}) is used. |
5409 |
16 Sep 10 |
nicklas |
62 |
|
5409 |
16 Sep 10 |
nicklas |
@param name The name of the resource bundle |
5409 |
16 Sep 10 |
nicklas |
@param sc A session control, or null |
5409 |
16 Sep 10 |
nicklas |
@param loader The class loader to use when locating the resource bundle(s). |
5409 |
16 Sep 10 |
nicklas |
If null, the context class loader of the current thread is used |
5409 |
16 Sep 10 |
nicklas |
@return A resource bundle wrapper |
5409 |
16 Sep 10 |
nicklas |
@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 |
Same as <code>getResouceBundle(name, locale, null)</code>. |
5409 |
16 Sep 10 |
nicklas |
@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 |
Get a resource bundle for the given locale. |
5409 |
16 Sep 10 |
nicklas |
87 |
|
5409 |
16 Sep 10 |
nicklas |
@param name The name of the resource bundle |
5409 |
16 Sep 10 |
nicklas |
@param locale A locale object |
5409 |
16 Sep 10 |
nicklas |
@param loader The class loader to use when locating the resource bundle(s). |
5409 |
16 Sep 10 |
nicklas |
If null, the context class loader of the current thread is used |
5409 |
16 Sep 10 |
nicklas |
@return A resource bundle wrapper |
5409 |
16 Sep 10 |
nicklas |
@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 |
} |