Archived

This forum has been archived. Please start a new discussion on GitHub.

Cannot read config file using Java ClassLoader

Hey,

I've packaged the configuration file for my ICE client, nvacconfig, into a JAR file, at the path config/nvacconfig. I can load this config file normally using Ice.InitializationData.properties.load(String file), but I cannot do that from inside my JAR; I need to use a ClassLoader to read the file.

Seeing as InitializationData.properties.load() only takes a String argument, I cannot pass the file as an InputStream, which is what I would normally do:
		ClassLoader cl = this.getClass().getClassLoader();
		InputStream file = cl.getResourceAsStream("config/nvacconfig");
		Ice.InitializationData id;
		try {
		    id = new Ice.InitializationData();
		    id.properties = Ice.Util.createProperties();
		    id.properties.load(file);
                    // Does not work

Also, I cannot use java.net.URL to load the appropriate file and pass it as a String; it will not read the file correctly.

Is there a way to load this configuration file properly to the InitializationData using a ClassLoader? Is there another way to load this configuration file?

Thanks,
Dan

Comments

  • benoit
    benoit Rennes, France
    Hi,

    No it's currently not possible to automatically load the configuration file from the Jar file (so you'll have to load the configuration file yourself and setup the properties "manually"). If you would like this feature to be added to Ice, please contact us at info@zeroc.com.

    Cheers,
    Benoit.
  • Hello,

    Thanks for the reply. I am a bit lost however as to how to setup the properties from the configuration file manually.

    I altered my Ice.Communicator.stringToProxy calls by simply hard-coding the appropriate information, such as:
    base = NVACMain.getIC().stringToProxy("IceStorm/TopicManager:tcp -p port -h localhost");
    

    The only problem I have remaining is the IceGrid Locator service. I am unsure of how to load this manually. I used to load it in the config file, but I can't anymore. I've tried passing it to Ice.Communicator.initialize, as if I were specifying it from the command line, but that has not worked either.

    I've tried using three different arguments, none of which have worked correctly:
    String[] argv = {"Ice.Default.Locator=IceGrid/Locator:default -h localhost -p port",
    			"IceGrid/Locator:default -h localhost -p port",
    			"--IceGrid/Locator:default -h localhost -p port"};
    		try {
    			ic = Ice.Util.initialize(argv);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    

    Obviously, this either cannot be done, or the syntax is wrong (I guessed on the syntax, as I couldn't find anything about doing this in the documentation).

    I am positive the problem lies in the IceGrid Locator, as I was not registering any events from IceStorm without loading the config file, even if I specified the appropriate proxies and topicnames.

    Is there a way to do what I am trying to do? Can I manually specify the IceGrid Locator information? Is there another way I can go about doing this?

    Thanks,
    Dan
  • benoit
    benoit Rennes, France
    The command line argument should be: "--Ice.Defaut.Locator=IceGrid/Locator:default -h localhost -p port".

    That's not exactly what I was suggesting though. I was suggesting to set the properties manually with "Ice::Properties::setProperty()" prior to the communicator initialization (with values obtained from the configuration file stored in the Jar file, you would take care of the parsing of this configuration file.)

    Something like the following:
       String locator = ... // get the locator setting from your configuration file.
       InitializationData initData = new Ice.InitializationData();
       initData.properties = Ice.Util.createProperties();
       initData.properties.setProperty("Ice.Default.Locator", locator);
    

    Cheers,
    Benoit.
  • Ah, I see now what you mean. This works fine now, thanks a lot.