org.hibernate.util.PropertiesHelper

本文介绍Hibernate3中用于处理属性文件取值的工具类PropertiesHelper。该类为final且仅包含只读方法,确保线程安全。文章详细展示了如何通过默认值处理配置文件中不存在的属性,并提供了多种数据类型的获取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该类为hibernate3处理属性文件取值的工具类,首先该类是一个final类,且只有private构造函数,即使用者没法继承该类

另外该类的所有方法为只读方法,没有写方法,所以该类又是一个线程安全类,多线程可以放心使用该类,不会出现资源竞争问题

 

该类取值模式值得借鉴,通过传入一个默认值,如果取不到,则直接返回默认值

 

 


package org.hibernate.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Iterator;


public final class PropertiesHelper {

 private static final String PLACEHOLDER_START = "${";
 private static final String PLACEHOLDER_END = "}";

 public static boolean getBoolean(String property, Properties properties) {
  String setting = properties.getProperty(property);
  return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
 }

 public static boolean getBoolean(String property, Properties properties, boolean defaultValue) {
  String setting = properties.getProperty(property);
  return setting==null ? defaultValue : Boolean.valueOf( setting.trim() ).booleanValue();
 }

 public static int getInt(String property, Properties properties, int defaultValue) {
  String propValue = properties.getProperty(property);
  return propValue==null ? defaultValue : Integer.parseInt( propValue.trim() );
 }

 public static String getString(String property, Properties properties, String defaultValue) {
  String propValue = properties.getProperty(property);
  return propValue==null ? defaultValue : propValue;
 }

 public static Integer getInteger(String property, Properties properties) {
  String propValue = properties.getProperty(property);
  return propValue==null ? null : Integer.valueOf( propValue.trim() );
 }

 public static Map toMap(String property, String delim, Properties properties) {
  Map map = new HashMap();
  String propValue = properties.getProperty(property);
  if (propValue!=null) {
   StringTokenizer tokens = new StringTokenizer(propValue, delim);
   while ( tokens.hasMoreTokens() ) {
    map.put(
     tokens.nextToken(),
        tokens.hasMoreElements() ? tokens.nextToken() : ""
    );
   }
  }
  return map;
 }

 public static String[] toStringArray(String property, String delim, Properties properties) {
  return toStringArray( properties.getProperty(property), delim );
 }

 public static String[] toStringArray(String propValue, String delim) {
  if (propValue!=null) {
   return StringHelper.split(delim, propValue);
  }
  else {
   return ArrayHelper.EMPTY_STRING_ARRAY;
  }
 }

 /**
  * replace a property by a starred version
  *
  * @param props properties to check
  * @param key proeprty to mask
  * @return cloned and masked properties
  */
 public static Properties maskOut(Properties props, String key) {
  Properties clone = (Properties) props.clone();
  if (clone.get(key) != null) {
   clone.setProperty(key, "****");
  }
  return clone;
 }

 public static void resolvePlaceHolders(Properties properties) {
  Iterator itr = properties.entrySet().iterator();
  while ( itr.hasNext() ) {
   final Map.Entry entry = ( Map.Entry ) itr.next();
   final String value = ( String ) entry.getValue();
   if ( value != null ) {
    final String resolved = resolvePlaceHolder( value );
    if ( !value.equals( resolved ) ) {
     if ( resolved == null ) {
      itr.remove();
     }
     else {
      entry.setValue( resolved );
     }
    }
   }
  }
 }

 public static String resolvePlaceHolder(String property) {
  if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
   return property;
  }
  StringBuffer buff = new StringBuffer();
  char[] chars = property.toCharArray();
  for ( int pos = 0; pos < chars.length; pos++ ) {
   if ( chars[pos] == '$' ) {
    // peek ahead
    if ( chars[pos+1] == '{' ) {
     // we have a placeholder, spin forward till we find the end
     String systemPropertyName = "";
     int x = pos + 2;
     for (  ; x < chars.length && chars[x] != '}'; x++ ) {
      systemPropertyName += chars[x];
      // if we reach the end of the string w/o finding the
      // matching end, that is an exception
      if ( x == chars.length - 1 ) {
       throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
      }
     }
     String systemProperty = extractFromSystem( systemPropertyName );
     buff.append( systemProperty == null ? "" : systemProperty );
     pos = x + 1;
     // make sure spinning forward did not put us past the end of the buffer...
     if ( pos >= chars.length ) {
      break;
     }
    }
   }
   buff.append( chars[pos] );
  }
  String rtn = buff.toString();
  return StringHelper.isEmpty( rtn ) ? null : rtn;
 }

 private static String extractFromSystem(String systemPropertyName) {
  try {
   return System.getProperty( systemPropertyName );
  }
  catch( Throwable t ) {
   return null;
  }
 }

 

 /*私有构造函数,一般单例模式使用私有构造函数*/
 private PropertiesHelper() {}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值