1 7 8 package org.enhydra.server; 9 10 import org.enhydra.util.ConfigFileInterface; 11 12 import com.lutris.util.Config; 13 import com.lutris.util.ConfigException; 14 import com.lutris.util.KeywordValueException; 15 16 21 public class DatabaseEdit { 22 23 26 private String [] databases = null; 27 28 32 private String currentDatabase = null; 33 34 40 private String defaultDatabase = "N/A"; 41 private String debug = "N/A"; 42 private String dBnameClassType = "N/A"; 43 private String dBnameJdbcDriver = "N/A"; 44 private String dBnameConnectionUrl = "N/A"; 45 private String dBnameConnectionUser = "N/A"; 46 private String dBnameConnectionPassword = "N/A"; 47 private String dBnameConnectionMaxPoolSize = "N/A"; 48 private String dBnameConnectionAllocationTimeout = "N/A"; 49 private String dBnameConnectionLogging = "N/A"; 50 private String dBnameObjectIdCacheSize = "N/A"; 51 private String dBnameObjectIdMinValue = "N/A"; 52 53 56 private Config configTemp; 57 58 62 private Config configOriginal; 63 64 72 public DatabaseEdit (Config config) throws ConfigException, KeywordValueException { 73 this.configOriginal = config; this.configTemp = config.getClonedConfig(); 76 if(configTemp.containsKey("DatabaseManager.Databases")) 77 databases = configTemp.getStrings("DatabaseManager.Databases"); 78 if(configTemp.containsKey("DatabaseManager.DefaultDatabase")) { 79 defaultDatabase = configTemp.getString("DatabaseManager.DefaultDatabase"); 80 currentDatabase = new String (defaultDatabase); 81 } 82 if (configTemp.containsKey("DatabaseManager.Debug")) 83 debug = configTemp.getString("DatabaseManager.Debug"); 84 if (currentDatabase == null && databases != null) 85 currentDatabase = new String (databases[0]); 86 87 this.refreshAllDbParameters(currentDatabase); 88 } 89 90 96 public void refreshAllDbParameters(String dBname) throws KeywordValueException { 97 98 this.databases = null; 99 this.currentDatabase = null; 100 101 this.defaultDatabase = "N/A"; 102 this.debug = "N/A"; 103 this.dBnameClassType = "N/A"; 104 this.dBnameJdbcDriver = "N/A"; 105 this.dBnameConnectionUrl = "N/A"; 106 this.dBnameConnectionUser = "N/A"; 107 this.dBnameConnectionPassword = "N/A"; 108 this.dBnameConnectionMaxPoolSize = "N/A"; 109 this.dBnameConnectionAllocationTimeout = "N/A"; 110 this.dBnameConnectionLogging = "N/A"; 111 this.dBnameObjectIdCacheSize = "N/A"; 112 this.dBnameObjectIdMinValue = "N/A"; 113 114 Config tempParentConfig = null; 115 Config tempConfig = (Config) configTemp.getSection("DatabaseManager"); 117 if (tempConfig != null) { 118 if(tempConfig.containsKey("Databases")) { 119 this.databases = tempConfig.getStrings("Databases"); 120 if(this.existDbName(dBname)) 122 this.currentDatabase = dBname; 123 } 124 if(tempConfig.containsKey("DefaultDatabase")) 125 defaultDatabase = tempConfig.getString("DefaultDatabase"); 126 127 if (tempConfig.containsKey("Debug")) 128 debug = tempConfig.getString("Debug"); 129 130 if(currentDatabase == null) 131 return; 132 133 tempParentConfig = tempConfig; 135 tempConfig = (Config) tempConfig.getSection("DB." + dBname); 136 if(tempConfig!=null) { 137 if (tempConfig.containsKey("ClassType")) 138 dBnameClassType = tempConfig.getString("ClassType"); 139 if (tempConfig.containsKey("JdbcDriver")) 140 dBnameJdbcDriver = tempConfig.getString("JdbcDriver"); 141 142 tempParentConfig = tempConfig; 143 tempConfig = (Config) tempConfig.getSection("Connection"); 144 if(tempConfig!=null) { 145 if (tempConfig.containsKey("Url")) 146 dBnameConnectionUrl = tempConfig.getString("Url"); 147 if (tempConfig.containsKey("User")) 148 dBnameConnectionUser = tempConfig.getString("User"); 149 if (tempConfig.containsKey("Password")) 150 dBnameConnectionPassword = tempConfig.getString("Password"); 151 if (tempConfig.containsKey("MaxPoolSize")) 152 dBnameConnectionMaxPoolSize = tempConfig.getString("MaxPoolSize"); 153 if (tempConfig.containsKey("AllocationTimeout")) 154 dBnameConnectionAllocationTimeout = tempConfig.getString("AllocationTimeout"); 155 if (tempConfig.containsKey("Logging")) 156 dBnameConnectionLogging = tempConfig.getString("Logging"); 157 } 158 tempConfig = tempParentConfig; 159 tempConfig = (Config) tempConfig.getSection("ObjectId"); 160 if(tempConfig!=null) { 161 if (tempConfig.containsKey("CacheSize")) 162 dBnameObjectIdCacheSize = tempConfig.getString("CacheSize"); 163 if (tempConfig.containsKey("MinValue")) 164 dBnameObjectIdMinValue = tempConfig.getString("MinValue"); 165 } 166 } 167 } 168 } 169 170 177 public boolean existDbName(String dBname) { 178 if(dBname == null || databases == null) 179 return false; 180 for (int i = 0; i < this.databases.length; i++) { 181 if (databases[i].equalsIgnoreCase(dBname)) 182 return true; 183 } 184 return false; 185 186 } 187 188 196 public boolean addDatabase(String dBname) throws KeywordValueException{ 197 if(this.existDbName(dBname)) 198 return false; 199 200 String [] newDbString = null; 201 if(this.databases == null) newDbString = new String [1]; 203 else 204 newDbString = new String [this.databases.length+1]; 205 206 newDbString[newDbString.length-1] = dBname; 207 208 for (int i = 0; i < newDbString.length-1; i++) 209 newDbString[i] = this.databases[i]; 210 211 this.configTemp.set("DatabaseManager.Databases", newDbString); 212 this.databases = newDbString; 213 214 this.setDBnameClassType("Standard", dBname); 217 this.setDBnameJdbcDriver("sun.jdbc.odbc.JdbcOdbcDriver", dBname); 218 this.setDBnameConnectionAllocationTimeout("10000", dBname); 219 this.setDBnameConnectionLogging("false", dBname); 220 this.setDBnameConnectionMaxPoolSize("30", dBname); 221 this.setDBnameConnectionUrl("jdbc:odbc:"+dBname, dBname); 222 this.setDBnameConnectionPassword("tiger", dBname); 223 this.setDBnameConnectionUser("miga", dBname); 224 this.setDBnameObjectIdCacheSize("20", dBname); 225 this.setDBnameObjectIdMinValue("1000000", dBname); 226 227 return true; 228 } 229 230 238 public boolean removeDatabase(String dBname) throws KeywordValueException { 239 if(!this.existDbName(dBname)) 240 return false; 241 String [] newDbString = new String [this.databases.length-1]; 242 243 for (int i=0,j=0; i < newDbString.length; i++) { 244 if(!this.databases[j].equalsIgnoreCase(dBname)) 245 newDbString[i] = this.databases[j]; 246 else 247 i--; 248 j++; 249 } 250 251 if(this.currentDatabase!=null && this.currentDatabase.equalsIgnoreCase(dBname)) { 252 this.currentDatabase = null; 253 } 254 255 String testDefaultDb = null; 257 if(configTemp.containsKey("DatabaseManager.DefaultDatabase")) 258 testDefaultDb = (String )configTemp.get("DatabaseManager.DefaultDatabase"); 259 if( (testDefaultDb!=null && !testDefaultDb.trim().equals("")) && 260 testDefaultDb.equalsIgnoreCase(dBname)) { 261 this.configTemp.remove("DatabaseManager.DefaultDatabase"); 262 this.defaultDatabase = "N/A"; 263 } 264 265 this.configTemp.remove("DatabaseManager.DB." + dBname + ".ClassType"); 266 this.configTemp.remove("DatabaseManager.DB." + dBname + ".JdbcDriver"); 267 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.AllocationTimeout"); 268 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.Logging"); 269 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.MaxPoolSize"); 270 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.Url"); 271 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.Password"); 272 this.configTemp.remove("DatabaseManager.DB." + dBname + ".Connection.User"); 273 this.configTemp.remove("DatabaseManager.DB." + dBname + ".ObjectId.CacheSize"); 274 this.configTemp.remove("DatabaseManager.DB." + dBname + ".ObjectId.MinValue"); 275 276 if(newDbString.length == 0) { this.configTemp.remove("DatabaseManager.Databases"); 278 this.databases = null; 279 this.currentDatabase = null; 280 } 281 else { 282 this.configTemp.set("DatabaseManager.Databases", newDbString); 283 this.databases = newDbString; 284 } 285 return true; 286 } 287 288 295 public void setDatabases(String [] databases) throws KeywordValueException { 296 String newDbParam = ""; 297 for (int i = 0; i < databases.length; i++) { 298 if(i==0) 299 newDbParam = "\"" + this.databases[i] + "\""; 300 else 301 newDbParam = newDbParam + ", \"" + this.databases[i] + "\""; 302 } 303 for (int i = 0; i < this.databases.length; i++) { 305 this.removeDatabase(databases[i]); 306 } 307 308 this.configTemp.set("DatabaseManager.Databases", newDbParam); 309 this.databases = databases; 310 311 for (int i = 0; i < this.databases.length; i++) { 313 this.addDatabase(databases[i]); 314 } 315 316 } 317 318 323 public String [] getDatabases() { 324 return this.databases; 325 } 326 327 334 public void setDBnameClassType(String value, String dBname) throws KeywordValueException{ 335 if(this.existDbName(dBname)) { 336 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 337 this.refreshAllDbParameters(dBname); 338 if(value==null || value.trim().equals("")) { 339 this.dBnameClassType = "N/A"; 340 configTemp.set("DatabaseManager.DB." + dBname + ".ClassType", ""); 341 } 342 else { 343 this.dBnameClassType = value; 344 configTemp.set("DatabaseManager.DB." + dBname + ".ClassType", value); 345 } 346 } 347 } 348 349 356 public String getDBnameClassType( String dBname) throws KeywordValueException { 357 if(this.existDbName(dBname)) { 358 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 359 this.refreshAllDbParameters(dBname); 360 return this.dBnameClassType; 361 } 362 else 363 return "N/A"; 364 } 365 366 374 public void setDBnameConnectionAllocationTimeout(String value, String dBname) throws KeywordValueException { 375 if(this.existDbName(dBname)) { 376 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 377 this.refreshAllDbParameters(dBname); 378 if(value==null || value.trim().equals("")) { 379 this.dBnameConnectionAllocationTimeout = "N/A"; 380 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.AllocationTimeout", ""); 381 } 382 else { 383 this.dBnameConnectionAllocationTimeout = value; 384 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.AllocationTimeout", value); 385 } 386 } 387 } 388 389 396 public String getDBnameConnectionAllocationTimeout( String dBname) throws KeywordValueException{ 397 if(this.existDbName(dBname)) { 398 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 399 this.refreshAllDbParameters(dBname); 400 return this.dBnameConnectionAllocationTimeout; 401 } 402 else 403 return "N/A"; 404 } 405 406 415 public void setDBnameConnectionLogging(String value, String dBname) throws KeywordValueException{ 416 if(this.existDbName(dBname)) { 417 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 418 this.refreshAllDbParameters(dBname); 419 if(value==null || value.trim().equals("") || 420 !(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) ) { 421 this.dBnameConnectionLogging = "N/A"; 422 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Logging", ""); 423 } 424 else { 425 this.dBnameConnectionLogging = value; 426 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Logging", value); 427 } 428 } 429 } 430 431 439 public String getDBnameConnectionLogging( String dBname) throws KeywordValueException{ 440 if(this.existDbName(dBname)) { 441 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 442 this.refreshAllDbParameters(dBname); 443 return this.dBnameConnectionLogging; 444 } 445 else 446 return "N/A"; 447 } 448 449 456 public void setDBnameConnectionMaxPoolSize(String value, String dBname) throws KeywordValueException{ 457 if(this.existDbName(dBname)) { 458 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 459 this.refreshAllDbParameters(dBname); 460 if(value==null || value.trim().equals("")) { 461 this.dBnameConnectionMaxPoolSize = "N/A"; 462 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.MaxPoolSize", ""); 463 } 464 else { 465 this.dBnameConnectionMaxPoolSize = value; 466 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.MaxPoolSize", value); 467 } 468 } 469 } 470 471 478 public String getDBnameConnectionMaxPoolSize( String dBname) throws KeywordValueException{ 479 if(this.existDbName(dBname)) { 480 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 481 this.refreshAllDbParameters(dBname); 482 return this.dBnameConnectionMaxPoolSize; 483 } 484 else 485 return "N/A"; 486 } 487 488 495 public void setDBnameConnectionPassword(String value, String dBname) throws KeywordValueException{ 496 if(this.existDbName(dBname)) { 497 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 498 this.refreshAllDbParameters(dBname); 499 if(value==null || value.trim().equals("")) { 500 this.dBnameConnectionPassword = "N/A"; 501 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Password", ""); 502 } 503 else { 504 this.dBnameConnectionPassword = value; 505 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Password", value); 506 } 507 } 508 } 509 510 517 public String getDBnameConnectionPassword( String dBname) throws KeywordValueException{ 518 if(this.existDbName(dBname)) { 519 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 520 this.refreshAllDbParameters(dBname); 521 return this.dBnameConnectionPassword; 522 } 523 else 524 return "N/A"; 525 } 526 527 534 public void setDBnameConnectionUrl(String value, String dBname) throws KeywordValueException{ 535 if(this.existDbName(dBname)) { 536 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 537 this.refreshAllDbParameters(dBname); 538 if(value==null || value.trim().equals("")) { 539 this.dBnameConnectionUrl = "N/A"; 540 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Url", ""); 541 } 542 else { 543 this.dBnameConnectionUrl = value; 544 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.Url", value); 545 } 546 } 547 } 548 549 556 public String getDBnameConnectionUrl( String dBname) throws KeywordValueException{ 557 if(this.existDbName(dBname)) { 558 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 559 this.refreshAllDbParameters(dBname); 560 return this.dBnameConnectionUrl; 561 } 562 else 563 return "N/A"; 564 } 565 566 573 public void setDBnameConnectionUser(String value, String dBname) throws KeywordValueException{ 574 if(this.existDbName(dBname)) { 575 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 576 this.refreshAllDbParameters(dBname); 577 if(value==null || value.trim().equals("")) { 578 this.dBnameConnectionUser = "N/A"; 579 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.User", ""); 580 } 581 else { 582 this.dBnameConnectionUser = value; 583 configTemp.set("DatabaseManager.DB." + dBname + ".Connection.User", value); 584 } 585 } 586 } 587 588 595 public String getDBnameConnectionUser( String dBname) throws KeywordValueException{ 596 if(this.existDbName(dBname)) { 597 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 598 this.refreshAllDbParameters(dBname); 599 return this.dBnameConnectionUser; 600 } 601 else 602 return "N/A"; 603 } 604 605 612 public void setDBnameJdbcDriver(String value, String dBname) throws KeywordValueException{ 613 if(this.existDbName(dBname)) { 614 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 615 this.refreshAllDbParameters(dBname); 616 if(value==null || value.trim().equals("")) { 617 this.dBnameJdbcDriver = "N/A"; 618 configTemp.set("DatabaseManager.DB." + dBname + ".JdbcDriver", ""); 619 } 620 else { 621 this.dBnameJdbcDriver = value; 622 configTemp.set("DatabaseManager.DB." + dBname + ".JdbcDriver", value); 623 } 624 } 625 } 626 627 634 public String getDBnameJdbcDriver( String dBname) throws KeywordValueException{ 635 if(this.existDbName(dBname)) { 636 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 637 this.refreshAllDbParameters(dBname); 638 return this.dBnameJdbcDriver; 639 } 640 else 641 return "N/A"; 642 } 643 644 651 public void setDBnameObjectIdCacheSize(String value, String dBname) throws KeywordValueException{ 652 if(this.existDbName(dBname)) { 653 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 654 this.refreshAllDbParameters(dBname); 655 if(value==null || value.trim().equals("")) { 656 this.dBnameObjectIdCacheSize = "N/A"; 657 configTemp.set("DatabaseManager.DB." + dBname + ".ObjectId.CacheSize", ""); 658 } 659 else { 660 this.dBnameObjectIdCacheSize = value; 661 configTemp.set("DatabaseManager.DB." + dBname + ".ObjectId.CacheSize", value); 662 } 663 } 664 } 665 666 673 public String getDBnameObjectIdCacheSize( String dBname) throws KeywordValueException{ 674 if(this.existDbName(dBname)) { 675 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 676 this.refreshAllDbParameters(dBname); 677 return this.dBnameObjectIdCacheSize; 678 } 679 else 680 return "N/A"; 681 } 682 683 690 public void setDBnameObjectIdMinValue(String value, String dBname) throws KeywordValueException{ 691 if(this.existDbName(dBname)) { 692 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 693 this.refreshAllDbParameters(dBname); 694 if(value==null || value.trim().equals("")) { 695 this.dBnameObjectIdMinValue = "N/A"; 696 configTemp.set("DatabaseManager.DB." + dBname + ".ObjectId.MinValue", ""); 697 } 698 else { 699 this.dBnameObjectIdMinValue = value; 700 configTemp.set("DatabaseManager.DB." + dBname + ".ObjectId.MinValue", value); 701 } 702 } 703 } 704 705 712 public String getDBnameObjectIdMinValue( String dBname) throws KeywordValueException{ 713 if(this.existDbName(dBname)) { 714 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 715 this.refreshAllDbParameters(dBname); 716 return this.dBnameObjectIdMinValue; 717 } 718 else 719 return "N/A"; 720 } 721 722 730 public void setDebug(String debug) throws KeywordValueException{ 731 if(debug==null || debug.trim().equals("") || debug.equalsIgnoreCase("N/A")) { 732 this.debug = "N/A"; 733 configTemp.remove("DatabaseManager.Debug"); 734 } 735 else if(debug.equalsIgnoreCase("true")) { 736 this.debug = debug; 737 configTemp.set("DatabaseManager.Debug", debug); 738 } 739 else { 740 this.debug = "false"; 741 configTemp.set("DatabaseManager.Debug", "false"); 742 } 743 } 744 745 751 public String getDebug() throws KeywordValueException { 752 this.refreshAllDbParameters(null); 753 return this.debug; 754 } 755 756 761 public void setDefaultDatabase(String dBname) throws KeywordValueException { 762 if(this.existDbName(dBname)) { 763 configTemp.set("DatabaseManager.DefaultDatabase",dBname); 764 this.defaultDatabase = dBname; 765 } 766 else if(dBname==null || dBname.trim().equals("")) 767 configTemp.remove("DatabaseManager.DefaultDatabase"); 768 this.defaultDatabase = "N/A"; 769 } 770 771 776 public String getDefaultDatabase() throws KeywordValueException { 777 this.refreshAllDbParameters(null); 778 return this.defaultDatabase; 779 } 780 781 786 public boolean saveState(){ 787 try{ 788 this.configOriginal.importConfig(this.configTemp); 789 ConfigFileInterface confFile = configOriginal.getConfigFile(); 790 confFile.write(); 791 } 792 catch(Exception e) { 793 return false; 794 } 795 return true; 796 } 797 798 799 } | Popular Tags |