|
| 1 | +package ca.uhn.fhir.i18n; |
| 2 | + |
| 3 | +import jakarta.annotation.Nonnull; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Enumeration; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Properties; |
| 13 | +import java.util.ResourceBundle; |
| 14 | + |
| 15 | +/** |
| 16 | + * Finds all properties files on the class path that match the given base name and merges them. |
| 17 | + * This implementation avoids the unnecessary overhead of writing to and reading from a stream. |
| 18 | + */ |
| 19 | +public class MultiFileResourceBundleControl extends ResourceBundle.Control { |
| 20 | + |
| 21 | + /** |
| 22 | + * Returns a list containing only Locale.ROOT as a candidate. |
| 23 | + * This forces the loader to look for the base bundle only (e.g., "bundle.properties"). |
| 24 | + * |
| 25 | + * @param baseName the base name of the resource bundle |
| 26 | + * @param locale the locale to load (this parameter is ignored) |
| 27 | + * @return a singleton list containing Locale.ROOT |
| 28 | + */ |
| 29 | + @Override |
| 30 | + public List<Locale> getCandidateLocales(String baseName, Locale locale) { |
| 31 | + return Collections.singletonList(Locale.ROOT); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Prevents fallback to the default locale if the root bundle isn't found. |
| 36 | + * |
| 37 | + * @param baseName the base name of the resource bundle |
| 38 | + * @param locale the locale where the search is failing |
| 39 | + * @return null to indicate no fallback should be attempted |
| 40 | + */ |
| 41 | + @Override |
| 42 | + public Locale getFallbackLocale(String baseName, Locale locale) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) |
| 48 | + throws IOException, IllegalAccessException, InstantiationException { |
| 49 | + |
| 50 | + // We only care about handling the root .properties file |
| 51 | + if (!"java.properties".equals(format) |
| 52 | + || (locale != null && !locale.toString().isEmpty())) { |
| 53 | + return super.newBundle(baseName, locale, format, loader, reload); |
| 54 | + } |
| 55 | + |
| 56 | + final String resourceName = toResourceName(toBundleName(baseName, locale), "properties"); |
| 57 | + final Properties mergedProperties = new Properties(); |
| 58 | + |
| 59 | + // Load from all matching resources |
| 60 | + final Enumeration<URL> resources = loader.getResources(resourceName); |
| 61 | + while (resources.hasMoreElements()) { |
| 62 | + final URL url = resources.nextElement(); |
| 63 | + |
| 64 | + try (InputStream is = url.openStream()) { |
| 65 | + mergedProperties.load(is); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Directly wrap the merged Properties object instead of performing a stream round-trip. |
| 70 | + return new PropertiesResourceBundle(mergedProperties); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * A lightweight, private ResourceBundle implementation that is backed directly by a Properties object. |
| 75 | + */ |
| 76 | + private static class PropertiesResourceBundle extends ResourceBundle { |
| 77 | + private final Properties properties; |
| 78 | + |
| 79 | + PropertiesResourceBundle(Properties properties) { |
| 80 | + this.properties = properties; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Object handleGetObject(@Nonnull String key) { |
| 85 | + return properties.getProperty(key); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + @Nonnull |
| 90 | + public Enumeration<String> getKeys() { |
| 91 | + return Collections.enumeration(properties.stringPropertyNames()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments