Using the properties object in Java

You can optionally use a properties object to control when your Java™ application uses the optimized REST service interface.

Before you begin

Ensure that an optimized REST service interface is available.

About this task

The Java programming language provides a construct that is called the properties object. Before using the properties object, set conventions on how property files and property values are defined, named, and managed. You can use the properties object to manage configuration values across different platforms or control URLs at the OpenAPI descriptor level or the service descriptor level. For example, you can run the same Java application in development, test, and production environments without requiring any code changes when you request z/TPF stateless application services. When you develop and test your Java application, the property file directs requests for stateless z/TPF application services to remote z/TPF test systems. When you deploy the same Java application to test and production systems, you can set the property file to use the optimized REST service interface when you call local stateless z/TPF application services.

You can use properties objects for stateful and stateless services. However, if you use properties objects for stateful services, you must always use the optimized REST service interface for the stateful services.

By using the properties object, configuration values that are coded as a set of string objects can be read from a file, which can be substituted into the application code. For example, you can use the properties object in the following situations when the target of the service varies based on the location of the caller:
  • If a Java application that calls a z/TPF application service is not on the z/TPF system, specify the URL as the host name or the IP address of the z/TPF system.
  • If the Java application is on the same z/TPF processor as the z/TPF application service, specify the URL as localhost.

Procedure

  1. Set up a properties object from a file in the file system.
    Properties appProps = new Properties();
    FileInputStream file = new FileInputStream("application.properties");
    appProps.load(file);
    file.close();
  2. Read a particular variable by calling one of the following variations of the getProperty API:
    appProps.getProperty(String key)
    appProps.getProperty(String key, String default)
    Note: If the default parameter is specified, the application can set a default value when the property is not found in the configuration.
  3. Use the getProperty API to control the URL instead of hard coding the value that is passed in.