I would like to check for the existence of a ResourceBundle without loading it.
Typically, I use Guice, and during initialization I want to check for existence, and at runtime I want to load it. If the package does not exist, I want to receive an early RB existence report.
If it was possible to get the ResourceBundle.Control instance used for a specific ResourceBundle, I would not have a problem getting the basic information to create the actual resource name (using toBundleName () and toResourceName ()), but this is not the case at this level.
Edit:
Ok, I found a way to do this. I will create a ResourceBundle.Control that is extensible (using custome addFormat (String, Class)) to save all package formats, and then use another method to check all possible file names for a specific locale (using the. GetResource class, as indicated below).
Coding:
class MyControl extends ResourceBundle.Control {
private Map<String,Class<? extends ResourceBundle>> formats = new LinkedHashMap();
public void addFormat(String format,Class<? extends ResourceBundle> rbType) {
formats.put(format, rbType);
}
public boolean resourceBundleExists(ClassLoader loader, String baseName, Locale locale) {
for (String format: formats.keySet()) {
if (loader.getResource(toResourceName(toBundleName(baseName, locale), format)) != null) {
return true;
}
}
return false;
}
}
source
share