1 19 20 27 package org.enhydra.dods; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.InputStream ; 32 import java.lang.reflect.Constructor ; 33 import java.net.URL ; 34 import java.sql.SQLException ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Set ; 38 39 import javax.naming.Context ; 40 import javax.naming.InitialContext ; 41 42 import com.lutris.appserver.server.sql.DatabaseManager; 43 import com.lutris.appserver.server.sql.DatabaseManagerException; 44 import com.lutris.appserver.server.sql.StandardDatabaseManager; 45 import com.lutris.logging.LogChannel; 46 import com.lutris.logging.Logger; 47 import com.lutris.logging.StandardLogger; 48 import com.lutris.util.Config; 49 import com.lutris.util.ConfigException; 50 import com.lutris.util.ConfigFile; 51 52 77 public class DODS { 78 79 80 88 private static HashMap databaseManagers; 89 90 95 private static DatabaseManager defaultDatabaseManager; 96 97 104 private static HashMap logChannels; 105 106 112 private static LogChannel defaultLogChannel; 113 114 122 private static boolean threading; 123 124 private static boolean dodsConfigured; 125 126 127 static { 128 init(); 129 } 130 131 136 public static void registerDefault(DatabaseManager databaseManager) { 137 defaultDatabaseManager = databaseManager; 138 setDodsConfigured(true); 139 } 140 141 146 public static void registerDefault(String fileName) 147 throws ConfigException, DatabaseManagerException, SQLException { 148 defaultDatabaseManager = StandardDatabaseManager.newInstance(fileName); 149 setDodsConfigured(true); 150 } 151 152 161 public static void register(DatabaseManager databaseManager) { 162 if (threading) { 163 databaseManagers.put(Thread.currentThread(), databaseManager); 164 if (defaultDatabaseManager == null) { 165 defaultDatabaseManager = databaseManager; 166 } 167 setDodsConfigured(true); 168 return; 169 } 170 defaultDatabaseManager = databaseManager; 171 setDodsConfigured(true); 172 } 173 174 183 public static void startup(String fileName) 184 throws ConfigException, DatabaseManagerException, SQLException { 185 186 LogChannel channel = null; 187 try { 188 channel = getLogChannel(fileName); 189 } catch (Exception ex) { 190 throw new ConfigException("Unable to invoke Logging Class defined in : " 191 + fileName); 192 } 193 defaultLogChannel = channel; 194 195 DatabaseManager databaseManager = StandardDatabaseManager.newInstance(fileName); 196 197 if (threading) { 198 databaseManagers.put(Thread.currentThread(), databaseManager); 199 logChannels.put(Thread.currentThread(), channel); 200 if (defaultDatabaseManager == null) { 201 defaultDatabaseManager = databaseManager; 202 } 203 defaultLogChannel = channel; 204 setDodsConfigured(true); 205 return; 206 } 207 defaultDatabaseManager = databaseManager; 208 ((StandardDatabaseManager)databaseManager).initChaches(databaseManager.getClass().getClassLoader()); 209 setDodsConfigured(true); 210 } 211 212 213 222 public static void startup(URL confURL, String confFile) 223 throws ConfigException, DatabaseManagerException, SQLException { 224 225 LogChannel channel = null; 226 try { 227 channel = getLogChannel(confURL, confFile); 228 } catch (Exception ex) { 229 throw new ConfigException("Unable to invoke Logging Class defined in : " 230 + confFile); 231 } 232 defaultLogChannel = channel; 233 234 DatabaseManager databaseManager = StandardDatabaseManager.newInstance(confURL, confFile); 235 if (threading) { 236 databaseManagers.put(Thread.currentThread(), databaseManager); 237 logChannels.put(Thread.currentThread(), channel); 238 if (defaultDatabaseManager == null) { 239 defaultDatabaseManager = databaseManager; 240 } 241 defaultLogChannel = channel; 242 setDodsConfigured(true); 243 return; 244 } 245 defaultDatabaseManager = databaseManager; 246 ((StandardDatabaseManager)databaseManager).initChaches(databaseManager.getClass().getClassLoader()); 247 setDodsConfigured(true); 248 } 249 250 251 252 261 public static void startup(Thread thread, String fileName) 262 throws ConfigException, DatabaseManagerException, SQLException { 263 LogChannel channel=null; 264 try { 265 channel = getLogChannel(fileName); 266 } catch (Exception ex) { 267 throw new ConfigException("Unable to invoke Logging Class defined in : " 268 + fileName); 269 } 270 logChannels.put(Thread.currentThread(), channel); 271 272 DatabaseManager databaseManager = StandardDatabaseManager.newInstance(fileName); 273 databaseManagers.put(thread, databaseManager); 274 if (defaultDatabaseManager == null) { 275 defaultLogChannel = channel; 276 defaultDatabaseManager = databaseManager; 277 } 278 ((StandardDatabaseManager)databaseManager).initChaches(databaseManager.getClass().getClassLoader()); 279 setDodsConfigured(true); 280 } 281 282 283 private static LogChannel getLogChannel(String cf) throws Exception { 284 try { 285 FileInputStream configFIS = new FileInputStream (cf); 286 ConfigFile cFile = new ConfigFile(configFIS); 287 Config config = cFile.getConfig(); 288 configFIS.close(); 289 Config logSection = (Config) config.getSection("DatabaseManager"); 290 String logClassName = logSection.getString("LogClassName","com.lutris.logging.StandardLogger"); 291 292 Class loggerClass; 293 Class [] argTypeArr = {Boolean.TYPE}; 294 Object [] argArr = {new Boolean (false)}; 295 296 loggerClass = Class.forName(logClassName); 297 Constructor logConstructor = loggerClass.getConstructor(argTypeArr); 298 Logger logger = (Logger) (logConstructor.newInstance(argArr)); 299 300 logger.configure(logSection); 301 return logger.getChannel("DatabaseManager"); 302 } catch (Throwable t) {} 303 return getLogChannel(null ,cf); 304 } 305 306 307 private static LogChannel getLogChannel(URL confURL, String confFile) throws ConfigException { 308 try { 309 InputStream configIS = Common.getConfFileFromURL(confURL, confFile); 310 ConfigFile configFile = new ConfigFile(configIS); 311 Config config = configFile.getConfig(); 312 configIS.close(); 313 Config logSection = (Config) config.getSection("DatabaseManager"); 314 315 String logClassName = logSection.getString("LogClassName","com.lutris.logging.StandardLogger"); 316 317 Class loggerClass; 318 Class [] argTypeArr = {Boolean.TYPE}; 319 Object [] argArr = {new Boolean (false)}; 320 321 loggerClass = Class.forName(logClassName); 322 Constructor logConstructor = loggerClass.getConstructor(argTypeArr); 323 Logger logger = (Logger) (logConstructor.newInstance(argArr)); 324 325 logger.configure(logSection); 326 return logger.getChannel("DatabaseManager"); 327 } catch (Throwable t) {} 328 return configureStandardLogerChannel(); 329 } 330 331 332 public static LogChannel configureStandardLogerChannel() 333 throws ConfigException { 334 LogChannel channel = null; 335 336 try { 337 File logFile = new File ("DatabaseManager.log"); 338 StandardLogger logger = new StandardLogger(false); 339 String [] fileLogLevels = {"EMERGENCY", "ALERT", "CRITICAL", "ERROR", 340 "WARNING", "NOTICE", "INFO" 341 }; 342 String [] stdErrLogLevels = {"EMERGENCY", "ALERT", "CRITICAL", 343 "ERROR", "WARNING", "NOTICE", "INFO" 344 }; 345 346 logger.configure(logFile, fileLogLevels, stdErrLogLevels); 347 return logger.getChannel("databaseManager"); 348 } catch (Exception ex) { 349 throw new ConfigException("Unable to invoke standard logger."); 350 } 351 } 352 353 361 public static void register(Thread thread, DatabaseManager databaseManager) { 362 databaseManagers.put(thread, databaseManager); 363 if (defaultDatabaseManager == null) { 364 defaultDatabaseManager = databaseManager; 365 } 366 setDodsConfigured(true); 367 } 368 369 374 public static void registerDefaultLogChannel(LogChannel channel) { 375 defaultLogChannel = channel; 376 } 377 378 385 public static void registerLogChannel(LogChannel channel) { 386 if (threading) { 387 logChannels.put(Thread.currentThread(), channel); 388 } 389 if (defaultLogChannel==null){ 390 defaultLogChannel = channel; 391 } 392 } 393 394 400 public static void registerLogChannel(Thread thread, LogChannel channel) { 401 logChannels.put(thread, channel); 402 if (defaultLogChannel==null){ 403 defaultLogChannel = channel; 404 } 405 } 406 407 416 public static DatabaseManager unregisterDefault() 417 throws DODSException { 418 try { 419 DatabaseManager dbManager = defaultDatabaseManager; 420 421 defaultDatabaseManager = null; 422 return dbManager; 423 } catch (Exception e) { 424 throw new DODSException(e); 425 } 426 } 427 428 440 public static DatabaseManager unregister() 441 throws DODSException { 442 try { 443 DatabaseManager dbManager; 444 445 if (threading) { 446 return (DatabaseManager) databaseManagers.remove(Thread.currentThread()); 447 } 448 dbManager = defaultDatabaseManager; 449 defaultDatabaseManager = null; 450 return dbManager; 451 } catch (Exception e) { 452 throw new DODSException(e); 453 } 454 } 455 456 465 public static DatabaseManager unregister(Thread thread) 466 throws DODSException { 467 try { 468 return (DatabaseManager) databaseManagers.remove(thread); 469 } catch (Exception e) { 470 throw new DODSException(e); 471 } 472 } 473 474 483 public static LogChannel unregisterDefaultLogChannel() 484 throws DODSException { 485 try { 486 LogChannel channel = defaultLogChannel; 487 defaultLogChannel = null; 488 return channel; 489 } catch (Exception e) { 490 throw new DODSException(e); 491 } 492 } 493 494 506 public static LogChannel unregisterLogChannel() 507 throws DODSException { 508 try { 509 if (threading) { 510 return (LogChannel) logChannels.remove(Thread.currentThread()); 511 }else{ 512 return unregisterDefaultLogChannel(); 513 } 514 } catch (Exception e) { 515 throw new DODSException(e); 516 } 517 } 518 519 528 public static LogChannel unregisterLogChannel(Thread thread) 529 throws DODSException { 530 try { 531 return (LogChannel) logChannels.remove(thread); 532 } catch (Exception e) { 533 throw new DODSException(e); 534 } 535 } 536 537 542 public static DatabaseManager getDefaultDatabaseManager() { 543 checkDodsConfiguration(); 544 return defaultDatabaseManager; 545 } 546 547 557 public static DatabaseManager getDatabaseManager() { 558 checkDodsConfiguration(); 559 if (threading) { 560 DatabaseManager dbManager = (DatabaseManager) databaseManagers.get(Thread.currentThread()); 561 562 if (dbManager != null) { 563 return dbManager; 564 } 565 } 566 return defaultDatabaseManager; 567 } 568 569 579 public static DatabaseManager getDatabaseManager(Thread thread) { 580 checkDodsConfiguration(); 581 DatabaseManager dbManager = (DatabaseManager) databaseManagers.get(thread); 582 583 if (dbManager != null) { 584 return dbManager; 585 } 586 return defaultDatabaseManager; 587 } 588 589 594 public static LogChannel getDefaultLogChannel() { 595 try{ 596 if (defaultLogChannel==null){ 597 defaultLogChannel=configureStandardLogerChannel(); 598 } 599 } catch (ConfigException ex) {} 600 return defaultLogChannel; 601 } 602 603 611 public static LogChannel getLogChannel() { 612 if (threading) { 613 LogChannel channel = (LogChannel) logChannels.get(Thread.currentThread()); 614 615 if (channel != null) { 616 return channel; 617 } 618 } 619 return getDefaultLogChannel(); 620 } 621 622 631 public static LogChannel getLogChannel(Thread thread) { 632 LogChannel channel = (LogChannel) logChannels.get(thread); 633 634 if (channel != null) { 635 return channel; 636 } 637 return defaultLogChannel; 638 } 639 640 648 public static void shutdown() 649 throws DODSException { 650 try { 651 if (threading) { 652 Set keys = databaseManagers.keySet(); 653 654 for (Iterator iter = keys.iterator(); iter.hasNext();) { 655 Thread key = (Thread ) iter.next(); 656 DatabaseManager dbManager = (DatabaseManager) databaseManagers.remove(key); 657 658 if (dbManager != null) { 659 dbManager.shutdown(); 660 dbManager = null; 661 } 662 } 663 keys = logChannels.keySet(); 664 for (Iterator iter = keys.iterator(); iter.hasNext();) { 665 Thread key = (Thread ) iter.next(); 666 667 logChannels.remove(key); 668 } 669 } 670 if (defaultDatabaseManager != null) { 671 defaultDatabaseManager.shutdown(); 672 defaultDatabaseManager = null; 673 } 674 dodsConfigured = false; 675 } catch (Exception e) { 676 throw new DODSException(e); 677 } 678 } 679 680 691 public static boolean isThreading() { 692 return threading; 693 } 694 695 706 public static void setThreading(boolean mode) { 707 threading = mode; 708 } 709 710 715 protected static void init() { 716 dodsConfigured = false; 717 threading = false; 718 databaseManagers = new HashMap (); 719 logChannels = new HashMap (); 720 } 721 722 723 724 private static void checkDodsConfiguration() { 725 String confFilePath = null; 726 try { 727 if (!dodsConfigured){ 728 confFilePath = System.getProperty(CommonConstants.DODS_CONFIG_FILE_PROPERTY_NAME); 729 if (null==confFilePath) { 730 try { 731 Context initContext = new InitialContext (); 732 Context envContext = (Context )initContext.lookup(CommonConstants.JNDI_ENV); 733 confFilePath = (String )envContext.lookup(CommonConstants.DODS_CONFIG_FILE_LOOKUP); 734 }catch (Exception e) {} 735 } 736 if (confFilePath!=null) { 737 DODS.startup(confFilePath); 738 }else { 739 DODS.startup((URL )null,CommonConstants.DEFAULT_CONFIG_FILE_NAME); 740 } 741 } 742 }catch (Exception e) { 743 dodsConfigured = false; 744 } 745 } 746 747 748 751 protected static boolean isDodsConfigured() { 752 return dodsConfigured; 753 } 754 755 756 759 protected static void setDodsConfigured(boolean configured) { 760 DODS.dodsConfigured = configured; 761 } 762 763 } 764 | Popular Tags |