Package org.jtester.module.utils

Source Code of org.jtester.module.utils.PropertiesReader

package org.jtester.module.utils;

import static ext.jtester.org.apache.commons.io.IOUtils.closeQuietly;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

import org.jtester.exception.JTesterException;
import org.jtester.utility.JTesterLogger;
import org.jtester.utility.ResourceHelper;

public class PropertiesReader {

  /**
   * Loads the properties file with the given name, which is available in the
   * user home folder. If no file with the given name is found, null is
   * returned.
   *
   * @param propertiesFileName
   *            The name of the properties file
   * @return The Properties object, null if the properties file wasn't found.
   */
  public Properties loadPropertiesFileFromUserHome(String propertiesFileName) {
    InputStream inputStream = null;
    try {
      if ("".equals(propertiesFileName)) {
        throw new IllegalArgumentException("Properties Filename must be given.");
      }
      Properties properties = new Properties();
      String userHomeDir = System.getProperty("user.home");
      File localPropertiesFile = new File(userHomeDir, propertiesFileName);
      if (!localPropertiesFile.exists()) {
        return null;
      }
      inputStream = new FileInputStream(localPropertiesFile);
      properties.load(inputStream);
      JTesterLogger.info("Loaded configuration file " + propertiesFileName + " from user home");
      return properties;

    } catch (FileNotFoundException e) {
      return null;
    } catch (Throwable e) {
      throw new JTesterException("Unable to load configuration file: " + propertiesFileName + " from user home",
          e);
    } finally {
      closeQuietly(inputStream);
    }
  }

  /**
   * Loads the properties file with the given name, which is available in the
   * classpath. If no file with the given name is found, null is returned.
   *
   * @param propertiesFileName
   *            The name of the properties file
   * @return The Properties object, null if the properties file wasn't found.
   */
  public Properties loadPropertiesFileFromClasspath(String propertiesFileName) {
    InputStream inputStream = null;
    try {
      if ("".equals(propertiesFileName)) {
        throw new IllegalArgumentException("Properties Filename must be given.");
      }
      Properties properties = new Properties();
      inputStream = ResourceHelper.getResourceAsStream(propertiesFileName);
      if (inputStream == null) {
        return null;
      }
      properties.load(inputStream);
      return properties;
    } catch (FileNotFoundException e) {
      return null;
    } catch (Throwable e) {
      throw new JTesterException("Unable to load configuration file: " + propertiesFileName, e);
    } finally {
      closeQuietly(inputStream);
    }
  }
}
TOP

Related Classes of org.jtester.module.utils.PropertiesReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.