All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ibankapp.base.exception.PropertyUtil Maven / Gradle / Ivy

The newest version!
/*
 * iBankApp
 *
 * License : Apache License,Version 2.0, January 2004
 *
 * See the LICENSE file in English or LICENSE.zh_CN in chinese
 * in the root directory or .
 */

package org.ibankapp.base.exception;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * 配置文件读取类.
 *
 * @author ibankapp
 * @author codelder
 * @since 1.0.0
 */
public class PropertyUtil {

  /**
   * 属性文件对象.
   */
  private static final Properties PROPS = new Properties();

  /**
   * 将属性文件读取进入属性对象.
   *
   * @param name 文件路径及名称
   */
  public static void load(String name) {
    InputStream is = null;

    try {
      is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
      PROPS.load(is);
    } catch (Exception e) {
      throw new BaseException().initCause(e);
    } finally {
      try {
        if (is != null) {
          is.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * 根据key值从属性文件中获取value.
   *
   * @param key 属性key
   * @return 属性value
   * @throws BaseException key为null异常
   */
  public static String getProperty(String key) {

    if (key == null) {
      throw new BaseException("E-BASE-000002");
    }

    return PROPS.getProperty(key);

  }

  /**
   * 根据key值和一个插值从属性文件获取value.
   *
   * @param key 属性key
   * @param appendMsg 插值
   * @return 属性value
   */
  public static String getProperty(String key, String appendMsg) {
    String property = getProperty(key);

    if (appendMsg != null) {
      property = MessageFormat.format(property, appendMsg);
    }

    return property;
  }

  /**
   * 根据key值和多个插值从属性文件获取value.
   *
   * @param key 属性key
   * @param appendMsgs 插值数组
   * @return 属性value
   */
  public static String getProperty(String key, String[] appendMsgs) {
    String property = getProperty(key);

    if (appendMsgs != null) {
      property = MessageFormat.format(property, (Object[]) appendMsgs);
    }

    return property;
  }

  /**
   * 根据key值和两个插值从属性文件获取value.
   *
   * @param key 属性key
   * @param appendMsg 插值1
   * @param appendMsg1 插值2
   * @return 属性value
   * @throws BaseException key在配置文件中不存在
   */
  public static String getProperty(String key, String appendMsg, String appendMsg1) {
    String property = getProperty(key);

    if (property == null) {
      throw new BaseException("E-BASE-000003", key);
    }

    property = MessageFormat.format(property, appendMsg, appendMsg1);

    return property;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy