1 package com.tonbeller.tbutils.res; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 import java.util.Collection ; 10 import java.util.Properties ; 11 12 import org.apache.log4j.Logger; 13 14 20 class FilePersistentResourceProvider implements PersistentResourceProvider { 21 Properties props; 22 File file; 23 24 private static Logger logger = Logger.getLogger(FilePersistentResourceProvider.class); 25 26 public FilePersistentResourceProvider(File file) { 27 this.file = file; 28 load(); 29 } 30 31 private void load() { 32 props = new Properties (); 33 InputStream is = null; 34 try { 35 if (!file.exists()) 36 return; 37 is = new FileInputStream (file); 38 props.load(is); 39 } catch (IOException e) { 40 logger.error(null, e); 41 } finally { 42 if (is != null) { 43 try { 44 is.close(); 45 } catch (IOException e) { 46 logger.error(null, e); 47 } 48 } 49 } 50 } 51 52 private void store() { 53 OutputStream os = null; 54 try { 55 os = new FileOutputStream (file); 56 props.store(os, "## TONBELLER"); 57 } catch (IOException e) { 58 logger.error(null, e); 59 } finally { 60 if (os != null) { 61 try { 62 os.close(); 63 } catch (IOException e1) { 64 logger.error(null, e1); 65 } 66 } 67 } 68 69 } 70 71 public String getString(String key) { 72 return props.getProperty(key); 73 } 74 75 public void store(String key, String value) { 76 props.setProperty(key, value); 77 } 78 79 public void remove(String key) { 80 props.remove(key); 81 } 82 83 public Collection keySet() { 84 return props.keySet(); 85 } 86 87 public void flush() { 88 store(); 89 } 90 91 public void close() { 92 store(); 93 } 94 95 public void dump(Dumper d) { 96 d.dump(this); 97 } 98 99 public String getName() { 100 return "FileResouceProvider " + file; 101 } 102 } | Popular Tags |