001: package net.javacoding.jspider.core.util.config.properties;
002:
003: import net.javacoding.jspider.core.util.config.PropertySet;
004: import net.javacoding.jspider.core.logging.LogFactory;
005: import net.javacoding.jspider.core.logging.Log;
006:
007: import java.io.*;
008: import java.util.*;
009:
010: /**
011: * $Id: PropertiesFilePropertySet.java,v 1.8 2003/03/27 17:44:23 vanrogu Exp $
012: */
013: public class PropertiesFilePropertySet implements PropertySet {
014:
015: protected static HashMap instances = new HashMap();
016: protected ResourceBundle props;
017: protected String fileName;
018:
019: private PropertiesFilePropertySet(String jspiderHome,
020: String configuration, String fileName) {
021: this .fileName = fileName;
022: try {
023: InputStream is = new FileInputStream(jspiderHome
024: + File.separator + "conf" + File.separator
025: + configuration + File.separator + fileName);
026: props = new PropertyResourceBundle(is);
027: } catch (IOException e) {
028: System.err
029: .println("[Config] "
030: + fileName
031: + " couldn't be found -- using all default values !");
032: props = null;
033: }
034: }
035:
036: public synchronized static PropertySet getInstance(
037: String jspiderHome, String configuration, String fileName) {
038: if (instances.get(fileName) == null) {
039: instances.put(fileName, new PropertiesFilePropertySet(
040: jspiderHome, configuration, fileName));
041: }
042: return (PropertySet) instances.get(fileName);
043: }
044:
045: public String getString(String name, String defaultValue) {
046: String value = null;
047: try {
048: value = getProperty(name);
049: } catch (MissingResourceException e) {
050: //System.err.println("[Config] Value for '" + name + "' not found in " + fileName + " - defaulting to '" + defaultValue + "'");
051: value = defaultValue;
052: }
053: return value;
054: }
055:
056: public Class getClass(String name, Class defaultValue) {
057: String className = null;
058: if (defaultValue == null) {
059: className = getString(name, name);
060: } else {
061: className = getString(name, defaultValue.getName());
062: }
063:
064: Class retClass = null;
065:
066: try {
067: //Using the thread's ContextClassLoader gives ClassCastEx in JUnit.
068: //retClass = Thread.currentThread().getContextClassLoader().loadClass(className);
069: retClass = Class.forName(className);
070: } catch (ClassNotFoundException e) {
071: //System.err.println("[Config] Class '" + className + "' couldn't be loaded - defaulted to '" + defaultValue + "'");
072: retClass = defaultValue;
073: }
074:
075: return retClass;
076:
077: }
078:
079: public int getInteger(String name, int defaultValue) {
080: String stringValue = getString(name, "" + defaultValue);
081:
082: int value = 0;
083: try {
084: value = Integer.parseInt(stringValue);
085: } catch (NumberFormatException e) {
086: //System.err.println("[Config] NumberFormatException on value '" + name + "', defaulted to " + defaultValue);
087: value = defaultValue;
088: }
089:
090: return value;
091: }
092:
093: public boolean getBoolean(String name, boolean defaultValue) {
094: String stringValue = getString(name, "" + defaultValue);
095: boolean value = new Boolean(stringValue).booleanValue();
096: return value;
097: }
098:
099: protected String getProperty(String name)
100: throws MissingResourceException {
101: if (props == null) {
102: throw new MissingResourceException(name, name, name);
103: } else {
104: return props.getString(name);
105: }
106: }
107: }
|