1 package alt.jiapi.util; 2 3 import java.util.Properties ; 4 import java.io.InputStream ; 5 6 11 public class Configuration { 12 private String resource; 13 14 17 public Configuration() { 18 this.resource = "/jiapi.properties"; 19 } 20 21 public Configuration(String resource) { 22 this.resource = resource; 23 } 24 25 26 public boolean getBoolean(String key, boolean deflt) { 27 if (key == null) { 28 return deflt; 29 } 30 31 Properties p = getProperties(resource); 32 String s = p.getProperty(key); 33 34 boolean b = deflt; 35 try { 36 b = Boolean.valueOf(s).booleanValue(); 37 } 38 catch(Exception e) { 39 } 40 41 return b; 42 } 43 44 45 public int getInt(String key, int deflt) { 46 if (key == null) { 47 return deflt; 48 } 49 50 Properties p = getProperties(resource); 51 String s = p.getProperty(key); 52 53 int i = deflt; 54 try { 55 i = Integer.valueOf(s).intValue(); 56 } 57 catch(Exception e) { 58 } 59 60 return i; 61 } 62 63 64 private Properties getProperties(String name) { 65 Properties properties = new Properties (); 66 67 try { 68 InputStream is = getClass().getResourceAsStream(name); 69 if (is != null) { 70 properties.load(is); 71 } 72 else { 73 System.out.println("Could not find resource: " + name); 74 } 75 } 76 catch (Exception e) { 77 System.out.println("Failed to get property file " + name + 78 ", " + e); 79 } 80 81 return properties; 82 } 83 } 84 | Popular Tags |