org.hibernate.util.PropertiesHelper

來源:互聯網
上載者:User

該類為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() {}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.