Is it possible to include resource packages in a resource package

We use java.util.ResourceBundle to load property information. Our properties file has grown so huge, and we are thinking of splitting the core properties file into several auxiliary modules. Can this be achieved?

 master.properties ==> master.properties include moduleA.properties include moduleB.properties 

Tell me?

+8
java resourcebundle
source share
3 answers

First of all, I wonder why you selected java.util.ResourceBundle over java.util.Properties . Given how your question is worded, you don't seem to care about localization / internationalization, nor about inheriting package files.

With Properties this is unusually simple because it implements a Map , which in turn offers a putAll() method to combine another map. Kickoff example:

 Properties master = new Properties(); master.load(masterInput); Properties moduleA = new Properties(); moduleA.load(moduleAinput); master.putAll(moduleA); Properties moduleB = new Properties(); moduleB.load(moduleBinput); master.putAll(moduleB); // Now `master` contains the properties of all files. 

If you really insist on using a ResourceBundle , it's best to create a custom ResourceBundle in which you load the custom Control .

Assuming you have the following entry in master.properties , which is a delimited string with the base names of the module properties files:

 include=moduleA,moduleB 

Then the following custom ResourceBundle example should work:

 public class MultiResourceBundle extends ResourceBundle { protected static final Control CONTROL = new MultiResourceBundleControl(); private Properties properties; public MultiResourceBundle(String baseName) { setParent(ResourceBundle.getBundle(baseName, CONTROL)); } protected MultiResourceBundle(Properties properties) { this.properties = properties; } @Override protected Object handleGetObject(String key) { return properties != null ? properties.get(key) : parent.getObject(key); } @Override @SuppressWarnings("unchecked") public Enumeration<String> getKeys() { return properties != null ? (Enumeration<String>) properties.propertyNames() : parent.getKeys(); } protected static class MultiResourceBundleControl extends Control { @Override public ResourceBundle newBundle( String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { Properties properties = load(baseName, loader); String include = properties.getProperty("include"); if (include != null) { for (String includeBaseName : include.split("\\s*,\\s*")) { properties.putAll(load(includeBaseName, loader)); } } return new MultiResourceBundle(properties); } private Properties load(String baseName, ClassLoader loader) throws IOException { Properties properties = new Properties(); properties.load(loader.getResourceAsStream(baseName + ".properties")); return properties; } } } 

(trivial exception handling and localization handling are left out, it's up to you)

This can be used as:

 ResourceBundle bundle = new MultiResourceBundle("master"); 
+12
source share

You can programmatically, however, build a ResourceBundle, but as you say, your file is huge, what if it is loaded into memory.

Update

 public class Resource extends java.util.ResourceBundle { public Object handleGetObject(String key) { //code } public Enumeration getKeys() { //code } } 

then for the locale IN

 import java.util.*; public class Resource_en_IN extends Resource{ public Object handleGetObject(String key) { //code } } 
+1
source share

More food for thought than a proven solution.

XML files support entities for inline text from other files during parsing. If you saw complex xml files, where this method was used to modulate files.

Properties now supports two file formats, the common .properties format with key / value pairs and xml formats. Properties can load and store in / from xml files.

ResourceBundle has one direct subclass: PropertyResourceBundle . It seems that this class is actually limited to the older key / value pair format, but it can be used to implement another class, for example XMLPropertyResourceBundle , which is able to read properties from xml files, where the entity trick can help modulate these files.

If this works, converting existing property files to xml property files should be simple, just use the Properties class, read it from the standard format and save it in XML.

+1
source share

Source: https://habr.com/ru/post/650746/


All Articles