3 Replies Latest reply: May 2, 2012 11:29 AM by 930303 RSS

    Properties

    930303
      I am tasked with a project, and I have next to no practical java experience. This is my task and current location:

      We had to move our web server to a different server because of hardware failure. I am taske with changing some report server issues because of the move. They want me to load the properties file that contains the server IP and such to a few of the existing classes. This way I can just add a method than alter the existing class to call the properties and reference information in the properties file. I have added the following code to the class that I want to alter:

      private void loadProperties()
      {
      String propFileName = null;
      FileInputStream propFile = null;
      Properties p = new Properties(System.getProperties());
      String sepChar = p.getProperty("file.separator");
      String appDir = p.getProperty("user.dir");

      try {
      //
      // Set the path to the properties file for the application.
      propFileName = appDir + sepChar + PROP_FILE;
      propFile = new FileInputStream(propFileName);
      p.load(propFile);
      System.setProperties(p);
      }

      catch ( Exception ex ) {
      ;
      }

      finally {
      if ( propFile != null ) {
      try {
      propFile.close();
      propFile = null;
      }

      catch ( Exception ex ) {

      }
      }

      p = null;
      appDir = null;
      sepChar = null;
      propFileName = null;
      }
      }

      Now I know this will work and it will load the property file into this class, but how do I get the info I want out of this property file and insert it into the necessary location in the class I am altering? I am under the impression that I just need a specific key from the property file and to replace the classes bad IP Address that is hardcoded with the key? I have been searching for info on this but I cannot find anything that I can relate to. Also how and or where should I call this loadproperties method to actually get it to load the properties?

      Any and help is greatly appreciated. I have downloaded a couple of java books to help me with some of the basics but they don't quite cover this.
        • 1. Re: Properties
          baftos
          See my comments in uppercase (I am not shouting, just making them stand out).
          // private void loadProperties() LET'S CHANGE TO:
          private String getIPAddress()
          {
          String propFileName = null;
          FileInputStream propFile = null;
          Properties p = new Properties(System.getProperties());
          String sepChar = p.getProperty("file.separator");
          String appDir = p.getProperty("user.dir");

          try {
          //
          // Set the path to the properties file for the application.
          propFileName = appDir + sepChar + PROP_FILE;
          propFile = new FileInputStream(propFileName);
          p.load(propFile);
          // System.setProperties(p); NO REASON TO DO THIS, DO RATHER THIS:
          return p.getProperty("IPADDRESS");
          }

          catch ( Exception ex ) {
          // BIG JAVA MISTAKE TO SWALLOW EXCEPTIONS, AT LEAT DO THIS:
          ex.printStackTrace();
          ;
          }

          finally {
          if ( propFile != null ) {
          try {
          propFile.close();
          propFile = null;
          }

          catch ( Exception ex ) {
          // EXCEPTION SWALLOWED AGAIN!
          }
          }

          p = null;
          appDir = null;
          sepChar = null;
          propFileName = null;
          }
          }
          • 2. Re: Properties
            BIJ001
            A sidenote. This ties the property file's location to the working directory:
            String appDir = p.getProperty("user.dir");
            It is not a very good place in a web- or application server environment.
            • 3. Re: Properties
              930303
              Thanks for the information :)