简体   繁体   English

从System.getProperties()将属性合并到ResourceBundle中

[英]Merge properties into a ResourceBundle from System.getProperties()

I'm building a ResourceBundle from a file, this bundle holds < String, String> values. 我正在从文件构建ResourceBundle,此捆绑包包含<String,String>值。

InputStream in = getClass().getResourceAsStream("SQL.properties");
properties = new PropertyResourceBundle(in);
in.close();

I would like to add/ replace on this bundle some properties that I'm passing from the command line using -Dsome.option.val.NAME1=HiEarth 我想在此捆绑包中添加/ 替换我使用-Dsome.option.val.NAME1 = HiEarth从命令行传递的一些属性

I don't care dumping the old bundle and creating a new one instead. 我不在乎丢弃旧的捆绑包,而是创建一个新的捆绑包。

Could you please tip? 你能给小费吗?

I think that what I need to do is : 我认为我需要做的是:

  1. Create from the bundle a HashMap< String, String> 从捆绑包中创建HashMap <String,String>
  2. Replace values. 替换值。
  3. Transform the HashMap into a InputStream. 将HashMap转换为InputStream。 //This is the complicated part... //这是复杂的部分...
  4. Build the new bundle from that. 从此构建新的捆绑包。

This does some of what you want (converts the System.properties to a ResourceBundle). 这完成了您想要的一些工作(将System.properties转换为ResourceBundle)。 Better error handling is left up to you :-) 更好的错误处理将留给您:-)

public static ResourceBundle createBundle()
    {
        final ResourceBundle  bundle;
        final Properties      properties;
        final CharArrayWriter charWriter;
        final PrintWriter     printWriter;
        final CharArrayReader charReader;

        charWriter = new CharArrayWriter();
        printWriter = new PrintWriter(charWriter);

        properties = System.getProperties();
        properties.list(printWriter);

        charReader = new CharArrayReader(charWriter.toCharArray());

        try
        {
            bundle = new PropertyResourceBundle(charReader);

            return (bundle);
        }
        catch(final IOException ex)
        {
            // cannot happen
            ex.printStackTrace();
        }

        throw new Error();
    }

This might not be the best way to do it but it's the best I can think of: implement a subclass of ResourceBundle that stores the properties you want to add/replace, then set the parent of that bundle to be the PropertyResourceBundle you load from the input stream. 这可能不是最好的方法,但我能想到的最好的方法是:实现ResourceBundle的子类,该子类存储您要添加/替换的属性,然后将该捆绑包的父级设置为从加载的PropertyResourceBundle输入流。

InputStream in = getClass().getResourceAsStream("SQL.properties");
properties = new PropertyResourceBundle(in);
in.close();
MyCLIResourceBundle b = new MyCLIResourceBundle(properties);
// use b as your bundle

where the implementation would be something like 那里的实现将是这样的

public class MyCLIResourceBundle extends ResourceBundle {
    public MyCLIResourceBundle(ResourceBundle parent) {
        super();
        this.setParent(parent);
        // go on and load your chosen properties from System.getProperties() or wherever
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM