1 21 package org.jahia.utils.properties; 22 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.util.Enumeration ; 31 import java.util.Properties ; 32 import java.util.Vector ; 33 34 import org.jahia.utils.JahiaConsole; 35 import org.jahia.utils.JahiaTools; 36 37 38 39 54 public class PropertiesManager 55 { 56 public Properties properties = new Properties (); 57 public String propertiesFilePath; 58 59 60 61 65 public PropertiesManager() 66 { 67 } 70 71 72 78 public PropertiesManager( String propertiesFilePath ) 79 { 80 this.propertiesFilePath = propertiesFilePath; 81 this.loadProperties(); 82 } 84 85 86 92 public PropertiesManager( Properties properties ) 93 { 94 this.properties = properties; 95 } 97 98 99 103 private void loadProperties() 104 { 105 FileInputStream inputStream = null; 106 try 107 { 108 inputStream = new FileInputStream ( propertiesFilePath ); 109 properties = new Properties (); 110 properties.load( inputStream ); 111 112 } catch (IOException ioe) { 113 JahiaConsole.println( "PropertiesManager", "IOException on loadProperties()." ); 114 } catch (SecurityException se) { 115 JahiaConsole.println( "PropertiesManager", "SecurityException on file ["+propertiesFilePath+"]"); 116 } finally { 117 try { 118 inputStream.close(); 119 inputStream = null; 120 } catch (Throwable t){ 121 t.printStackTrace(); 122 } 123 } 124 } 126 127 128 135 public String getProperty( String propertyName ) 136 { 137 return properties.getProperty( propertyName ); 138 } 140 141 142 149 public void setProperty( String propertyName, 150 String propvalue ) 151 { 152 properties.setProperty( propertyName, propvalue ); 153 } 155 156 157 163 public void removeProperty( String propertyName ) 164 { 165 properties.remove( propertyName ); 166 } 168 169 170 175 public void storeProperties() 176 { 177 try { 178 storeProperties( this.propertiesFilePath ); 179 } catch (NullPointerException npe) { 180 JahiaConsole.println( "PropertiesManager", "NullPointerException on storeProperties()." ); 181 } 182 } 184 185 186 194 public void storeProperties( String propertiesFilePath ) 195 throws NullPointerException 196 { 197 boolean baseObjectExists = true; 198 Vector bufferVector = new Vector (); 199 String lineReaded = null; 200 201 File propertiesFileObject = new File ( propertiesFilePath ); 202 File propertiesFileFolder = propertiesFileObject.getParentFile(); 203 204 if(!propertiesFileFolder.exists()) { 206 propertiesFileFolder.mkdirs(); 207 propertiesFileFolder = null; 208 } 209 210 File propertiesFileObjectBase = null; 212 try { 213 propertiesFileObjectBase = new File ( this.propertiesFilePath ); 214 215 } catch (NullPointerException npe) { 216 baseObjectExists = false; 217 } finally { 218 propertiesFileObjectBase = null; 219 } 220 221 try 222 { 223 if(baseObjectExists) 224 { 225 BufferedReader buffered = new BufferedReader ( new FileReader ( this.propertiesFilePath ) ); 226 int position = 0; 227 228 Vector allProperties = new Vector (); 230 Enumeration allPropertiesEnumeration = properties.propertyNames(); 231 while(allPropertiesEnumeration.hasMoreElements()) { 232 allProperties.add( (String ) allPropertiesEnumeration.nextElement() ); 233 } 234 235 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 propertyNames = allProperties.elements(); 244 245 while(propertyNames.hasMoreElements() && !propertyFound) 246 { 247 String propertyName = (String ) propertyNames.nextElement(); 248 String 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 thisLineBuffer = new StringBuffer (); 257 thisLineBuffer.append( lineReaded.substring(0,position+1) ); 258 thisLineBuffer.append( " " ); 259 thisLineBuffer.append( JahiaTools.string2Property( propvalue ) ); 260 bufferVector.add( thisLineBuffer.toString() ); 261 262 allProperties.remove( propertyName ); 264 } 265 } 266 } 267 } 268 else 269 { 270 bufferVector.add( lineReaded.trim() ); 272 } 273 } catch (IndexOutOfBoundsException ioobe) { 274 } 275 } 276 277 Enumeration restantPropertyNames = allProperties.elements(); 279 while(restantPropertyNames.hasMoreElements()) 280 { 281 String restantPropertyName = (String ) restantPropertyNames.nextElement(); 282 StringBuffer specialLineBuffer = new StringBuffer (); 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 buffered.close(); 294 295 writeTheFile( propertiesFilePath, bufferVector ); 297 } 298 else 299 { 300 FileOutputStream outputStream = new FileOutputStream ( propertiesFileObject ); 301 properties.store( outputStream, "This file has been written by Jahia." ); 302 outputStream.close(); 303 } 304 } catch (java.io.IOException ioe) { 305 } 306 } 308 309 310 317 private void writeTheFile( String propertiesFilePath, 318 Vector bufferVector ) 319 { 320 321 File thisFile = null; 322 FileWriter fileWriter = null; 323 StringBuffer outputBuffer = null; 324 325 try 326 { 327 thisFile = new File ( propertiesFilePath ); 328 fileWriter = new FileWriter ( thisFile ); 329 outputBuffer = new StringBuffer (); 330 331 for(int i=0; i < bufferVector.size(); i++) { 332 outputBuffer.append((String ) bufferVector.get(i)); 333 outputBuffer.append("\n"); 334 } 335 336 fileWriter.write( outputBuffer.toString() ); 337 } catch (java.io.IOException ioe) { 338 } finally { 339 try { 340 fileWriter.close(); 341 }catch ( java.io.IOException ioe2 ){ 342 } 343 fileWriter = null; 344 thisFile = null; 345 } 346 } 348 349 350 356 public Properties getPropertiesObject() 357 { 358 return properties; 359 } 361 362 } | Popular Tags |