ICE Read Properties file
It’s easy to change properties if they are set in Properties files. Properties file contains many name–value pairs with each pair on a separate line. When any empty lines and lines consisting entirely of white space those characters are ignored. If you want to comment any properties then use # character introduces as comment in the file.
This demo is continuation of ICE Hello World tutorial where we put ICE adapter and end point information inside the code. Using property file we will keep these value outside and read dynamically and set in system path after loading property file.
- Project structure with properties file:
- configuration.properties: We are setting end point parameter here and read in client and server file:
JavaHonkAdapter.Endpoints=tcp -p 10000
- LoadReadPropertyFile.java class to load and set property value in system path:
package com.javahonk.servent; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Properties; public class LoadReadPropertyFile { static { try { Properties property = new Properties(); Reader myReader = new InputStreamReader(new FileInputStream(System.getProperty("ICE_CONFIG"))); property.load(myReader); for (String name : property.stringPropertyNames()) { System.setProperty(name, property.getProperty(name)); } } catch (IOException e) { e.printStackTrace(); } } public static void loadPropertyFile(){} }
- Please copy all configuration and setting from ICE Hello World tutorial and little modification done in Server.java and Client.java class to read and set end point value from property file:
package com.javahonk.server; import com.javahonk.servent.HelloWorldI; import com.javahonk.servent.LoadReadPropertyFile; public class Server { public static void main(String[] args) throws Exception { LoadReadPropertyFile.loadPropertyFile(); Ice.Communicator ic = null; try { System.out.println("Initialize ice run time..."); ic = Ice.Util.initialize(args); // Creates object adapter and its mandatory to trail it with name Adapter. Port number 10000 to receive the request System.out.println("Create object adapter, listening on port 10000..."); Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("JavaHonkAdapter", System.getProperty("JavaHonkAdapter.Endpoints")); System.out.println("Create object out from servant interface..."); Ice.Object object = new HelloWorldI(); System.out.println("It's required to add adapter into servent..."); adapter.add(object, Ice.Util.stringToIdentity("JavaHonk")); System.out.println("Activating adapter, waiting to process request..."); adapter.activate(); ic.waitForShutdown(); } catch (Exception exception) { exception.printStackTrace(); System.exit(1); } finally { if (ic != null) { ic.destroy(); } } } }
- Client.java:
package com.javahonk.client; import com.javahonk.servent.LoadReadPropertyFile; import firstHelloWorld.HelloWorldPrx; import firstHelloWorld.HelloWorldPrxHelper; public class Client { public static void main(String[] args) { LoadReadPropertyFile.loadPropertyFile(); Ice.Communicator ic = null; try { // Initialize Ice run time ic = Ice.Util.initialize(args); // Access to remote adapter JavaHonk agent Ice.ObjectPrx base = ic.stringToProxy("JavaHonk:"+System.getProperty("JavaHonkAdapter.Endpoints")); // Convert proxy HelloWorldPrx helloWorld = HelloWorldPrxHelper.checkedCast(base); // Check if conversion proxy is valid if (helloWorld == null) { throw new Error("Proxy is invalid"); } // Call the method and pass the value helloWorld.greeting("Hello World"); helloWorld.testAnotherOperation("Java Honk Hello World!"); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { if (ic != null) { ic.destroy(); } } System.exit(1); } }
- You could load property file by directly setting its fully qualified path in the code but this will make dependency on the code, every time you change path of the file you will have to recompile class. To avoid this we will set file path as VM argument on both Server and Client class and read its value. I have set file path in VM argument in eclipse as below. If you are using command prompt to run this then pass same from command prompt:
- Run client server separately as you ran in this tutorial you will see below output:
- For more information please refer ICE manual here
Download project: ICEHelloWorld