1 4 package org.oddjob.values.types; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.util.Collection ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 import java.util.Set ; 14 15 import org.oddjob.OddjobException; 16 import org.oddjob.util.OddjobConfigException; 17 18 35 public class PropertiesType implements Map { 36 37 private Properties props; 38 39 44 private File file; 45 46 51 private InputStream input; 52 53 56 public File getFile() { 57 return file; 58 } 59 62 public void setFile(File file) { 63 this.file = file; 64 } 65 66 public Object valueFor(Class required) { 67 load(); 68 return props; 69 } 70 71 private void load() { 72 if (props != null) { 73 return; 74 } 75 76 try { 77 if (file != null) { 78 input = new FileInputStream (file); 79 } 80 if (input == null) { 81 throw new OddjobConfigException("No input type specified for properties."); 82 } 83 props = new Properties (); 84 props.load(input); 85 } catch (IOException e) { 86 throw new OddjobException("Can't load properties!", e); 87 } 88 } 89 90 93 public InputStream getInput() { 94 return input; 95 } 96 99 public void setInput(InputStream input) { 100 this.input = input; 101 } 102 103 106 public void clear() { 107 throw new UnsupportedOperationException ("This map is read only!"); 108 } 109 110 113 public boolean containsKey(Object key) { 114 load(); 115 return props.containsKey(key); 116 } 117 118 121 public boolean containsValue(Object value) { 122 load(); 123 return props.containsValue(value); 124 } 125 126 129 public Set entrySet() { 130 load(); 131 return props.entrySet(); 132 } 133 134 137 public Object get(Object key) { 138 load(); 139 return props.get(key); 140 } 141 142 145 public boolean isEmpty() { 146 load(); 147 return props.isEmpty(); 148 } 149 150 153 public Set keySet() { 154 load(); 155 return props.keySet(); 156 } 157 158 161 public Object put(Object key, Object value) { 162 throw new UnsupportedOperationException ("This map is read only!"); 163 } 164 165 168 public void putAll(Map t) { 169 throw new UnsupportedOperationException ("This map is read only!"); 170 } 171 172 175 public Object remove(Object key) { 176 load(); 177 return props.remove(key); 178 } 179 180 183 public int size() { 184 load(); 185 return props.size(); 186 } 187 188 191 public Collection values() { 192 load(); 193 return props.values(); 194 } 195 196 } 197 | Popular Tags |