1 53 54 package com.Yasna.forum; 55 56 import com.Yasna.forum.database.DbConnectionManager; 57 58 import javax.naming.InitialContext ; 59 import java.util.*; 60 import java.io.*; 61 import java.sql.ResultSet ; 62 import java.sql.Connection ; 63 import java.sql.PreparedStatement ; 64 import java.sql.SQLException ; 65 66 82 public class PropertyManager { 83 84 87 private static final int MAJOR_VERSION = 3; 88 89 92 private static final int MINOR_VERSION = 0; 93 94 97 private static final int REVISION_VERSION = 1; 98 99 private static PropertyManager manager = null; 100 private static Object managerLock = new Object (); 101 private static String propsName = "/yazd.properties"; 102 private static boolean loadFromDb = true; 103 private static final String LOAD_PROPERTIES="select name,propvalue from yazdProps"; 104 private static final String DELETE_PROPERTIES="delete from yazdProps"; 105 private static final String INSERT_PROPERTIES="insert into yazdProps(name,propvalue) values (?,?)"; 106 107 113 public static String getProperty(String name) { 114 if (manager == null) { 115 synchronized(managerLock) { 116 if (manager == null) { 117 manager = new PropertyManager(propsName); 118 } 119 } 120 } 121 return manager.getProp(name); 122 } 123 public static void setPath(String path){ 124 if (manager == null) { 125 synchronized(managerLock) { 126 if (manager == null) { 127 manager = new PropertyManager(propsName); 128 } 129 } 130 } 131 manager.setProp("path",path); 132 } 133 140 public static void setProperty(String name, String value) { 141 if (manager == null) { 142 synchronized(managerLock) { 143 if (manager == null) { 144 manager = new PropertyManager(propsName); 145 } 146 } 147 } 148 manager.setProp(name, value); 149 } 150 151 157 public static void deleteProperty(String name) { 158 if (manager == null) { 159 synchronized(managerLock) { 160 if (manager == null) { 161 manager = new PropertyManager(propsName); 162 } 163 } 164 } 165 manager.deleteProp(name); 166 } 167 168 173 public static Enumeration propertyNames() { 174 if (manager == null) { 175 synchronized(managerLock) { 176 if (manager == null) { 177 manager = new PropertyManager(propsName); 178 } 179 } 180 } 181 return manager.propNames(); 182 } 183 184 189 public static boolean propertyFileIsReadable() { 190 if (manager == null) { 191 synchronized(managerLock) { 192 if (manager == null) { 193 manager = new PropertyManager(propsName); 194 } 195 } 196 } 197 return manager.propFileIsReadable(); 198 } 199 200 205 public static boolean propertyFileIsWritable() { 206 if (manager == null) { 207 synchronized(managerLock) { 208 if (manager == null) { 209 manager = new PropertyManager(propsName); 210 } 211 } 212 } 213 return manager.propFileIsWritable(); 214 } 215 216 220 public static boolean propertyFileExists() { 221 if (manager == null) { 222 synchronized(managerLock) { 223 if (manager == null) { 224 manager = new PropertyManager(propsName); 225 } 226 } 227 } 228 return manager.propFileExists(); 229 } 230 231 234 public static String getYazdVersion() { 235 return MAJOR_VERSION + "." + MINOR_VERSION + "." + REVISION_VERSION; 236 } 237 238 241 public static int getYazdVersionMajor() { 242 return MAJOR_VERSION; 243 } 244 245 248 public static int getYazdVersionMinor() { 249 return MINOR_VERSION; 250 } 251 252 255 public static int getYazdVersionRevision() { 256 return REVISION_VERSION; 257 } 258 259 private Properties properties = null; 260 private Object propertiesLock = new Object (); 261 private String resourceURI; 262 263 266 private PropertyManager(String resourceURI) { 267 this.resourceURI = resourceURI; 268 } 269 270 279 protected String getProp(String name) { 280 if (properties == null) { 283 synchronized(propertiesLock) { 284 if (properties == null) { 286 loadProps(); 287 } 288 } 289 } 290 String property = properties.getProperty(name); 291 if (property == null) { 292 return null; 293 } 294 else { 295 return property.trim(); 296 } 297 } 298 299 303 protected void setProp(String name, String value) { 304 synchronized (propertiesLock) { 306 if (properties == null) { 308 loadProps(); 309 } 310 properties.setProperty(name, value); 311 saveProps(); 312 } 313 } 314 315 protected void deleteProp(String name) { 316 synchronized (propertiesLock) { 318 if (properties == null) { 320 loadProps(); 321 } 322 properties.remove(name); 323 saveProps(); 324 } 325 } 326 327 protected Enumeration propNames() { 328 if (properties == null) { 331 synchronized(propertiesLock) { 332 if (properties == null) { 334 loadProps(); 335 } 336 } 337 } 338 return properties.propertyNames(); 339 } 340 341 344 private void loadProps() { 345 switch(getLoadSource()){ 346 case 2: 347 System.err.println("Loading Properties from database"); 348 loadPropsFromDb(); 349 break; 350 default: 351 System.err.println("Loading Properties from yolanda.properties"); 352 loadPropsFromFile(); 353 } 354 355 } 356 private void loadPropsFromFile() { 357 properties = new Properties(); 358 InputStream in = null; 359 try { 360 in = getClass().getResourceAsStream(resourceURI); 361 properties.load(in); 362 } 363 catch (Exception e) { 364 System.err.println("Error reading Yolanda properties in PropertyManager.loadProps() " + e); 365 e.printStackTrace(); 366 } 367 finally { 368 try { 369 in.close(); 370 } catch (Exception e) { } 371 } 372 } 373 private void loadPropsFromDb() { 374 properties = new Properties(); 375 Connection con = null; 376 PreparedStatement pstmt = null; 377 try { 378 con = DbConnectionManager.getConnection(); 379 pstmt = con.prepareStatement(LOAD_PROPERTIES); 380 ResultSet rs = pstmt.executeQuery(); 381 while(rs.next()) { 382 String name = rs.getString("name"); 383 String value = rs.getString("propvalue"); 384 properties.put(name, value); 385 } 386 } 387 catch( Exception e ) { 388 loadFromDb=false; 389 System.err.println("Error in Property Manager:loadProperties():" + e.getMessage()); 390 } 391 finally { 392 try { pstmt.close(); } 393 catch (Exception e) { e.printStackTrace(); } 394 try { con.close(); } 395 catch (Exception e) { e.printStackTrace(); } 396 } 397 } 398 399 400 401 404 private void saveProps() { 405 if(propFileIsWritable()){ 409 String path = properties.getProperty("path").trim(); 410 OutputStream out = null; 411 try { 412 out = new FileOutputStream(path); 413 properties.store(out, "yazd.properties -- " + (new java.util.Date ())); 414 } 415 catch (Exception ioe) { 416 System.err.println("There was an error writing yazd.properties to " + path + ". " + 417 "Ensure that the path exists and that the Yazd process has permission " + 418 "to write to it -- " + ioe); 419 ioe.printStackTrace(); 420 } 421 finally { 422 try { 423 out.close(); 424 } catch (Exception e) { } 425 } 426 } 427 if(properties.getProperty("setup")!=null && properties.getProperty("setup").equals("true")){ 430 Connection con = null; 431 PreparedStatement pstmt = null; 432 try { 433 con = DbConnectionManager.getConnection(); 434 pstmt = con.prepareStatement(DELETE_PROPERTIES); 436 pstmt.execute(); 437 pstmt.close(); 438 pstmt = con.prepareStatement(INSERT_PROPERTIES); 440 Enumeration enume = properties.keys(); 441 while (enume.hasMoreElements()) { 442 String name = (String )enume.nextElement(); 443 String value = (String )properties.get(name); 444 pstmt.setString(1, name); 445 pstmt.setString(2, value); 446 pstmt.executeUpdate(); 447 } 448 } 449 catch( SQLException sqle ) { 450 System.err.println(sqle); 451 } 452 finally { 453 try { pstmt.close(); } 454 catch (Exception e) { e.printStackTrace(); } 455 try { con.close(); } 456 catch (Exception e) { e.printStackTrace(); } 457 } 458 459 } 460 } 461 468 469 private int getLoadSource(){ 470 int loadsource=1; try{ 473 InitialContext ctxt = new InitialContext (); 474 ctxt.lookup(DbConnectionManager.CONTEXT_JDBC_NAME); 475 loadsource=2; 476 }catch(Exception e){ 477 478 } 479 return loadsource; 480 481 } 482 483 488 public boolean propFileIsReadable() { 489 try { 490 InputStream in = getClass().getResourceAsStream(resourceURI); 491 return true; 492 } 493 catch (Exception e) { 494 return false; 495 } 496 } 497 498 502 public boolean propFileExists() { 503 String path = getProp("path"); 504 if( path == null ) { 505 return false; 506 } 507 File file = new File(path); 508 if (file.isFile()) { 509 return true; 510 } 511 else { 512 return false; 513 } 514 } 515 516 521 public boolean propFileIsWritable() { 522 String path = getProp("path"); 523 File file = new File(path); 524 if (file.isFile()) { 525 if (file.canWrite()) { 527 return true; 528 } 529 else { 530 return false; 531 } 532 } 533 else { 534 return false; 535 } 536 } 537 } 538 | Popular Tags |