}
//-- private utilities --//
/** Returns Map(String key, String label) of the specified locale. */
private final Map getLabels(Locale locale) {
WaitLock lock = null;
for (;;) {
final Object o;
synchronized (_labels) {
o = _labels.get(locale);
if (o == null)
_labels.put(locale, lock = new WaitLock()); //lock it
}
if (o instanceof Map)
return (Map)o;
if (o == null)
break; //go to load the page
//wait because some one is creating the servlet
if (!((WaitLock)o).waitUntilUnlock(5*60*1000))
log.warning("Take too long to wait loading labels: "+locale
+"\nTry to load again automatically...");
} //for(;;)
if (_jarcharset == null)
_jarcharset = Library.getProperty("org.zkoss.util.label.classpath.charset", "UTF-8");
if (_warcharset == null) {
_warcharset = Library.getProperty("org.zkoss.util.label.web.charset", null);
if (_warcharset == null)
_warcharset = Library.getProperty("org.zkoss.util.label.WEB-INF.charset", "UTF-8"); //backward compatible
}
try {
//get the class name
log.info("Loading labels for "+locale);
final Map labels = new HashMap(512);
//1. load from modules
final ClassLocator locator = new ClassLocator();
for (Enumeration en = locator.getResources(
locale == null ? "metainfo/i3-label.properties":
"metainfo/i3-label_" + locale + ".properties");
en.hasMoreElements();) {
final URL url = (URL)en.nextElement();
load(labels, url, _jarcharset);
}
//2. load from extra resource
for (Iterator it = _locators.iterator(); it.hasNext();) {
final URL url = ((LabelLocator)it.next()).locate(locale);
if (url != null)
load(labels, url, _warcharset);
}
//add to map
synchronized (_labels) {
_labels.put(locale, labels);
}
return labels;
} catch (Throwable ex) {
synchronized (_labels) {
_labels.remove(locale);
}
throw SystemException.Aide.wrap(ex);
} finally {
lock.unlock(); //unlock (always unlock to avoid deadlock)
}
}