1 2 3 25 package com.lutris.appserver.server.sql; 26 27 import java.io.FileInputStream ; 28 import java.io.InputStream ; 29 import java.sql.SQLException ; 30 import java.lang.reflect.Method ; 31 import java.net.URL ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import org.enhydra.dods.Common; 36 import org.enhydra.dods.CommonConstants; 37 import org.enhydra.dods.DODS; 38 import org.enhydra.dods.cache.CacheConstants; 39 import org.enhydra.dods.cache.Wrapper; 40 import com.lutris.appserver.server.sql.standard.StandardLogicalDatabase; 41 import com.lutris.appserver.server.sql.standard.DriverSpecificConstants; 42 import com.lutris.classloader.MultiClassLoader; 43 import com.lutris.util.Config; 44 import com.lutris.util.ConfigException; 45 import com.lutris.util.ConfigFile; 46 import org.enhydra.util.ConfigFileInterface; 47 import com.lutris.util.KeywordValueException; 48 import com.lutris.dods.builder.generator.query.QueryBuilder; 49 import com.lutris.logging.Logger; 50 51 import java.io.File ; 52 53 import javax.xml.parsers.DocumentBuilder ; 54 import javax.xml.parsers.DocumentBuilderFactory ; 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Element ; 58 import org.w3c.dom.NodeList ; 59 60 61 119 public class StandardDatabaseManager implements DatabaseManager, CacheConstants, DriverSpecificConstants { 120 121 124 private Hashtable logicalDatabases = new Hashtable (); 125 126 129 private LogicalDatabase defaultLogicalDatabase = null; 130 131 134 protected boolean debug = false; 135 protected String defaultDB = null; 136 protected Config config = null; 137 138 protected String confDir = null; 139 140 private String appName = CacheConstants.DEFAULT_APP_NAME; 141 142 private DatabaseManagerConfiguration dbmConf = new DatabaseManagerConfiguration(); 143 144 private Config mainConfig = null; 145 146 147 148 149 162 public StandardDatabaseManager(Config config) 163 throws ConfigException, DatabaseManagerException, SQLException { 164 165 this.config = config; 166 String [] databases = config.getStrings("Databases"); 167 168 if (databases == null) { 169 return; 170 } 171 172 try { 173 confDir = config.getString("ConfigurationDir", null); 174 } catch (KeywordValueException except) { 175 confDir = null; 176 } 177 try { 178 appName = config.getString(PARAMNAME_APP_NAME, DEFAULT_APP_NAME); 179 } catch (KeywordValueException except) { 180 appName = DEFAULT_APP_NAME; 181 } 182 Common.setConfigDir(confDir); 183 184 try { 185 Config userConfig = (Config) config.getSection("DB.User"); 186 if (userConfig != null) { 187 String value; 188 if ((value = userConfig.getString("userWildcard")) != null) { 189 dbmConf.setUserConfigWildcard(value); 190 } 191 if ((value = userConfig.getString("userSingleWildcard")) != null) { 192 dbmConf.setUserConfigSingleWildcard(value); 193 } 194 if ((value = userConfig.getString("userSingleWildcardEscape")) 195 != null) { 196 dbmConf.setUserConfigSingleWildcardEscape(value); 197 } 198 if ((value = userConfig.getString("userWildcardEscape")) != null) { 199 dbmConf.setUserConfigWildcardEscape(value); 200 } 201 } 202 } catch (Exception except) {} 203 204 205 Config defaultsConfig; 206 207 try { 208 defaultsConfig = (Config) config.getSection("defaults"); 209 } catch (KeywordValueException except) { 210 throw new ConfigException("No DatabaseManager.defaults defined in config file."); 211 } 212 213 if (defaultsConfig != null) { 214 dbmConf.setAllReadOnly(defaultsConfig.getBoolean(PARAMNAME_ALL_READ_ONLY, 215 CacheConstants.DEFAULT_All_READONLY)); 216 dbmConf.setLazyLoading(defaultsConfig.getBoolean(PARAMNAME_LAZY_LOADING, 217 false)); 218 dbmConf.setCaseSensitive(defaultsConfig.getBoolean(PARAMNAME_CASE_SENSITIVE, 219 DEFAULT_CASE_SENSITIVE)); 220 dbmConf.setMaxExecuteTime(defaultsConfig.getInt(PARAMNAME_MAX_EXECUTE_TIME, 221 0)); 222 dbmConf.setTransactionCheck(defaultsConfig.getBoolean(PARAMNAME_TRANSACTION_CHECK, 223 false)); 224 dbmConf.setDeleteCheckVersion(defaultsConfig.getBoolean(PARAMNAME_DELETE_CHECK_VERSION, 225 false)); 226 dbmConf.setAutoSave(defaultsConfig.getBoolean(PARAMNAME_AUTO_SAVE, 227 false)); 228 dbmConf.setAutoSaveCreateVirgin(defaultsConfig.getBoolean(PARAMNAME_AUTO_SAVE_CREATE_VIRGIN, 229 false)); 230 dbmConf.setAutoWrite(defaultsConfig.getBoolean(PARAMNAME_AUTO_WRITE, 231 false)); 232 dbmConf.setTransactionCaches(defaultsConfig.getBoolean(PARAMNAME_TRANSACTION_CACHES, 233 false)); 234 dbmConf.setDeadlockWaitTime(defaultsConfig.getInt(PARAMNAME_DEADLOCK_READ_TIME, 235 0)); 236 dbmConf.setDeadlockRetryCount(defaultsConfig.getInt(PARAMNAME_DEADLOCK_RETRY_NUMBER, 237 0)); 238 QueryBuilder.setDefaultQueryTimeout(config.getInt(PARAMNAME_QUERY_TIMEOUT, 239 0)); 240 dbmConf.setQueryTimeout(defaultsConfig.getInt(PARAMNAME_QUERY_TIMEOUT, 241 0)); 242 dbmConf.setSelectOids(defaultsConfig.getBoolean(PARAMNAME_SELECT_OIDS, 243 false)); 244 dbmConf.setIncrementVersions(defaultsConfig.getBoolean(PARAMNAME_INCREMENT_VERSIONS, 245 true)); 246 QueryBuilder.setDefaultFetchSize(config.getInt(PARAMNAME_DEFAULT_FETCH_SIZE, 247 -1)); 248 dbmConf.setDefaultFetchSize(defaultsConfig.getInt(PARAMNAME_DEFAULT_FETCH_SIZE, 249 -1)); 250 dbmConf.setDBTransactionFactoryName(defaultsConfig.getString(CommonConstants.TRANSACTION_FACTORY, 251 null)); 252 dbmConf.setConnectionAllocatorName(defaultsConfig.getString(CommonConstants.CONNECTION_ALLOCATOR, 253 null)); 254 dbmConf.setDBConnectionFactoryName(defaultsConfig.getString(CommonConstants.CONNECTION_FACTORY, 255 null)); 256 dbmConf.setQueryCacheImplClass(defaultsConfig.getString(CommonConstants.QUERY_CACAHE_IMPL_CLASS, 257 null)); 258 dbmConf.setFullCacheCountLimit(defaultsConfig.getInt(CacheConstants.FULL_CACHE_COUNT_LIMIT, 259 CacheConstants.DEFAULT_FULL_CACHE_COUNT_LIMIT)); 260 dbmConf.setCaseInsensitiveDatabase(defaultsConfig.getBoolean(CommonConstants.CASE_INSENSITIVE_DATABASE, 261 CommonConstants.DEFAULT_CASE_INSENSITIVE_DATABASE)); 262 dbmConf.setXaDefaultTimeout(defaultsConfig.getInt(CommonConstants.XA_DEFAULT_TIMEOUT 263 ,CommonConstants.DEFAULT_XA_DEFAULT_TIMEOUT)); 264 dbmConf.setXaTransactonManagerLookupName(defaultsConfig.getString(CommonConstants.XA_TM_LOOKUP_NAME 265 ,CommonConstants.DEFAULT_XA_TM_LOOKUP_NAME)); 266 dbmConf.setXaUsageCase(defaultsConfig.getInt(CommonConstants.XA_USAGE_CASE 267 ,CommonConstants.DEFAULT_XA_USAGE_CASE)); 268 dbmConf.setXaWrappedTransImplFactory(defaultsConfig.getString(CommonConstants.XA_WRAPPED_TRANS_IMPL_FACTORY 269 ,CommonConstants.DEFAULT_XA_WRAPPED_TRANS_IMPL_FACTORY)); 270 dbmConf.setXaUserTransactonLookupName(defaultsConfig.getString(CommonConstants.XA_USER_TRANSACTION_LOOKUP_NAME 271 ,CommonConstants.DEFAULT_XA_USER_TRANSACTION_LOOKUP_NAME)); 272 dbmConf.setXaJtaSupport(defaultsConfig.getString(CommonConstants.XA_JTA_SUPPORT 273 ,CommonConstants.DEFAULT_XA_JTA_SUPPORT)); 274 try { 275 dbmConf.setUseCursorName(defaultsConfig.getBoolean(PARAMNAME_USE_CURSOR_NAME)); 276 } catch (ConfigException e) { 277 dbmConf.setUseCursorName(null); 278 } 279 dbmConf.setInitCachesResultSetType(defaultsConfig.getString(CommonConstants.INIT_CACHES_RESULT_SET_TYPE, 280 null)); 281 dbmConf.setInitCachesResultSetConcurrency(defaultsConfig.getString(CommonConstants.INIT_CACHES_RESULT_SET_CONCURRENCY, 282 null)); 283 dbmConf.setSqlBatch(defaultsConfig.getBoolean(CommonConstants.SQL_BATCH, 284 CommonConstants.DEFAULT_SQL_BATCH)); 285 try { 286 dbmConf.setQueryTimeLimit(new Integer (defaultsConfig.getInt(CommonConstants.QUERY_TIME_LIMIT))); 287 } catch (ConfigException e) { 288 dbmConf.setQueryTimeLimit(null); 289 } 290 } 291 try { 292 defaultsConfig = (Config) config.getSection("defaults.cache"); 293 } catch (KeywordValueException except) { 294 throw new ConfigException("No DatabaseManager.defaults defined in config file."); 295 } 296 297 if (defaultsConfig != null) { 298 dbmConf.setReserveFactor(defaultsConfig.getDouble(PARAMNAME_RESERVE_FACTOR, 299 CacheConstants.DEFAULT_RESERVE_FACTOR)); 300 dbmConf.setCachePercentage(defaultsConfig.getDouble(PARAMNAME_CACHE_PERCENTAGE, 301 CacheConstants.DEFAULT_CACHE_PERCENTAGE)); 302 dbmConf.setMaxCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_CACHE_SIZE, 303 CacheConstants.DEFAULT_MAX_CACHE_SIZE)); 304 dbmConf.setMaxSimpleCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_SIMPLE_CACHE_SIZE, 305 CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE)); 306 dbmConf.setMaxComplexCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_COMPLEX_CACHE_SIZE, 307 CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE)); 308 dbmConf.setMaxMultiJoinCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_MULTI_JOIN_CACHE_SIZE, 309 CacheConstants.DEFAULT_MAX_MULTI_JOIN_QUERY_CACHE_SIZE)); 310 dbmConf.setInitAllCaches(defaultsConfig.getBoolean(PARAMNAME_INITIAL_ALL_CACHES, 311 CacheConstants.DEFAULT_INITIAL_ALL_CACHES)); 312 dbmConf.setInitialCacheFetchSize(defaultsConfig.getInt(CacheConstants.PARAMNAME_INITIAL_CACHE_FETCH_SIZE, 313 CacheConstants.DEFAULT_INITIAL_CACHE_FETCH_SIZE)); 314 dbmConf.setInitialDSCacheSize(defaultsConfig.getInt(CacheConstants.PARAMNAME_INITIAL_DS_CACHE_SIZE, 315 CacheConstants.DEFAULT_INITIAL_DS_CACHE_SIZE)); 316 dbmConf.setDodsCacheFactory(defaultsConfig.getString(CacheConstants.PARAMNAME_DODS_CACHE_FACTORY 317 ,CacheConstants.DEFAULT_DODS_CACHE_FACTORY)); 318 } 319 320 isDODSConfigured = null != System.getProperty("DODS_HOME", null) 321 || null != config.getString("ConfigurationDir", null); 322 mustConfigureDODS = false; 324 325 328 Config dbConfig; 329 330 for (int idx = 0; idx < databases.length; idx++) { 331 String dbName = databases[idx]; 332 333 try { 334 dbConfig = (Config) config.getSection("DB." + dbName); 335 } catch (KeywordValueException except) { 336 throw new ConfigException("No DatabaseManager.DB." + dbName 337 + " defined in config file."); 338 } 339 340 if (logicalDatabases.get(dbName) != null) { 341 throw new DatabaseManagerException("duplicate logical database name: \"" 342 + dbName + "\""); 343 } 344 345 LogicalDatabase logicalDatabase = loadLogicalDatabase(dbName,dbConfig); 346 347 logicalDatabases.put(dbName, logicalDatabase); 348 } 349 350 353 defaultDB = config.getString("DefaultDatabase", databases[0]); 354 setDefaultDatabase(defaultDB); 355 356 359 boolean debugLogging = config.getBoolean("Debug", false); 360 361 setDebugLogging(debugLogging); 362 Wrapper.getInstance().setTimeout(config.getLong("MainCacheLockTimeout", 363 100)); 364 365 376 String oidColumnName = config.getString("ObjectIdColumnName", null); 377 378 if (oidColumnName != null) { 379 CoreDO.set_OIdColumnName(oidColumnName); 380 } 381 String versionColumnName = config.getString("VersionColumnName", null); 382 383 if (versionColumnName != null) { 384 CoreDO.set_versionColumnName(versionColumnName); 385 } 386 387 if(mainConfig == null) { 388 ConfigFileInterface mainConfigFile = null; 389 if(config!=null) 390 mainConfigFile = config.getConfigFile(); 391 if(mainConfigFile!=null) 392 mainConfig = mainConfigFile.getConfig(); 393 } 394 } 395 396 397 398 399 public void initChaches(ClassLoader clsLoader) { 400 try{ 401 initAllDodsCaches(this.config, clsLoader); 402 }catch(Exception e){ 403 DODS.getLogChannel().write(Logger.DEBUG,"Unable to load caches at startup"); 404 } 405 } 406 407 408 409 410 420 public static DatabaseManager newInstance(URL confURL, String confFile) 421 throws ConfigException, DatabaseManagerException, SQLException { 422 try { 423 InputStream configIS = Common.getConfFileFromURL(confURL, confFile); 424 ConfigFile configFile = new ConfigFile(configIS); 425 Config config = configFile.getConfig(); 426 configIS.close(); 427 Config dbConfig = new Config(config.getSection("DatabaseManager"),configFile); 428 return new StandardDatabaseManager(dbConfig); 429 } catch (Exception e) { 430 throw new ConfigException(e); 431 } 432 } 433 434 435 436 444 public static DatabaseManager newInstance(String fileName) 445 throws ConfigException, DatabaseManagerException, SQLException { 446 try { 447 Config config = null; 448 ConfigFile configFile = null; 449 if(fileName!=null) { 450 try { 451 File inputFile = new File(fileName); 452 if(inputFile!=null && inputFile.isFile()) { 453 configFile = new ConfigFile(inputFile); 454 config = configFile.getConfig(); 455 } 456 } catch (Exception e) { 457 System.out.println("Error during configuration file reading: '"+fileName+"'"); 458 } 459 } 460 if (config==null){ 461 InputStream configIS=Common.getConfFileFromURL(null,null); 462 configFile = new ConfigFile(configIS); 463 config = configFile.getConfig(); 464 configIS.close(); 465 } 466 if (config==null){ 467 throw new Exception ("Can't create configuration object from file:'"+fileName+"'"); 468 } 469 Config dbConfig = new Config(config.getSection("DatabaseManager"),configFile); 470 return new StandardDatabaseManager(dbConfig); 471 } catch (Exception e) { 472 throw new ConfigException(e); 473 } 474 } 475 476 482 public Config getConfig() { 483 return config; 484 } 485 486 492 493 public Config getParentConfig() { 494 495 return mainConfig; 496 } 497 498 499 500 boolean isDODSConfigured = false; 501 boolean mustConfigureDODS = false; 502 503 512 public LogicalDatabase loadLogicalDatabase(String dbName, Config dbConfig) 513 throws DatabaseManagerException { 514 LogicalDatabase lDB = null; 515 516 try { 517 if (mustConfigureDODS && !isDODSConfigured) { 518 throw new DatabaseManagerException("DODS is not configured properly."); 519 } 520 if (isDODSConfigured) { 521 String dbClassName = dbConfig.getString("ClassType", "Standard"); 522 String classPath = Common.getDodsConfProperty("ClassPath", 523 dbClassName); 524 String className = Common.getDodsConfProperty("ClassName", 525 dbClassName); 526 527 if (classPath != null && className != null) { 528 MultiClassLoader loader = new MultiClassLoader(null); 529 530 loader.setClassPath(classPath); 531 Class dbClass = loader.loadClass(className); 532 533 lDB = (LogicalDatabase) dbClass.newInstance(); 534 lDB.init(dbName, dbConfig); 535 } 536 } 537 if (lDB == null) { 538 lDB = new StandardLogicalDatabase(dbName, dbConfig, dbmConf); 539 } 540 } catch (Exception except) { 541 throw new DatabaseManagerException("Could not create logical database " 542 + dbName, 543 except); 544 } 545 return lDB; 546 } 547 548 553 public String getDefaultDB() { 554 return defaultDB; 555 } 556 557 577 public DBConnection allocateConnection(String dbName) 578 throws DatabaseManagerException, SQLException { 579 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 580 581 return logicalDatabase.allocateConnection(); 582 } 583 584 603 public DBConnection allocateConnection() 604 throws DatabaseManagerException, SQLException { 605 if (defaultLogicalDatabase == null) { 606 throw new DatabaseManagerException("Default logical database " 607 + "has not been specified."); 608 } 609 return defaultLogicalDatabase.allocateConnection(); 610 } 611 612 623 public ObjectId allocateObjectId(String dbName) 624 throws DatabaseManagerException, ObjectIdException { 625 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 626 627 return logicalDatabase.allocateObjectId(); 628 } 629 630 641 public ObjectId allocateObjectId() 642 throws DatabaseManagerException, ObjectIdException { 643 if (defaultLogicalDatabase == null) { 644 throw new DatabaseManagerException("Default logical database " 645 + "has not been specified."); 646 } 647 return defaultLogicalDatabase.allocateObjectId(); 648 } 649 650 662 public void checkOId(String dbName, ObjectId oid) 663 throws DatabaseManagerException, ObjectIdException { 664 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 665 666 logicalDatabase.checkOId(oid); 667 } 668 669 679 public void checkOId(ObjectId oid) 680 throws DatabaseManagerException, ObjectIdException { 681 if (defaultLogicalDatabase == null) { 682 throw new DatabaseManagerException("Default logical database " 683 + "has not been specified."); 684 } 685 defaultLogicalDatabase.checkOId(oid); 686 } 687 688 699 public DBTransaction createTransaction(String dbName) 700 throws DatabaseManagerException, SQLException { 701 DBTransaction ret; 702 703 if (dbName == null) { 704 return createTransaction(); 705 } 706 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 707 708 ret = logicalDatabase.createTransaction(); 709 ret.setDatabaseName(dbName); 710 return ret; 711 } 712 713 724 public DBTransaction createTransaction() 725 throws DatabaseManagerException, SQLException { 726 DBTransaction ret; 727 728 if (defaultLogicalDatabase == null) { 729 throw new DatabaseManagerException("Default logical database " 730 + "has not been specified."); 731 } 732 ret = defaultLogicalDatabase.createTransaction(); 733 ret.setDatabaseName(defaultDB); 734 return ret; 735 } 736 737 748 public DBQuery createQuery(String dbName) 749 throws DatabaseManagerException, SQLException { 750 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 751 752 return logicalDatabase.createQuery(); 753 } 754 755 766 public DBQuery createQuery() 767 throws DatabaseManagerException, SQLException { 768 if (defaultLogicalDatabase == null) { 769 throw new DatabaseManagerException("Default logical database " 770 + "has not been specified."); 771 } 772 return defaultLogicalDatabase.createQuery(); 773 } 774 775 786 public String logicalDatabaseType(String dbName) 787 throws DatabaseManagerException, SQLException { 788 LogicalDatabase logicalDatabase = null; 789 790 if (dbName != null) { 791 logicalDatabase = findLogicalDatabase(dbName); 792 return logicalDatabase.getType(); 793 } else { 794 return logicalDatabaseType(); 795 } 796 } 797 798 809 public String logicalDatabaseType() 810 throws DatabaseManagerException, SQLException { 811 if (defaultLogicalDatabase == null) { 812 throw new DatabaseManagerException("Default logical database " 813 + "has not been specified."); 814 } 815 return defaultLogicalDatabase.getType(); 816 } 817 818 825 public LogicalDatabase findLogicalDatabase(String dbName) 826 throws DatabaseManagerException { 827 LogicalDatabase logicalDatabase = (LogicalDatabase) logicalDatabases.get(dbName); 828 829 if (logicalDatabase == null) { 830 throw new DatabaseManagerException 831 ("unknown logical database name: \"" + dbName + "\""); 832 } 833 return logicalDatabase; 834 } 835 836 846 public void setDefaultDatabase(String dbName) 847 throws DatabaseManagerException { 848 defaultLogicalDatabase = findLogicalDatabase(dbName); 849 } 850 851 855 public void shutdown() { 856 for (Enumeration keys = logicalDatabases.keys(); 857 keys.hasMoreElements();) { 858 LogicalDatabase logicalDatabase = (LogicalDatabase) 859 logicalDatabases.get((String ) keys.nextElement()); 860 861 logicalDatabase.shutdown(); 862 } 863 } 864 865 873 public String [] getLogicalDatabaseNames() { 874 String [] names = new String [logicalDatabases.size()]; 875 int idx = 0; 876 Enumeration keys = logicalDatabases.keys(); 877 878 while (keys.hasMoreElements()) { 879 names[idx++] = (String ) keys.nextElement(); 880 } 881 return names; 882 } 883 884 894 public String getType(String dbName) 895 throws DatabaseManagerException { 896 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 897 898 return logicalDatabase.getType(); 899 } 900 901 909 public long getRequestCount(String dbName) 910 throws DatabaseManagerException { 911 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 912 913 return logicalDatabase.getRequestCount(); 914 } 915 916 924 public int getActiveConnectionCount(String dbName) 925 throws DatabaseManagerException { 926 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 927 928 return logicalDatabase.getActiveConnectionCount(); 929 } 930 931 943 public int getMaxConnectionCount(String dbName) 944 throws DatabaseManagerException { 945 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 946 947 return logicalDatabase.getMaxConnectionCount(); 948 } 949 950 959 public Date getMaxConnectionCountDate(String dbName) 960 throws DatabaseManagerException { 961 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 962 963 return logicalDatabase.getMaxConnectionCountDate(); 964 } 965 966 975 public void resetMaxConnectionCount(String dbName) 976 throws DatabaseManagerException { 977 LogicalDatabase logicalDatabase = findLogicalDatabase(dbName); 978 979 logicalDatabase.resetMaxConnectionCount(); 980 return; 981 } 982 983 988 public void setDebugLogging(boolean condition) { 989 debug = condition; 990 } 991 992 997 public boolean getAllReadOnly() { 998 return dbmConf.isAllReadOnly(); 999 } 1000 1001 1006 public String getAppName() { 1007 return appName; 1008 } 1009 1010 public DatabaseManagerConfiguration getDatabaseManagerConfiguration() { 1011 return dbmConf; 1012 } 1013 1014 1015 1020 private void initDatabaseCaches(Config dbConfig,String fileName, ClassLoader cls){ 1021 String tablesXmlFile=null; 1022 try { 1023 tablesXmlFile = dbConfig.getString("ClassList",fileName); 1024 } catch (ConfigException e) {} 1025 InputStream tablesList = null; 1026 if (tablesXmlFile!=null){ 1027 tablesList = getTablesXML(tablesXmlFile,cls); 1028 }else{ 1029 tablesList = getTablesXML((String )null,cls); 1030 } 1031 if(tablesList!=null){ 1032 initCaches(tablesList, cls); 1033 } 1034 } 1035 1036 1041 public void initAllDodsCaches(Config config, ClassLoader cls){ 1042 Config dbConfig = null; 1043 String [] databases = null; 1044 String tablesXmlFile = null; 1045 try { 1046 databases = config.getStrings("Databases"); 1047 tablesXmlFile = config.getString("ClassList",(String )null); 1048 } catch (ConfigException e) { 1049 DODS.getLogChannel().write(Logger.DEBUG,"Unable to init caches: Error reading application configuation file"); 1050 } 1051 if(databases!=null){ 1052 for (int idx = 0; idx < databases.length; idx++) { 1053 String dbName = databases[idx]; 1054 try { 1055 dbConfig = (Config) config.getSection("DB." + dbName); 1056 initDatabaseCaches(dbConfig,tablesXmlFile, cls); 1057 } catch (Exception except) { 1058 DODS.getLogChannel().write(Logger.DEBUG,"Unable to init caches for database: "+dbName); 1059 } 1060 } 1061 } 1062 } 1063 1064 1069 private InputStream getTablesXML(String fileName, ClassLoader cls){ 1070 InputStream res=null; 1071 if(fileName!=null){ 1072 try{ 1073 File tablesFile = new File(fileName); 1074 if (tablesFile.isFile()) { 1075 res= new FileInputStream (fileName); 1076 } 1077 }catch(Exception e){ 1078 DODS.getLogChannel().write(Logger.DEBUG,"Unable to read table names from file: "+fileName); 1079 } 1080 }else{ 1081 try{ 1082 res = cls.getResourceAsStream("org/enhydra/dods/DODSClassList.xml"); 1084 }catch(Exception e){ 1085 DODS.getLogChannel().write(Logger.DEBUG,"Unable to read table names from resources: org.enhydra.dods.DODSClassList.xml"); 1086 } 1087 } 1088 return res; 1089 } 1090 1091 1095 private void initCaches(InputStream tablesList, ClassLoader cls){ 1096 Document doc = null; 1097 try { 1098 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 1099 DocumentBuilder builder = factory.newDocumentBuilder(); 1100 doc = builder.parse(tablesList); 1101 } catch (Exception e) { 1102 DODS.getLogChannel().write(Logger.DEBUG,"Unable to parse xml file with cache initialisation tables list."); 1103 } 1104 if(doc!=null){ 1105 NodeList classNodes = doc.getElementsByTagName("CLASS"); 1106 for (int i = 0; i < classNodes.getLength(); i++) { 1107 String className = ((Element )classNodes.item(i)).getAttribute("name"); 1108 try { 1109 Class ce = cls.loadClass(className); 1110 Method mth = ce.getMethod("getCacheDodsTableName",new Class [] {}); 1111 mth.invoke(null, new Object []{}); 1112 } catch (Exception e1) { 1113 e1.printStackTrace(); 1114 DODS.getLogChannel().write(Logger.DEBUG,"Unable to invoke caches for class: "+className); 1115 } 1116 } 1117 } 1118 } 1119 1120} 1121 | Popular Tags |