1 64 65 package com.jcorporate.expresso.services.dbobj; 66 67 import com.jcorporate.expresso.core.ExpressoSchema; 68 import com.jcorporate.expresso.core.cache.CacheException; 69 import com.jcorporate.expresso.core.cache.CacheManager; 70 import com.jcorporate.expresso.core.cache.CacheSystem; 71 import com.jcorporate.expresso.core.controller.ControllerRequest; 72 import com.jcorporate.expresso.core.db.DBConnection; 73 import com.jcorporate.expresso.core.db.DBConnectionPool; 74 import com.jcorporate.expresso.core.db.DBException; 75 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 76 import com.jcorporate.expresso.core.dbobj.ValidValue; 77 import com.jcorporate.expresso.core.misc.ConfigManager; 78 import com.jcorporate.expresso.core.misc.ConfigurationException; 79 import com.jcorporate.expresso.core.misc.StringUtil; 80 import org.apache.log4j.Logger; 81 82 import java.util.Enumeration ; 83 import java.util.HashSet ; 84 import java.util.Iterator ; 85 86 87 95 public class Setup 96 extends SecuredDBObject { 97 98 101 private static boolean cacheInitialized = false; 102 103 106 public static final String CACHE_NAME = Setup.class.getName() + ".setupCache"; 107 108 111 private static Logger log = Logger.getLogger(Setup.class); 112 113 114 117 private static HashSet tableNotExists = new HashSet (); 118 public static final String SETUP_CODE = "SetupCode"; 119 public static final String SETUP_DESCRIP = "Descrip"; 120 public static final String SETUP_VALUE = "SetupValue"; 121 public static final String SCHEMA_CLASS = "SchemaClass"; 122 123 128 public Setup() 129 throws DBException { 130 super(); 131 } 132 133 134 140 public Setup(int uid) 141 throws DBException { 142 super(uid); 143 } 144 145 152 public Setup(ControllerRequest request) 153 throws DBException { 154 super(request); 155 } 156 157 162 public synchronized void add() 163 throws DBException { 164 super.add(); 165 updateCache(); 166 } 167 168 169 174 public synchronized void delete() 175 throws DBException { 176 super.delete(); 177 updateCache(); 178 } 179 180 181 185 public boolean find() 186 throws DBException { 187 if (getField(SCHEMA_CLASS).equals("")) { 188 setField(SCHEMA_CLASS, ExpressoSchema.class.getName()); 189 } 190 191 return super.find(); 192 } 193 194 195 208 public static String getValue(String dbName, String setupCode) 209 throws DBException { 210 211 if (tableNotExists.contains(dbName)) { 212 if (log.isDebugEnabled()) { 213 log.debug("No setup info yet for db " + dbName); 214 } 215 216 return ""; 217 } 218 219 return getValue(dbName, "com.jcorporate.expresso.core.ExpressoSchema", 220 setupCode); 221 } 222 223 224 236 public static String getValue(String dbName, String schemaName, 237 String setupCode) 238 throws DBException { 239 if (StringUtil.notNull(dbName).equals("")) { 240 dbName = DBConnection.DEFAULT_DB_CONTEXT_NAME; 241 } 242 243 244 boolean isCaching = false; 246 try { 247 isCaching = ConfigManager.getJdbc(dbName).cache(); 248 } catch (ConfigurationException e) { 249 throw new DBException(e); 250 } 251 252 if (!isCaching) { 253 Setup sl = new Setup(SecuredDBObject.SYSTEM_ACCOUNT); 254 sl.setDataContext(dbName); 255 sl.setField(SETUP_CODE, setupCode); 256 sl.setField(SCHEMA_CLASS, schemaName); 257 sl.retrieve(); 258 259 return ConfigManager.expandValue(StringUtil.notNull(sl.getField(SETUP_VALUE))); 260 } 261 262 263 CacheManager.getInstance(); 266 if (tableNotExists.contains(dbName)) { 267 if (log.isDebugEnabled()) { 268 log.debug("No setup info yet for db " + dbName); 269 } 270 271 return (""); 272 } 273 if (StringUtil.notNull(schemaName).equals("")) { 274 throw new DBException("Schema name may not be null or blank"); 275 } 276 if (StringUtil.notNull(setupCode).equals("")) { 277 throw new DBException("Setup code may not be null or blank"); 278 } 279 280 if (!CacheManager.existsCache(dbName, CACHE_NAME) && isCaching) { 281 log.info("Setup cache did not exist - reading setup values"); 282 readSetups(); 283 } 284 285 289 int itemCount = CacheManager.getItemCount(dbName, CACHE_NAME); 290 291 if (itemCount == 0) { 292 293 if (isCaching) { 294 log.info("Setup cache did not exist - reading setup values"); 295 readSetups(); 296 } 297 298 itemCount = CacheManager.getItemCount(dbName, CACHE_NAME); 299 300 if (itemCount == 0) { 301 log.error("For context: " + dbName + ", Setup cache still has zero items after re-reading"); 302 } 303 } 304 305 ValidValue oneItem = (ValidValue) CacheManager.getItem(dbName, 306 CACHE_NAME, 307 schemaName + "|" + setupCode); 308 309 if (oneItem == null) { 310 return null; 311 } 312 313 return ConfigManager.expandValue(StringUtil.notNull(oneItem.getDescription())); 314 } 315 316 317 326 public static String getValueRequired(String dbName, String setupCode) 327 throws DBException { 328 String setupValue = null; 329 330 if (tableNotExists.contains(dbName)) { 331 throw new DBException("No Setup Value '" + setupCode + 332 "' for default schema, db '" + dbName + "'" + 333 " Table does not yet exist"); 334 } 335 336 setupValue = getValue(dbName, setupCode); 337 338 if (setupValue == null) { 339 throw new DBException("No value supplied in setup for '" + 340 setupCode + "' for default schema, db '" + 341 dbName + "'"); 342 } else { 343 return setupValue; 344 } 345 } 346 347 348 355 public static String getValueUnrequired(String dbName, String setupCode) { 356 String setupValue = null; 357 358 try { 359 setupValue = getValue(dbName, setupCode); 360 if ("".equals(setupValue)) { 362 setupValue = null; 363 } 364 } catch (DBException e) { 365 } 367 368 return setupValue; 369 } 370 371 379 public static String getValueUnrequired(String dbName, String schema, String setupCode) { 380 String setupValue = null; 381 382 try { 383 setupValue = getValue(dbName, schema, setupCode); 384 if ("".equals(setupValue)) { 386 setupValue = null; 387 } 388 } catch (DBException e) { 389 } 391 392 return setupValue; 393 } 394 395 396 406 public static String getValueRequired(String dbName, String schema, 407 String setupCode) 408 throws DBException { 409 410 if (tableNotExists.contains(dbName)) { 411 throw new DBException("No Setup Value '" + setupCode + 412 "' for default schema, db '" + dbName + "'" + 413 " Table does not yet exist"); 414 } 415 if (schema.equals("")) { 416 return getValueRequired(dbName, setupCode); 417 } 418 if (StringUtil.notNull(dbName).equals("")) { 419 dbName = "default"; 420 } 421 422 String setupValue = getValue(dbName, schema, setupCode); 423 424 if (setupValue == null) { 425 throw new DBException("No value supplied in setup for '" + 426 setupCode + "' for schema '" + schema + 427 "', db '" + dbName + "'"); 428 } else { 429 return setupValue; 430 } 431 } 432 433 434 439 private void isDirectory() 440 throws DBException { 441 442 443 if (ConfigManager.expandValue(getField(SETUP_VALUE)).equals("")) { 444 return; 445 } 446 447 if (!ConfigManager.expandValue(getField(SETUP_VALUE)).endsWith("/")) { 448 throw new DBException("Setup value '" + getField(SETUP_VALUE) + 449 "' for code '" + getField(SETUP_CODE) + 450 "' (" + getField(SETUP_DESCRIP) + 451 ") is not a valid directory name. Must use '/' as " + 452 "path seperator and end with '/'"); 453 } 454 } 455 456 457 462 public static synchronized void readSetups(boolean forceRead) 463 throws DBException { 464 465 if (!ConfigManager.isInitialized()) { 466 throw new DBException("ConfigManager was not initialized", 467 ConfigManager.getConfigurationFailureException()); 468 } 469 470 log.info("Reading setup values for all databases/contexts"); 471 472 CacheManager.getInstance(); int keyCount = 0; 474 String oneConfigKey = null; 475 for (Enumeration e = ConfigManager.getAllConfigKeys(); 476 e.hasMoreElements();) { 477 keyCount++; 478 479 481 482 483 boolean skipThisContext = false; 484 485 try { 486 oneConfigKey = (String ) e.nextElement(); 487 488 490 skipThisContext = false; 491 492 if (tableNotExists.contains(oneConfigKey)) { 493 skipThisContext = true; 494 } 495 if (forceRead) { 496 skipThisContext = false; 497 } 498 503 try { 504 if (!ConfigManager.getContext(oneConfigKey) 505 .hasSetupTables()) { 506 skipThisContext = true; 507 } 508 } catch (ConfigurationException ce) { 509 throw new DBException(ce); 510 } 511 if (!skipThisContext) { 512 CacheSystem cs = CacheManager.getCacheSystem(oneConfigKey); 513 if (log.isInfoEnabled()) { 514 log.info("Reading setup values for '" + oneConfigKey + 515 "'"); 516 } 517 518 if (!cs.existsCache(CACHE_NAME)) { 519 cs.createCache(CACHE_NAME, 520 false); 521 } 522 523 Setup sl = new Setup(SecuredDBObject.SYSTEM_ACCOUNT); 524 sl.setDataContext(oneConfigKey); 525 526 Setup oneSetup = null; 527 int setupCount = 0; 528 529 for (Iterator ek = sl.searchAndRetrieveList().iterator(); 530 ek.hasNext();) { 531 setupCount++; 532 oneSetup = (Setup) ek.next(); 533 CacheManager.addItem(oneConfigKey, CACHE_NAME, 534 new ValidValue(oneSetup.getField(SCHEMA_CLASS) + "|" + 535 oneSetup.getField(SETUP_CODE), 536 oneSetup.getField(SETUP_VALUE))); 537 } 538 if (setupCount == 0) { 539 throw new DBException("No setup values were read" + 540 "in db '" + oneConfigKey + 541 "'. Run DBCreate to setup default " + 542 "setup values."); 543 } else { 544 log.info(setupCount + " setup entries read in db '" + 545 oneConfigKey + "'"); 546 } 547 548 DBConnectionPool onePool = DBConnectionPool.getInstance(oneConfigKey); 549 550 String maxConnect = StringUtil.notNull(getValue(oneConfigKey, "MaxConnections")); 551 552 if (!maxConnect.equals("")) { 553 try { 554 int newMax = new Integer (maxConnect).intValue(); 555 onePool.setMaxConnections(newMax); 556 } catch (NumberFormatException ne) { 557 throw new DBException("Value for MaxConnections" + 558 "must be a number"); 559 } 560 } 561 try { 562 String timeOut = StringUtil.notNull(getValue(oneConfigKey, 563 "ConnTimeOut")); 564 int interval = 30; 565 566 if (!timeOut.equals("")) { 567 interval = new Integer (getValue(oneConfigKey, 568 "ConnTimeOut")).intValue(); 569 } 570 571 onePool.setTimeOutInterval(interval); 572 } catch (NumberFormatException ne) { 573 throw new DBException("Value for ConnTimeOut is " + 574 "not a number. Check setup values"); 575 } 576 } 577 578 } catch (DBException de) { 579 log.error("WARNING: Unable to read setup values for db '" 580 + oneConfigKey + "'", de); 581 582 584 tableNotExists.add(oneConfigKey); 585 } catch (CacheException ce) { 586 log.error("WARNING: Cache error reading setup values for db '" 587 + oneConfigKey + "'", ce); 588 } 589 } 590 591 if (keyCount == 0) { 592 throw new DBException("No config keys to read setup values" 593 + "for - there must be at least the " + 594 "default config key/db"); 595 } 596 597 cacheInitialized = true; 598 } 599 600 605 public static synchronized void readSetups() 606 throws DBException { 607 readSetups(false); 608 } 609 610 611 616 public synchronized void retrieve() 617 throws DBException { 618 if (getField(SCHEMA_CLASS).equals("")) { 619 setField(SCHEMA_CLASS, ExpressoSchema.class.getName()); 620 } 621 622 super.retrieve(); 623 } 624 625 626 632 public static synchronized void setTableExists(String dbName) { 633 if (tableNotExists.contains(dbName)) { 634 tableNotExists.remove(dbName); 635 } 636 } 637 638 641 protected synchronized void setupFields() 642 throws DBException { 643 setTargetTable("SETUP"); 644 setDescription("DBsetup"); 645 setCharset("ISO-8859-1"); 646 addField(SCHEMA_CLASS, "char", 128, false, "schema"); 647 addField(SETUP_CODE, "char", 30, false, SETUP_CODE); 648 addField(SETUP_DESCRIP, "char", 80, false, "description"); 649 addField(SETUP_VALUE, "varchar", 255, true, SETUP_VALUE); 650 setStringFilter(SCHEMA_CLASS, "stripFilter"); 651 setStringFilter(SETUP_CODE, "stripFilter"); 652 setStringFilter(SETUP_DESCRIP, "stripFilter"); 653 setStringFilter(SETUP_VALUE, "rawFilter"); 654 addKey(SCHEMA_CLASS); 655 addKey(SETUP_CODE); 656 setMultiValued(SCHEMA_CLASS); 657 setLookupObject(SCHEMA_CLASS, SchemaList.class.getName()); 658 } 659 660 661 666 public synchronized void update() 667 throws DBException { 668 669 670 if (getField(SETUP_CODE).equals("BaseDir")) { 671 isDirectory(); 672 } else if (getField(SETUP_CODE).equals("QueueDirectory")) { 673 isDirectory(); 674 } else if (getField(SETUP_CODE).equals("ReportRoot")) { 675 isDirectory(); 676 } else if (getField(SETUP_CODE).equals("TempDir")) { 677 isDirectory(); 678 } 679 680 super.update(); 681 updateCache(); 682 } 683 684 685 691 private void updateCache() 692 throws DBException { 693 694 695 if (cacheInitialized) { 696 readSetups(); 697 } 698 } 699 700 705 public void setCode(String code) throws DBException { 706 setField(SETUP_CODE, code); 707 } 708 709 714 public void setValue(String value) throws DBException { 715 setField(SETUP_VALUE, value); 716 } 717 718 723 public void setDescrip(String descrip) throws DBException { 724 setField(SETUP_DESCRIP, descrip); 725 } 726 727 732 public void setSchemaClass(String schemaclassname) throws DBException { 733 setField(SCHEMA_CLASS, schemaclassname); 734 } 735 736 } 737 | Popular Tags |