KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > util > config > properties > PropertiesFilePropertySet


1 package net.javacoding.jspider.core.util.config.properties;
2
3 import net.javacoding.jspider.core.util.config.PropertySet;
4 import net.javacoding.jspider.core.logging.LogFactory;
5 import net.javacoding.jspider.core.logging.Log;
6
7 import java.io.*;
8 import java.util.*;
9
10 /**
11  * $Id: PropertiesFilePropertySet.java,v 1.8 2003/03/27 17:44:23 vanrogu Exp $
12  */

13 public class PropertiesFilePropertySet implements PropertySet {
14
15     protected static HashMap instances = new HashMap();
16     protected ResourceBundle props;
17     protected String JavaDoc fileName;
18
19     private PropertiesFilePropertySet(String JavaDoc jspiderHome, String JavaDoc configuration, String JavaDoc fileName ) {
20        this.fileName = fileName;
21         try {
22             InputStream is = new FileInputStream(jspiderHome + File.separator + "conf" + File.separator + configuration + File.separator + fileName );
23             props = new PropertyResourceBundle(is);
24         } catch (IOException e) {
25             System.err.println("[Config] " + fileName + " couldn't be found -- using all default values !");
26             props = null;
27         }
28     }
29
30     public synchronized static PropertySet getInstance(String JavaDoc jspiderHome, String JavaDoc configuration, String JavaDoc fileName) {
31             if (instances.get(fileName) == null) {
32                 instances.put(fileName, new PropertiesFilePropertySet(jspiderHome, configuration, fileName));
33             }
34             return (PropertySet)instances.get(fileName);
35     }
36
37     public String JavaDoc getString(String JavaDoc name, String JavaDoc defaultValue) {
38         String JavaDoc value = null;
39         try {
40             value = getProperty(name);
41         } catch (MissingResourceException e) {
42             //System.err.println("[Config] Value for '" + name + "' not found in " + fileName + " - defaulting to '" + defaultValue + "'");
43
value = defaultValue;
44         }
45         return value;
46     }
47
48     public Class JavaDoc getClass(String JavaDoc name, Class JavaDoc defaultValue) {
49         String JavaDoc className = null;
50         if ( defaultValue == null ) {
51           className = getString(name, name);
52         } else {
53           className = getString(name, defaultValue.getName());
54         }
55
56         Class JavaDoc retClass = null;
57
58         try {
59             //Using the thread's ContextClassLoader gives ClassCastEx in JUnit.
60
//retClass = Thread.currentThread().getContextClassLoader().loadClass(className);
61
retClass = Class.forName(className);
62         } catch (ClassNotFoundException JavaDoc e) {
63             //System.err.println("[Config] Class '" + className + "' couldn't be loaded - defaulted to '" + defaultValue + "'");
64
retClass = defaultValue;
65         }
66
67         return retClass;
68
69     }
70
71     public int getInteger(String JavaDoc name, int defaultValue) {
72         String JavaDoc stringValue = getString(name, "" + defaultValue);
73
74         int value = 0;
75         try {
76             value = Integer.parseInt(stringValue);
77         } catch (NumberFormatException JavaDoc e) {
78             //System.err.println("[Config] NumberFormatException on value '" + name + "', defaulted to " + defaultValue);
79
value = defaultValue;
80         }
81
82         return value;
83     }
84
85     public boolean getBoolean(String JavaDoc name, boolean defaultValue) {
86         String JavaDoc stringValue = getString(name, "" + defaultValue);
87         boolean value = new Boolean JavaDoc(stringValue).booleanValue();
88         return value;
89     }
90
91     protected String JavaDoc getProperty ( String JavaDoc name ) throws MissingResourceException {
92         if ( props == null ) {
93             throw new MissingResourceException(name,name,name);
94         } else{
95             return props.getString(name);
96         }
97     }
98 }
99
Popular Tags