in src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java [458:512]
private ResourceBundle getResourceBundleInternal(ResourceResolver optionalResolver, final String baseName, Locale locale, final boolean overwriteCache) {
if (locale == null) {
locale = defaultLocale;
}
final Key key = new Key(baseName, locale);
JcrResourceBundle resourceBundle = !overwriteCache ? resourceBundleCache.get(key) : null;
if (resourceBundle != null) {
log.debug("getResourceBundleInternal({}): got cache hit on first try", key);
} else {
if (loadingGuards.get(key) == null) {
loadingGuards.putIfAbsent(key, new Semaphore(1));
}
final Semaphore loadingGuard = loadingGuards.get(key);
try {
loadingGuard.acquire();
resourceBundle = !overwriteCache ? resourceBundleCache.get(key) : null;
if (resourceBundle != null) {
log.debug("getResourceBundleInternal({}): got cache hit on second try", key);
} else {
log.debug("getResourceBundleInternal({}): reading from Repository", key);
ResourceResolver localResolver = null;
try {
if ( optionalResolver == null ) {
localResolver = createResourceResolver();
optionalResolver = localResolver;
}
resourceBundle = createResourceBundle(optionalResolver, key.baseName, key.locale);
// put the newly created ResourceBundle to the cache. If it replaces an existing entry unregister the existing
// service registration first before re-registering the new ResourceBundle.
if (resourceBundleCache.put(key, resourceBundle) != null) {
unregisterResourceBundle(key);
}
registerResourceBundle(key, resourceBundle);
} catch ( final LoginException le) {
throw (MissingResourceException)new MissingResourceException("Unable to create service resource resolver",
baseName,
locale.toString()).initCause(le);
} finally {
if ( localResolver != null ) {
localResolver.close();
}
}
}
} catch (InterruptedException e) {
Thread.interrupted();
} finally {
loadingGuard.release();
}
}
log.trace("getResourceBundleInternal({}) ==> {}", key, resourceBundle);
return resourceBundle;
}