KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > framework > PropertiesHandler


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.framework;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  *
31  * @author Herkules
32  */

33 public class PropertiesHandler
34 {
35     private final static String JavaDoc DBPROPSDIR = ".bdbench";
36     private final String JavaDoc mFilename;
37     private Properties JavaDoc mProperties;
38     
39     /**
40      * Creates a new instance of BenchmarkSettings.
41      */

42     public PropertiesHandler( String JavaDoc propertiesname )
43     {
44         mFilename = propertiesname;
45         load();
46     }
47     
48
49     private final String JavaDoc getSettingsDirectoryName()
50     {
51         String JavaDoc home = System.getProperty( "user.home", "." );
52         return home + File.separator + DBPROPSDIR;
53     }
54     
55     private final String JavaDoc getSettingsFilename(){
56         
57         String JavaDoc fileName = mFilename;
58         
59         File JavaDoc file = new File JavaDoc(fileName);
60         if(file.exists()){
61             String JavaDoc path = file.getAbsolutePath();
62             reportSettingsFile(path);
63             return path;
64         }
65         fileName = getSettingsDirectoryName() + File.separator + fileName;
66         reportSettingsFile(fileName);
67         return fileName;
68     }
69     
70     private void reportSettingsFile(String JavaDoc path){
71         System.out.println("\nUsing settings file:");
72         System.out.println(path + "\n");
73     }
74     
75     /**
76      * Load default and custom settings.
77      */

78     public boolean load()
79     {
80         try
81         {
82             mProperties = new Properties JavaDoc();
83             File JavaDoc file = new File JavaDoc(mFilename);
84             if(file.exists()){
85                 mProperties.load(new FileInputStream JavaDoc(file));
86             }else{
87                 mProperties.load( PropertiesHandler.class.getClassLoader().getResourceAsStream( mFilename ) );
88             }
89         }
90         catch ( IOException JavaDoc ioex )
91         {
92             Log.logger.warning( "Cannot load default properties." );
93             return false;
94         }
95
96         try
97         {
98             FileInputStream JavaDoc in = new FileInputStream JavaDoc( getSettingsFilename() );
99             mProperties.load( in );
100         }
101         catch ( IOException JavaDoc ioex )
102         {
103             // no custom file present .... thats ok.
104
Log.logger.info( "No custom properties found. Using defaults." );
105
106             // create the missing file
107
save();
108         }
109         
110         return true;
111     }
112     
113
114     /**
115      * Persist the custom settings.
116      */

117     public boolean save()
118     {
119         try
120         {
121             File JavaDoc dir = new File JavaDoc( getSettingsDirectoryName() );
122             dir.mkdir();
123             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc( getSettingsFilename() );
124             mProperties.store( out, "DB benchmark settings" );
125         }
126         catch ( IOException JavaDoc ioex )
127         {
128             Log.logger.warning( "Cannot save custom settings." );
129             return false;
130         }
131         return true;
132     }
133     
134     
135     /**
136      * same as <code>Properties#getProperty()</code>
137      */

138     public String JavaDoc get( String JavaDoc key )
139     {
140         return mProperties.getProperty(key);
141     }
142
143     
144     /**
145      * same as <code>Properties#getProperty()</code>
146      */

147     public String JavaDoc get( String JavaDoc key, String JavaDoc defaultValue )
148     {
149         return mProperties.getProperty( key, defaultValue );
150     }
151     
152     
153     /**
154      * same as <code>Properties#put()</code>
155      */

156     public void put( String JavaDoc key, String JavaDoc value )
157     {
158         mProperties.put( key, value );
159     }
160     
161
162     /**
163      * retrieve an array that might be formatted like 1,2,3 or [1,2,3] or 1 2 3 or 1;2;3 ...
164      */

165     public String JavaDoc[] getArray( String JavaDoc key )
166     {
167         
168         try{
169             String JavaDoc s = get( key );
170             
171             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(s, "[ \t,;]" );
172             int len = tokenizer.countTokens();
173             String JavaDoc[] res = new String JavaDoc[ len ];
174             for ( int i = 0; i < len; i++ )
175             {
176                 res[i] = tokenizer.nextToken();
177             }
178             
179             return res;
180         }catch(Exception JavaDoc e){
181             System.out.println("Key not available in " + mFilename +":\n" + key + "\n");
182         }
183         return null;
184     }
185     
186     
187     /**
188      * retrieve an array that might be formatted like 1,2,3 or [1,2,3] or 1 2 3 or 1;2;3 ...
189      */

190     public int[] getIntArray( String JavaDoc key )
191     {
192         String JavaDoc s = get( key );
193         
194         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(s, "[ \t,;]" );
195         int len = tokenizer.countTokens();
196         int[] res = new int[ len ];
197         for ( int i = 0; i < len; i++ )
198         {
199             res[i] = Integer.parseInt( tokenizer.nextToken() );
200         }
201         
202         return res;
203     }
204
205     
206     /**
207      * retrieve a flag
208      */

209     public boolean getBoolean( String JavaDoc key )
210     {
211         String JavaDoc val = mProperties.getProperty(key);
212         return Boolean.valueOf( val ).booleanValue();
213     }
214     
215     
216     /**
217      * Retrieve the properties object handled here.
218      */

219     public Properties JavaDoc getProperties()
220     {
221         return mProperties;
222     }
223 }
224
Popular Tags