You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2006/11/09 21:13:27 UTC
svn commit: r473057 -
/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java
Author: pcl
Date: Thu Nov 9 12:13:27 2006
New Revision: 473057
URL: http://svn.apache.org/viewvc?view=rev&rev=473057
Log:
localizer optimizations for normal usage patterns
Modified:
incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java
Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java?view=diff&rev=473057&r1=473056&r2=473057
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Localizer.java Thu Nov 9 12:13:27 2006
@@ -53,12 +53,6 @@
new StreamResourceBundleProvider(),
new ZipResourceBundleProvider(), }));
- // the local file name and class' classloader
- private ResourceBundle _bundle = null;
-
- // the package that this localizer was created for.
- private Package _package;
-
/**
* Return a Localizer instance that will access the properties file
* in the package of the given class using the system default locale.
@@ -83,12 +77,12 @@
if (locale == null)
locale = Locale.getDefault();
- int dot = (cls == null) ? -1 : cls.getName().lastIndexOf('.');
+ Package pkg = cls == null ? null : cls.getPackage();
String file;
- if (dot == -1)
+ if (pkg == null)
file = "localizer";
else
- file = cls.getName().substring(0, dot + 1) + "localizer";
+ file = pkg.getName() + ".localizer";
String key = file + locale.toString();
// no locking; ok if bundle created multiple times
@@ -97,22 +91,12 @@
Localizer loc = (Localizer) _localizers.get(key);
if (loc != null)
return loc;
-
- // find resource bundle
- ResourceBundle bundle = null;
- ClassLoader loader = (cls == null) ? null : cls.getClassLoader();
- for (Iterator itr = _providers.iterator(); itr.hasNext();) {
- bundle = ((ResourceBundleProvider) itr.next()).findResource
- (file, locale, loader);
- if (bundle != null)
- break;
+ else {
+ loc = new Localizer(pkg, file, locale,
+ cls == null ? null : cls.getClassLoader());
+ _localizers.put(key, loc);
+ return loc;
}
-
- // cache the localizer
- loc = new Localizer(cls == null ? null : cls.getPackage());
- loc._bundle = bundle;
- _localizers.put(key, loc);
- return loc;
}
/**
@@ -129,8 +113,30 @@
return _providers.remove(provider);
}
- private Localizer(Package p) {
+ private String _file;
+ private ResourceBundle _bundle = null;
+ private Package _package;
+ private Locale _locale;
+ private ClassLoader _loader;
+
+ private Localizer(Package p, String f, Locale locale, ClassLoader loader) {
_package = p;
+ _file = f;
+ _locale = locale;
+ _loader = loader;
+ }
+
+ private ResourceBundle getBundle() {
+ // no locking; it's ok to create multiple bundles
+ if (_bundle == null) {
+ // find resource bundle
+ for (Iterator itr = _providers.iterator();
+ itr.hasNext() && _bundle == null; ) {
+ _bundle = ((ResourceBundleProvider) itr.next())
+ .findResource(_file, _locale, _loader);
+ }
+ }
+ return _bundle;
}
/**
@@ -207,7 +213,7 @@
* @see #get(String)
*/
public Message get(String key, Object[] subs) {
- return new Message(_package, _bundle, key, subs, false);
+ return new Message(_package, getBundle(), key, subs, false);
}
/**
@@ -219,7 +225,7 @@
* @see #getFatal(String)
*/
public Message getFatal(String key, Object[] subs) {
- return new Message(_package, _bundle, key, subs, true);
+ return new Message(_package, getBundle(), key, subs, true);
}
/**