public class ExternalResourceLoader implements IStringResourceLoader {
private static final Logger logger = LoggerFactory.getLogger(ExternalResourceLoader.class);
private String _resourceBundleName;
public ExternalResourceLoader(String resourceBundleName) {
_resourceBundleName = resourceBundleName;
}
public String loadStringResource(Component component, String key) {
return findResource(component.getLocale(), key);
}
public String loadStringResource(Class<?> clazz, String key, Locale locale, String style) {
return findResource(locale, key);
}
private String findResource(Locale locale, String key) {
String string = null;
ResourceBundle resourceBundle = null;
try {
resourceBundle = ResourceBundle.getBundle(_resourceBundleName, locale);
} catch (MissingResourceException e) {
if (shouldThrowExceptionForMissingResource()) {
throw new WicketRuntimeException(String.format("Unable able to locate resource bundle for the specifed base name: %s", _resourceBundleName));
}
logger.warn("Unable able to locate resource bundle for the specifed base name: {}", _resourceBundleName);
}
if (resourceBundle != null) {
boolean caught = false;
try {
string = resourceBundle.getString(key);
} catch (MissingResourceException e) {
caught = true;
}
if (caught || string == null) {
if (shouldThrowExceptionForMissingResource()) {
throw new WicketRuntimeException(String.format("Unable able to locate resource bundle for the specifed base name: %s", _resourceBundleName));
}
logger.warn("No value found key {} in resource bundle {}.", key, _resourceBundleName);
}
}
return string;
}
private boolean shouldThrowExceptionForMissingResource()
return turn Application.get().getResourceSettings().getThrowExceptionOnMissingResource();
}
}
{ |