KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > properties > PropertiesManager


1 // $Id: PropertiesManager.java 3567 2003-06-30 22:32:03Z shuber $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// PropertiesManager
15
//
16
// 27.03.2001 AK added in jahia.
17
// 28.03.2001 AK mammouth fix into method storeProperties.
18
// 12.08.2001 NK Close input stream after reading from property file, otherwhise the file cannot be deleted.
19
//
20

21 package org.jahia.utils.properties;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.jahia.utils.JahiaConsole;
35 import org.jahia.utils.JahiaTools;
36
37
38
39 /**
40  * desc: This class provides to you a *super interface* for properties.
41  * It allows you to create a new properties file, set properties, remove
42  * properties, get properties, get properties from an existing properties
43  * object or from a flat file, get a complete properties object, and you can
44  * store the new properties object where you want on the filesystem. The store
45  * method keep your base properties file design (not like the store() method
46  * from java properties object)!
47  *
48  * Copyright: Copyright (c) 2002
49  * Company: Jahia Ltd
50  *
51  * @author Alexandre Kraft
52  * @version 1.0
53  */

54 public class PropertiesManager
55 {
56     public Properties JavaDoc properties = new Properties JavaDoc();
57     public String JavaDoc propertiesFilePath;
58
59
60
61     /**
62      * Default constructor.
63      * @author Alexandre Kraft
64      */

65     public PropertiesManager()
66     {
67         // do nothing :o)
68
} // end constructor
69

70
71
72     /**
73      * Default constructor.
74      * @author Alexandre Kraft
75      *
76      * @param propertiesFilePath The filesystem path from the file is loaded.
77      */

78     public PropertiesManager( String JavaDoc propertiesFilePath )
79     {
80         this.propertiesFilePath = propertiesFilePath;
81         this.loadProperties();
82     } // end constructor
83

84
85
86     /**
87      * Default constructor.
88      * @author Alexandre Kraft
89      *
90      * @param properties The properties object used to define base properties.
91      */

92     public PropertiesManager( Properties JavaDoc properties )
93     {
94         this.properties = properties;
95     } // end constructor
96

97
98
99     /**
100      * Load a complete properties file in memory by its filename.
101      * @author Alexandre Kraft
102      */

103     private void loadProperties()
104     {
105         FileInputStream JavaDoc inputStream = null;
106         try
107         {
108             inputStream = new FileInputStream JavaDoc( propertiesFilePath );
109             properties = new Properties JavaDoc();
110             properties.load( inputStream );
111
112         } catch (IOException JavaDoc ioe) {
113             JahiaConsole.println( "PropertiesManager", "IOException on loadProperties()." );
114         } catch (SecurityException JavaDoc se) {
115             JahiaConsole.println( "PropertiesManager", "SecurityException on file ["+propertiesFilePath+"]");
116         } finally {
117             try {
118                 inputStream.close();
119                 inputStream = null;
120             } catch (Throwable JavaDoc t){
121                 t.printStackTrace();
122             }
123         }
124     } // end loadProperties
125

126
127
128     /**
129      * Get a property value by its name.
130      * @author Alexandre Kraft
131      *
132      * @param propertyName The property name to get its value.
133      * @return Returns a String containing the value of the property name.
134      */

135     public String JavaDoc getProperty( String JavaDoc propertyName )
136     {
137         return properties.getProperty( propertyName );
138     } // end getProperty
139

140
141
142     /**
143      * Set a property value by its name.
144      * @author Alexandre Kraft
145      *
146      * @param propertyName The property name to set.
147      * @param propvalue The property value to set.
148      */

149     public void setProperty( String JavaDoc propertyName,
150                              String JavaDoc propvalue )
151     {
152         properties.setProperty( propertyName, propvalue );
153     } // end setProperty
154

155
156
157     /**
158      * Remove a property by its name.
159      * @author Alexandre Kraft
160      *
161      * @param propertyName The property name to remove.
162      */

163     public void removeProperty( String JavaDoc propertyName )
164     {
165         properties.remove( propertyName );
166     } // end removeProperty
167

168
169
170     /**
171      * Store new properties and values in the properties file.
172      * The file writed is the same, using the same file path, as the file loaded before.
173      * @author Alexandre Kraft
174      */

175     public void storeProperties()
176     {
177         try {
178             storeProperties( this.propertiesFilePath );
179         } catch (NullPointerException JavaDoc npe) {
180             JahiaConsole.println( "PropertiesManager", "NullPointerException on storeProperties()." );
181         }
182     } // end storeProperties
183

184
185
186     /**
187      * Store new properties and values in the properties file.
188      * If the file where you want to write doesn't exists, the file is created.
189      * @author Alexandre Kraft
190      * @author Khue N'Guyen
191      *
192      * @param propertiesFilePath The filesystem path where the file is saved.
193      */

194     public void storeProperties( String JavaDoc propertiesFilePath )
195     throws NullPointerException JavaDoc
196     {
197         boolean baseObjectExists = true;
198         Vector JavaDoc bufferVector = new Vector JavaDoc();
199         String JavaDoc lineReaded = null;
200
201         File JavaDoc propertiesFileObject = new File JavaDoc( propertiesFilePath );
202         File JavaDoc propertiesFileFolder = propertiesFileObject.getParentFile();
203
204         // check if the destination folder exists and create it if needed...
205
if(!propertiesFileFolder.exists()) {
206             propertiesFileFolder.mkdirs();
207             propertiesFileFolder = null;
208         }
209
210         // try to create a file object via the propertiesFilePath...
211
File JavaDoc propertiesFileObjectBase = null;
212         try {
213             propertiesFileObjectBase = new File JavaDoc( this.propertiesFilePath );
214                 
215         } catch (NullPointerException JavaDoc npe) {
216             baseObjectExists = false;
217         } finally {
218             propertiesFileObjectBase = null;
219         }
220
221         try
222         {
223             if(baseObjectExists)
224             {
225                 BufferedReader JavaDoc buffered = new BufferedReader JavaDoc( new FileReader JavaDoc( this.propertiesFilePath ) );
226                 int position = 0;
227
228                 // compose all properties vector, used to find the new properties...
229
Vector JavaDoc allProperties = new Vector JavaDoc();
230                 Enumeration JavaDoc allPropertiesEnumeration = properties.propertyNames();
231                 while(allPropertiesEnumeration.hasMoreElements()) {
232                     allProperties.add( (String JavaDoc) allPropertiesEnumeration.nextElement() );
233                 }
234
235                 // parse the file...
236
while((lineReaded = buffered.readLine()) != null)
237                 {
238                     try {
239                         if(!lineReaded.trim().equals("") && !lineReaded.trim().substring(0,1).equals("#"))
240                         {
241                             boolean propertyFound = false;
242                             int countThisLine = 0;
243                             Enumeration JavaDoc propertyNames = allProperties.elements();
244
245                             while(propertyNames.hasMoreElements() && !propertyFound)
246                             {
247                                 String JavaDoc propertyName = (String JavaDoc) propertyNames.nextElement();
248                                 String JavaDoc propvalue = properties.getProperty( propertyName );
249
250                                 if(lineReaded.indexOf(propertyName + " ") == 0) {
251                                     position = lineReaded.indexOf("=");
252                                     if(position >= 0)
253                                     {
254                                         propertyFound = true;
255
256                                         StringBuffer JavaDoc thisLineBuffer = new StringBuffer JavaDoc();
257                                         thisLineBuffer.append( lineReaded.substring(0,position+1) );
258                                         thisLineBuffer.append( " " );
259                                         thisLineBuffer.append( JahiaTools.string2Property( propvalue ) );
260                                         bufferVector.add( thisLineBuffer.toString() );
261
262                                         // remove this line from allProperties to affine the search and to find new properties...
263
allProperties.remove( propertyName );
264                                     }
265                                 }
266                             }
267                         }
268                         else
269                         {
270                             // this is a special line only for layout, like a comment or a blank line...
271
bufferVector.add( lineReaded.trim() );
272                         }
273                     } catch (IndexOutOfBoundsException JavaDoc ioobe) {
274                     }
275                 }
276
277                 // add not found properties at the end of the file (and the jahia.properties layout is keeping)...
278
Enumeration JavaDoc restantPropertyNames = allProperties.elements();
279                 while(restantPropertyNames.hasMoreElements())
280                 {
281                     String JavaDoc restantPropertyName = (String JavaDoc) restantPropertyNames.nextElement();
282                     StringBuffer JavaDoc specialLineBuffer = new StringBuffer JavaDoc();
283                     specialLineBuffer.append( restantPropertyName );
284                     for(int i=0; i<55-restantPropertyName.length(); i++) {
285                         specialLineBuffer.append( " " );
286                     }
287                     specialLineBuffer.append( "= " );
288                     specialLineBuffer.append( properties.getProperty( restantPropertyName ) );
289                     bufferVector.add( specialLineBuffer.toString() );
290                 }
291
292                 // close the buffered filereader...
293
buffered.close();
294
295                 // write the file...
296
writeTheFile( propertiesFilePath, bufferVector );
297             }
298             else
299             {
300                 FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc( propertiesFileObject );
301                 properties.store( outputStream, "This file has been written by Jahia." );
302                 outputStream.close();
303             }
304         } catch (java.io.IOException JavaDoc ioe) {
305         }
306     } // end storeProperties
307

308
309
310     /**
311      * Write the file composed by the storeProperties() method, using
312      * @author Alexandre Kraft
313      *
314      * @param propertiesFilePath The filesystem path where the file is saved.
315      * @param bufferVector Vector containing all the string lines of the new file.
316      */

317     private void writeTheFile( String JavaDoc propertiesFilePath,
318                                Vector JavaDoc bufferVector )
319     {
320
321         File JavaDoc thisFile = null;
322         FileWriter JavaDoc fileWriter = null;
323         StringBuffer JavaDoc outputBuffer = null;
324
325         try
326         {
327             thisFile = new File JavaDoc( propertiesFilePath );
328             fileWriter = new FileWriter JavaDoc( thisFile );
329             outputBuffer = new StringBuffer JavaDoc();
330
331             for(int i=0; i < bufferVector.size(); i++) {
332                 outputBuffer.append((String JavaDoc) bufferVector.get(i));
333                 outputBuffer.append("\n");
334             }
335
336             fileWriter.write( outputBuffer.toString() );
337         } catch (java.io.IOException JavaDoc ioe) {
338         } finally {
339             try {
340                 fileWriter.close();
341             }catch ( java.io.IOException JavaDoc ioe2 ){
342             }
343             fileWriter = null;
344             thisFile = null;
345         }
346     } // end writeTheFile
347

348
349
350     /**
351      * Get the properties object for the instance of this class.
352      * @author Alexandre Kraft
353      *
354      * @return The properties object for the instance of this class.
355      */

356     public Properties JavaDoc getPropertiesObject()
357     {
358         return properties;
359     } // end getPropertiesObject
360

361
362 } // end PropertiesManager
Popular Tags