Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Config.java

00001 package edu.virtualschool.jwaa;
00002 import java.io.BufferedInputStream;
00003 import java.io.File;
00004 import java.io.FileInputStream;
00005 import java.io.FileNotFoundException;
00006 import java.io.IOException;
00007 import java.util.Properties;
00008 
00012 public final class Config
00013 {
00014   public final Properties properties;
00015 
00016   public Config(File f) throws Fault
00017   {
00018     this(f.toString());
00019   }
00020   public Config(Properties p) throws Fault
00021   {
00022     this.properties = p;
00023   }
00024   public Config(String f) throws Fault
00025   {
00026       this.properties = new Properties();
00027       try
00028       {
00029         FileInputStream is = new FileInputStream(f.toString());
00030         BufferedInputStream bis = new BufferedInputStream(is);
00031         properties.load(bis);
00032         bis.close();
00033       }
00034       catch (FileNotFoundException e)
00035       {
00036         throw new Fault("Can't find "+f, e); 
00037       }
00038       catch (IOException e)
00039       {
00040         throw new Fault("IOException in "+f, e); 
00041       }
00042   }
00043   public final String get(String key, String defaultValue)
00044   {
00045     String value = properties.getProperty(key, defaultValue);
00046     if (value == null)
00047       value = defaultValue;
00048     return value;
00049   }
00050   public final boolean get(String key, boolean defaultValue)
00051   {
00052     String value = get(key, null);
00053     if (value == null)
00054       return defaultValue;
00055     return value.equalsIgnoreCase("true");
00056   }
00057   public final int get(String key, int defaultValue)
00058   {
00059     String value = get(key, null);
00060     if (value == null)
00061       return defaultValue;
00062     try
00063     {
00064       return Integer.parseInt(value);
00065     }
00066     catch (NumberFormatException e)
00067     {
00068       return defaultValue;
00069     }
00070   }
00071   public final long get(String key, long defaultValue)
00072   {
00073     String value = get(key, null);
00074     if (value == null)
00075       return defaultValue;
00076     try
00077     {
00078       return Long.parseLong(value);
00079     }
00080     catch (NumberFormatException e)
00081     {
00082       return defaultValue;
00083     }
00084   }
00085   public final void put(String key, Object value)
00086   {
00087     properties.setProperty(key, value + "");
00088   }
00089   public final void put(String key, boolean value)
00090   {
00091     properties.setProperty(key, value + "");
00092   }
00093   public final void put(String key, int value)
00094   {
00095     properties.setProperty(key, value + "");
00096   }
00097   public final void put(String key, long value)
00098   {
00099     properties.setProperty(key, value + "");
00100   }
00101 
00102   public final String get(String key) throws Fault
00103   {
00104     String value = get(key, null);
00105     if (value == null)
00106       throw new Fault(key + " missing in " + this);
00107     return value;
00108   }
00109   public final boolean getBoolean(String key) throws Fault
00110   {
00111     String value = get(key);
00112     return value.equalsIgnoreCase("true");
00113   }
00114   public final int getInt(String key) throws Fault
00115   {
00116     String value = get(key);
00117     try
00118     {
00119       return Integer.parseInt(value);
00120     }
00121     catch (NumberFormatException e)
00122     {
00123       throw new Fault("" + key + " must have an integer value: " + this +"");
00124     }
00125   }
00126   public final long getLong(String key) throws Fault
00127   {
00128     String value = get(key);
00129     try
00130     {
00131       return Long.parseLong(value);
00132     }
00133     catch (NumberFormatException e)
00134     {
00135       throw new Fault("" + key + " must have a long value: " + this +"");
00136     }
00137   }
00138   public void list(java.io.PrintStream ps)
00139   {
00140     properties.list(ps);
00141   }
00142   public final static Config loadFile(File path) throws Fault
00143   {
00144     return loadFile(path.toString());
00145   }
00146   public final static Config loadFile(String path) throws Fault
00147   {
00148     try
00149     {
00150       Properties p = new Properties();
00151       FileInputStream fis = new FileInputStream(path.toString());
00152       BufferedInputStream bis = new BufferedInputStream(fis);
00153       p.load(bis);
00154       bis.close();
00155       return new Config(p);
00156     }
00157     catch (Exception e)
00158     {
00159       throw new Fault(e, e);
00160     }
00161   }
00162   public String toString()
00163   {
00164     return properties+"";
00165   }
00166   public final static class Fault extends edu.virtualschool.jwaa.Fault
00167   {
00168     public Fault(Object m, Throwable e) { super(m, e); }
00169     public Fault(Object m) { super(m, null); }
00170     public Fault(Throwable e) { super(e, e); }
00171   }
00172 }