PropertiesFilePropertySet.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:3k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util.config.properties;
  2. import net.javacoding.jspider.core.util.config.PropertySet;
  3. import net.javacoding.jspider.core.logging.LogFactory;
  4. import net.javacoding.jspider.core.logging.Log;
  5. import java.io.*;
  6. import java.util.*;
  7. /**
  8.  * $Id: PropertiesFilePropertySet.java,v 1.8 2003/03/27 17:44:23 vanrogu Exp $
  9.  */
  10. public class PropertiesFilePropertySet implements PropertySet {
  11.     protected static HashMap instances = new HashMap();
  12.     protected ResourceBundle props;
  13.     protected String fileName;
  14.     private PropertiesFilePropertySet(String jspiderHome, String configuration, String fileName ) {
  15.        this.fileName = fileName;
  16.         try {
  17.             InputStream is = new FileInputStream(jspiderHome + File.separator + "conf" + File.separator + configuration + File.separator + fileName );
  18.             props = new PropertyResourceBundle(is);
  19.         } catch (IOException e) {
  20.             System.err.println("[Config] " + fileName + " couldn't be found -- using all default values !");
  21.             props = null;
  22.         }
  23.     }
  24.     public synchronized static PropertySet getInstance(String jspiderHome, String configuration, String fileName) {
  25.             if (instances.get(fileName) == null) {
  26.                 instances.put(fileName, new PropertiesFilePropertySet(jspiderHome, configuration, fileName));
  27.             }
  28.             return (PropertySet)instances.get(fileName);
  29.     }
  30.     public String getString(String name, String defaultValue) {
  31.         String value = null;
  32.         try {
  33.             value = getProperty(name);
  34.         } catch (MissingResourceException e) {
  35.             //System.err.println("[Config] Value for '" + name + "' not found in " + fileName + " - defaulting to '" + defaultValue + "'");
  36.             value = defaultValue;
  37.         }
  38.         return value;
  39.     }
  40.     public Class getClass(String name, Class defaultValue) {
  41.         String className = null;
  42.         if ( defaultValue == null ) {
  43.           className = getString(name, name);
  44.         } else {
  45.           className = getString(name, defaultValue.getName());
  46.         }
  47.         Class retClass = null;
  48.         try {
  49.             //Using the thread's ContextClassLoader gives ClassCastEx in JUnit.
  50.             //retClass = Thread.currentThread().getContextClassLoader().loadClass(className);
  51.             retClass = Class.forName(className);
  52.         } catch (ClassNotFoundException e) {
  53.             //System.err.println("[Config] Class '" + className + "' couldn't be loaded - defaulted to '" + defaultValue + "'");
  54.             retClass = defaultValue;
  55.         }
  56.         return retClass;
  57.     }
  58.     public int getInteger(String name, int defaultValue) {
  59.         String stringValue = getString(name, "" + defaultValue);
  60.         int value = 0;
  61.         try {
  62.             value = Integer.parseInt(stringValue);
  63.         } catch (NumberFormatException e) {
  64.             //System.err.println("[Config] NumberFormatException on value '" + name + "', defaulted to " + defaultValue);
  65.             value = defaultValue;
  66.         }
  67.         return value;
  68.     }
  69.     public boolean getBoolean(String name, boolean defaultValue) {
  70.         String stringValue = getString(name, "" + defaultValue);
  71.         boolean value = new Boolean(stringValue).booleanValue();
  72.         return value;
  73.     }
  74.     protected String getProperty ( String name ) throws MissingResourceException {
  75.         if ( props == null ) {
  76.             throw new MissingResourceException(name,name,name);
  77.         } else{
  78.             return props.getString(name);
  79.         }
  80.     }
  81. }