KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > PropertyManager


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

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 /**
34  * Manages properties for the entire Yazd system. Properties are merely
35  * pieces of information that need to be saved in between server restarts. The
36  * class also reports the version of Yazd.
37  * <p>
38  * At the moment, properties are stored in a Java Properties file. In a version
39  * of Yazd coming soon, the properties file format will move to XML. XML
40  * properties will allow hierarchical property structures which may mean the
41  * API of this class will have to change.
42  * <p>
43  * Yazd properties are only meant to be set and retrevied by core Yazd classes.
44  * Therefore, skin writers should probably ignore this class.
45  * <p>
46  * This class is implemented as a singleton since many classloaders seem to
47  * take issue with doing classpath resource loading from a static context.
48  */

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 JavaDoc managerLock = new Object JavaDoc();
57     private static String JavaDoc propsName = "/nemesis.properties";
58
59     /**
60      * Returns a Yazd property.
61      *
62      * @param name the name of the property to return.
63      * @return the property value specified by name.
64      */

65     public static String JavaDoc getProperty(String JavaDoc 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     /**
77      * Sets a Yazd property. If the property doesn't already exists, a new
78      * one will be created.
79      *
80      * @param name the name of the property being set.
81      * @param value the value of the property being set.
82      */

83     public static void setProperty(String JavaDoc name, String JavaDoc 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     /**
95      * Deletes a Yazd property. If the property doesn't exist, the method
96      * does nothing.
97      *
98      * @param name the name of the property to delete.
99      */

100     public static void deleteProperty(String JavaDoc 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     /**
112      * Returns the names of the Yazd properties.
113      *
114      * @return an Enumeration of the Yazd property names.
115      */

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     /**
128      * Returns true if the properties are readable. This method is mainly
129      * valuable at setup time to ensure that the properties file is setup
130      * correctly.
131      */

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     /**
144      * Returns true if the properties are writable. This method is mainly
145      * valuable at setup time to ensure that the properties file is setup
146      * correctly.
147      */

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     /**
160      * Returns true if the yazd.properties file exists where the path property
161      * purports that it does.
162      */

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 JavaDoc propertiesLock = new Object JavaDoc();
179     private String JavaDoc resourceURI;
180
181     /**
182      * Creates a new PropertyManager. Singleton access only.
183      */

184     private PropertyManager(String JavaDoc resourceURI) {
185         this.resourceURI = resourceURI;
186     }
187
188     /**
189      * Gets a Yazd property. Yazd properties are stored in yazd.properties.
190      * The properties file should be accesible from the classpath. Additionally,
191      * it should have a path field that gives the full path to where the
192      * file is located. Getting properties is a fast operation.
193      *
194      * @param name the name of the property to get.
195      * @return the property specified by name.
196      */

197     protected String JavaDoc getProp(String JavaDoc name) {
198         //If properties aren't loaded yet. We also need to make this thread
199
//safe, so synchronize...
200
if (properties == null) {
201             synchronized (propertiesLock) {
202                 //Need an additional check
203
if (properties == null) {
204                     loadProps();
205                 }
206             }
207         }
208         String JavaDoc property = properties.getProperty(name);
209         if (property == null) {
210             return null;
211         } else {
212             return property.trim();
213         }
214     }
215
216     /**
217      * Sets a Yazd property. Because the properties must be saved to disk
218      * every time a property is set, property setting is relatively slow.
219      */

220     protected void setProp(String JavaDoc name, String JavaDoc value) {
221         //Only one thread should be writing to the file system at once.
222
synchronized (propertiesLock) {
223             //Create the properties object if necessary.
224
if (properties == null) {
225                 loadProps();
226             }
227             properties.setProperty(name, value);
228             saveProps();
229         }
230     }
231
232     protected void deleteProp(String JavaDoc name) {
233         //Only one thread should be writing to the file system at once.
234
synchronized (propertiesLock) {
235             //Create the properties object if necessary.
236
if (properties == null) {
237                 loadProps();
238             }
239             properties.remove(name);
240             saveProps();
241         }
242     }
243
244     protected Enumeration propNames() {
245         //If properties aren't loaded yet. We also need to make this thread
246
//safe, so synchronize...
247
if (properties == null) {
248             synchronized (propertiesLock) {
249                 //Need an additional check
250
if (properties == null) {
251                     loadProps();
252                 }
253             }
254         }
255         return properties.propertyNames();
256     }
257
258     /**
259      * Loads Yazd properties from the disk.
260      */

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 JavaDoc e) {
268             log.error("Error reading properties in PropertyManager.loadProps() ", e);
269
270         } finally {
271             try {
272                 in.close();
273             } catch (Exception JavaDoc e) {
274             }
275         }
276     }
277
278     /**
279      * Saves Yazd properties to disk.
280      */

281     private void saveProps() {
282         //Now, save the properties to disk. In order for this to work, the user
283
//needs to have set the path field in the properties file. Trim
284
//the String to make sure there are no extra spaces.
285
String JavaDoc 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 JavaDoc()));
290         } catch (Exception JavaDoc 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 JavaDoc e) {
302             }
303         }
304     }
305
306     /**
307      * Returns true if the properties are readable. This method is mainly
308      * valuable at setup time to ensure that the properties file is setup
309      * correctly.
310      */

311     public boolean propFileIsReadable() {
312         try {
313             InputStream in = getClass().getResourceAsStream(resourceURI);
314             return true;
315         } catch (Exception JavaDoc e) {
316             return false;
317         }
318     }
319
320     /**
321      * Returns true if the yazd.properties file exists where the path property
322      * purports that it does.
323      */

324     public boolean propFileExists() {
325         String JavaDoc 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     /**
338      * Returns true if the properties are writable. This method is mainly
339      * valuable at setup time to ensure that the properties file is setup
340      * correctly.
341      */

342     public boolean propFileIsWritable() {
343         String JavaDoc path = getProp("path");
344         File file = new File(path);
345         if (file.isFile()) {
346             //See if we can write to the file
347
if (file.canWrite()) {
348                 return true;
349             } else {
350                 return false;
351             }
352         } else {
353             return false;
354         }
355     }
356 }
357
Popular Tags