| 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 13 public class PropertiesFilePropertySet implements PropertySet { 14 15 protected static HashMap instances = new HashMap(); 16 protected ResourceBundle props; 17 protected String fileName; 18 19 private PropertiesFilePropertySet(String jspiderHome, String configuration, String 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 jspiderHome, String configuration, String 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 getString(String name, String defaultValue) { 38 String value = null; 39 try { 40 value = getProperty(name); 41 } catch (MissingResourceException e) { 42 value = defaultValue; 44 } 45 return value; 46 } 47 48 public Class getClass(String name, Class defaultValue) { 49 String className = null; 50 if ( defaultValue == null ) { 51 className = getString(name, name); 52 } else { 53 className = getString(name, defaultValue.getName()); 54 } 55 56 Class retClass = null; 57 58 try { 59 retClass = Class.forName(className); 62 } catch (ClassNotFoundException e) { 63 retClass = defaultValue; 65 } 66 67 return retClass; 68 69 } 70 71 public int getInteger(String name, int defaultValue) { 72 String stringValue = getString(name, "" + defaultValue); 73 74 int value = 0; 75 try { 76 value = Integer.parseInt(stringValue); 77 } catch (NumberFormatException e) { 78 value = defaultValue; 80 } 81 82 return value; 83 } 84 85 public boolean getBoolean(String name, boolean defaultValue) { 86 String stringValue = getString(name, "" + defaultValue); 87 boolean value = new Boolean (stringValue).booleanValue(); 88 return value; 89 } 90 91 protected String getProperty ( String 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 |