1 18 package org.apache.geronimo.interop.properties; 19 20 import java.io.BufferedInputStream ; 21 import java.io.FileInputStream ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 27 import org.apache.geronimo.interop.SystemException; 28 29 30 public class PropertyMap extends HashMap { 31 public PropertyMap() { 32 super(); 33 } 34 35 public PropertyMap(Map map) { 36 super(map); 37 } 38 39 public Object put(Object key, Object value) { 40 if (key == null) { 41 throw new NullPointerException ("key"); 42 } 43 if (value == null) { 44 throw new NullPointerException ("value"); 46 } 47 return super.put(key, value); 48 } 49 50 public String getProperty(String name) { 51 return (String ) super.get(name); 52 } 53 54 public String getProperty(String name, String defaultValue) { 55 String value = getProperty(name); 56 if (value == null || value.length() == 0) { 57 value = defaultValue; 58 } 59 return value; 60 } 61 62 public Properties getProperties() { 63 Properties props = new Properties (); 64 for (Iterator i = entrySet().iterator(); i.hasNext();) { 65 Map.Entry entry = (Map.Entry ) i.next(); 66 String key = (String ) entry.getKey(); 67 String value = (String ) entry.getValue(); 68 props.put(key, value); 69 } 70 return props; 71 } 72 73 public static PropertyMap readFile(String fileName) { 74 try { 75 Properties props = new Properties (); 76 BufferedInputStream input = new BufferedInputStream (new FileInputStream (fileName)); 77 props.load(input); 78 input.close(); 79 PropertyMap map = new PropertyMap(); 80 for (Iterator i = props.entrySet().iterator(); i.hasNext();) { 81 Map.Entry entry = (Map.Entry ) i.next(); 82 String key = entry.getKey().toString().trim(); 83 String value = entry.getValue().toString().trim(); 84 map.put(key, value); 85 } 86 return map; 87 } catch (Exception ex) { 88 throw new SystemException(ex); 89 } 90 } 91 } 92 | Popular Tags |