1 19 20 package org.polepos.framework; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.util.Properties ; 27 import java.util.StringTokenizer ; 28 29 33 public class PropertiesHandler 34 { 35 private final static String DBPROPSDIR = ".bdbench"; 36 private final String mFilename; 37 private Properties mProperties; 38 39 42 public PropertiesHandler( String propertiesname ) 43 { 44 mFilename = propertiesname; 45 load(); 46 } 47 48 49 private final String getSettingsDirectoryName() 50 { 51 String home = System.getProperty( "user.home", "." ); 52 return home + File.separator + DBPROPSDIR; 53 } 54 55 private final String getSettingsFilename(){ 56 57 String fileName = mFilename; 58 59 File file = new File (fileName); 60 if(file.exists()){ 61 String 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 path){ 71 System.out.println("\nUsing settings file:"); 72 System.out.println(path + "\n"); 73 } 74 75 78 public boolean load() 79 { 80 try 81 { 82 mProperties = new Properties (); 83 File file = new File (mFilename); 84 if(file.exists()){ 85 mProperties.load(new FileInputStream (file)); 86 }else{ 87 mProperties.load( PropertiesHandler.class.getClassLoader().getResourceAsStream( mFilename ) ); 88 } 89 } 90 catch ( IOException ioex ) 91 { 92 Log.logger.warning( "Cannot load default properties." ); 93 return false; 94 } 95 96 try 97 { 98 FileInputStream in = new FileInputStream ( getSettingsFilename() ); 99 mProperties.load( in ); 100 } 101 catch ( IOException ioex ) 102 { 103 Log.logger.info( "No custom properties found. Using defaults." ); 105 106 save(); 108 } 109 110 return true; 111 } 112 113 114 117 public boolean save() 118 { 119 try 120 { 121 File dir = new File ( getSettingsDirectoryName() ); 122 dir.mkdir(); 123 FileOutputStream out = new FileOutputStream ( getSettingsFilename() ); 124 mProperties.store( out, "DB benchmark settings" ); 125 } 126 catch ( IOException ioex ) 127 { 128 Log.logger.warning( "Cannot save custom settings." ); 129 return false; 130 } 131 return true; 132 } 133 134 135 138 public String get( String key ) 139 { 140 return mProperties.getProperty(key); 141 } 142 143 144 147 public String get( String key, String defaultValue ) 148 { 149 return mProperties.getProperty( key, defaultValue ); 150 } 151 152 153 156 public void put( String key, String value ) 157 { 158 mProperties.put( key, value ); 159 } 160 161 162 165 public String [] getArray( String key ) 166 { 167 168 try{ 169 String s = get( key ); 170 171 StringTokenizer tokenizer = new StringTokenizer (s, "[ \t,;]" ); 172 int len = tokenizer.countTokens(); 173 String [] res = new String [ len ]; 174 for ( int i = 0; i < len; i++ ) 175 { 176 res[i] = tokenizer.nextToken(); 177 } 178 179 return res; 180 }catch(Exception e){ 181 System.out.println("Key not available in " + mFilename +":\n" + key + "\n"); 182 } 183 return null; 184 } 185 186 187 190 public int[] getIntArray( String key ) 191 { 192 String s = get( key ); 193 194 StringTokenizer tokenizer = new StringTokenizer (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 209 public boolean getBoolean( String key ) 210 { 211 String val = mProperties.getProperty(key); 212 return Boolean.valueOf( val ).booleanValue(); 213 } 214 215 216 219 public Properties getProperties() 220 { 221 return mProperties; 222 } 223 } 224
| Popular Tags
|