1 25 26 27 package org.nemesis.forum.util; 28 29 import java.util.*; 30 import java.io.*; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 49 public class PropertyManager { 50 51 static protected Log log = LogFactory.getLog(PropertyManager.class); 52 53 54 55 private static PropertyManager manager = null; 56 private static Object managerLock = new Object (); 57 private static String propsName = "/nemesis.properties"; 58 59 65 public static String getProperty(String name) { 66 if (manager == null) { 67 synchronized (managerLock) { 68 if (manager == null) { 69 manager = new PropertyManager(propsName); 70 } 71 } 72 } 73 return manager.getProp(name); 74 } 75 76 83 public static void setProperty(String name, String value) { 84 if (manager == null) { 85 synchronized (managerLock) { 86 if (manager == null) { 87 manager = new PropertyManager(propsName); 88 } 89 } 90 } 91 manager.setProp(name, value); 92 } 93 94 100 public static void deleteProperty(String name) { 101 if (manager == null) { 102 synchronized (managerLock) { 103 if (manager == null) { 104 manager = new PropertyManager(propsName); 105 } 106 } 107 } 108 manager.deleteProp(name); 109 } 110 111 116 public static Enumeration propertyNames() { 117 if (manager == null) { 118 synchronized (managerLock) { 119 if (manager == null) { 120 manager = new PropertyManager(propsName); 121 } 122 } 123 } 124 return manager.propNames(); 125 } 126 127 132 public static boolean propertyFileIsReadable() { 133 if (manager == null) { 134 synchronized (managerLock) { 135 if (manager == null) { 136 manager = new PropertyManager(propsName); 137 } 138 } 139 } 140 return manager.propFileIsReadable(); 141 } 142 143 148 public static boolean propertyFileIsWritable() { 149 if (manager == null) { 150 synchronized (managerLock) { 151 if (manager == null) { 152 manager = new PropertyManager(propsName); 153 } 154 } 155 } 156 return manager.propFileIsWritable(); 157 } 158 159 163 public static boolean propertyFileExists() { 164 if (manager == null) { 165 synchronized (managerLock) { 166 if (manager == null) { 167 manager = new PropertyManager(propsName); 168 } 169 } 170 } 171 return manager.propFileExists(); 172 } 173 174 175 176 177 private Properties properties = null; 178 private Object propertiesLock = new Object (); 179 private String resourceURI; 180 181 184 private PropertyManager(String resourceURI) { 185 this.resourceURI = resourceURI; 186 } 187 188 197 protected String getProp(String name) { 198 if (properties == null) { 201 synchronized (propertiesLock) { 202 if (properties == null) { 204 loadProps(); 205 } 206 } 207 } 208 String property = properties.getProperty(name); 209 if (property == null) { 210 return null; 211 } else { 212 return property.trim(); 213 } 214 } 215 216 220 protected void setProp(String name, String value) { 221 synchronized (propertiesLock) { 223 if (properties == null) { 225 loadProps(); 226 } 227 properties.setProperty(name, value); 228 saveProps(); 229 } 230 } 231 232 protected void deleteProp(String name) { 233 synchronized (propertiesLock) { 235 if (properties == null) { 237 loadProps(); 238 } 239 properties.remove(name); 240 saveProps(); 241 } 242 } 243 244 protected Enumeration propNames() { 245 if (properties == null) { 248 synchronized (propertiesLock) { 249 if (properties == null) { 251 loadProps(); 252 } 253 } 254 } 255 return properties.propertyNames(); 256 } 257 258 261 private void loadProps() { 262 properties = new Properties(); 263 InputStream in = null; 264 try { 265 in = getClass().getResourceAsStream(resourceURI); 266 properties.load(in); 267 } catch (Exception e) { 268 log.error("Error reading properties in PropertyManager.loadProps() ", e); 269 270 } finally { 271 try { 272 in.close(); 273 } catch (Exception e) { 274 } 275 } 276 } 277 278 281 private void saveProps() { 282 String path = properties.getProperty("path").trim(); 286 OutputStream out = null; 287 try { 288 out = new FileOutputStream(path); 289 properties.store(out, "yazd.properties -- " + (new java.util.Date ())); 290 } catch (Exception ioe) { 291 log.error( 292 "There was an error writing yazd.properties to " 293 + path 294 + ". " 295 + "Ensure that the path exists and that the Yazd process has permission " 296 + "to write to it -- ", 297 ioe); 298 } finally { 299 try { 300 out.close(); 301 } catch (Exception e) { 302 } 303 } 304 } 305 306 311 public boolean propFileIsReadable() { 312 try { 313 InputStream in = getClass().getResourceAsStream(resourceURI); 314 return true; 315 } catch (Exception e) { 316 return false; 317 } 318 } 319 320 324 public boolean propFileExists() { 325 String path = getProp("path"); 326 if (path == null) { 327 return false; 328 } 329 File file = new File(path); 330 if (file.isFile()) { 331 return true; 332 } else { 333 return false; 334 } 335 } 336 337 342 public boolean propFileIsWritable() { 343 String path = getProp("path"); 344 File file = new File(path); 345 if (file.isFile()) { 346 if (file.canWrite()) { 348 return true; 349 } else { 350 return false; 351 } 352 } else { 353 return false; 354 } 355 } 356 } 357 | Popular Tags |