Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

java - Is it possible to include resource bundle files within a resource bundle

We are using java.util.ResourceBundle to load property information. Our property file has grown so huge and we are thinking of splitting the master property file into several sub modules. Is it possible to achieve this?

master.properties

==> 

 master.properties
   include moduleA.properties
   include moduleB.properties

Let me know?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, I wonder why you've chosen java.util.ResourceBundle over java.util.Properties. Given how your question is formulated, you don't seem to care about localization/internationalization nor about bundle file inheritance.

With Properties it's extraordinary easy since it implements Map which in turn offers a putAll() method to merge 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 in using ResourceBundle, your best bet is to create a custom ResourceBundle wherein you contol the loading by a custom Control.

Assuming that you've the following entry in master.properties which represents a commaseparated string with 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 is left aside, this is up to you)

This can be used as:

ResourceBundle bundle = new MultiResourceBundle("master");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...