1 8 9 package org.enhydra.server; 10 11 import java.util.Date ; 12 13 import com.lutris.appserver.server.Application; 14 import com.lutris.appserver.server.sql.DatabaseManager; 15 import com.lutris.appserver.server.sql.DatabaseManagerException; 16 import com.lutris.util.Config; 17 import com.lutris.util.KeywordValueException; 18 19 24 public class DatabaseInfo { 25 26 29 private String [] logicalDbNames = null; 30 31 35 private String currentDatabase = null; 36 37 41 private String defaultLogicalDbName = "N/A"; 42 private String type = "N/A"; 43 private String dbType = "N/A"; 44 private String activeConnections = "N/A"; 45 private String peakConnections = "N/A"; 46 private Date maxConnectionCountDate = null;; 47 private String totalRequests = "N/A"; 48 49 52 private DatabaseManager dm; 53 54 57 private Config config; 58 59 67 public DatabaseInfo(Application app, Config appConfig) 68 throws DatabaseManagerException, KeywordValueException { 69 70 if (app != null) { 71 dm = app.getDatabaseManager(); if (dm != null) { 73 this.logicalDbNames = dm.getLogicalDatabaseNames(); 74 if(logicalDbNames == null || logicalDbNames.length == 0) 75 return; 76 else if (appConfig != null && appConfig.containsKey("DatabaseManager.DefaultDatabase")) { 77 this.config = appConfig; 78 79 String tempDefaultDb = (String )appConfig.get("DatabaseManager.DefaultDatabase"); 80 if(this.existDbName(tempDefaultDb)) { 81 this.defaultLogicalDbName = tempDefaultDb; 82 this.currentDatabase = tempDefaultDb; 83 } 84 else { 85 this.defaultLogicalDbName = logicalDbNames[0]; 86 this.currentDatabase = logicalDbNames[0]; 87 } 88 } 89 else { 90 this.defaultLogicalDbName = logicalDbNames[0]; 91 this.currentDatabase = logicalDbNames[0]; 92 } 93 94 if(dm.getClass().getName().equals("com.lutris.appserver.server.sql.StandardDatabaseManager")) 96 this.type = "Enhydra Standard Database Manager"; else 98 this.type = "N/A"; 99 100 this.getDatabaseManagerParameters(currentDatabase); 101 } 102 else 103 log("WARNING: DatabaseManager = null !"); 104 } 105 else 106 log("WARNING: app = null !"); 107 } 108 109 115 private void getDatabaseManagerParameters(String dbName) 116 throws DatabaseManagerException { 117 if(this.existDbName(dbName) && dm != null) { 118 this.currentDatabase = dbName; 119 120 long temp = dm.getActiveConnectionCount(dbName); 121 if(temp == -1) 122 this.activeConnections = "N/A"; 123 else 124 this.activeConnections = String.valueOf(temp); 125 126 temp = dm.getRequestCount(dbName); 127 if(temp == -1) 128 this.totalRequests = "N/A"; 129 else 130 this.totalRequests = String.valueOf(temp); 131 132 temp = dm.getMaxConnectionCount(dbName); 133 if(temp == -1) 134 this.peakConnections = "N/A"; 135 else 136 this.peakConnections = String.valueOf(temp); 137 138 this.maxConnectionCountDate = dm.getMaxConnectionCountDate(dbName); 139 140 String tempString = dm.getType(dbName); 141 if(tempString == null) 142 this.dbType = "N/A"; 143 else 144 this.dbType = tempString; 145 } 146 else { 147 this.currentDatabase = null; 148 this.activeConnections = "N/A"; 149 this.totalRequests = "N/A"; 150 this.peakConnections = "N/A"; 151 this.maxConnectionCountDate = null; 152 this.dbType = "N/A"; 153 } 154 } 155 156 162 private boolean existDbName(String dBname) { 163 if(dBname == null || logicalDbNames == null) 164 return false; 165 for (int i = 0; i < this.logicalDbNames.length; i++) { 166 if (logicalDbNames[i].equalsIgnoreCase(dBname)) 167 return true; 168 } 169 return false; 170 } 171 172 178 public String [] getLogicalDbNames() { 179 return logicalDbNames; 180 } 181 182 187 public String getDbManagerType() { 188 return this.type; 189 } 190 191 198 public String getDbType(String dBname) throws DatabaseManagerException { 199 if(this.existDbName(dBname)) { 200 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 201 this.getDatabaseManagerParameters(dBname); 202 return this.dbType; 203 } 204 else 205 return "N/A"; 206 } 207 208 216 public String getActiveConnections(String dBname) throws DatabaseManagerException { 217 if(this.existDbName(dBname)) { 218 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 219 this.getDatabaseManagerParameters(dBname); 220 return this.activeConnections; 221 } 222 else 223 return "N/A"; 224 } 225 226 234 public String getPeakConnections(String dBname) throws DatabaseManagerException { 235 if(this.existDbName(dBname)) { 236 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 237 this.getDatabaseManagerParameters(dBname); 238 return this.peakConnections; 239 } 240 else 241 return "N/A"; 242 } 243 244 252 public Date getPeakConnectionsDate(String dBname) throws DatabaseManagerException { 253 if(this.existDbName(dBname)) { 254 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 255 this.getDatabaseManagerParameters(dBname); 256 return this.maxConnectionCountDate; 257 } 258 else 259 return null; 260 } 261 262 269 public String getTotalRequests(String dBname) throws DatabaseManagerException { 270 if(this.existDbName(dBname)) { 271 if(this.currentDatabase==null || !dBname.equalsIgnoreCase(this.currentDatabase)) 272 this.getDatabaseManagerParameters(dBname); 273 return this.totalRequests; 274 } 275 else 276 return null; 277 } 278 279 285 public void setDefaultLogicalDbName(String dBname) 286 throws DatabaseManagerException, KeywordValueException { 287 if(this.existDbName(dBname)) { 288 this.dm.setDefaultDatabase(dBname); 289 this.defaultLogicalDbName = dBname; 290 this.config.set("DatabaseManager.DefaultDatabase",dBname); 291 } 292 } 293 294 298 public String getDefaultLogicalDbName() { 299 return this.defaultLogicalDbName; 300 } 301 302 307 public void resetMaxConnectionCount(String dBname) throws DatabaseManagerException { 308 if(this.existDbName(dBname)) { 309 this.dm.resetMaxConnectionCount(dBname); 310 this.getDatabaseManagerParameters(dBname); 311 } 312 } 313 314 318 private void log(String msg) { 319 System.err.println("EnhydraServer, Database Info: "+msg); 321 } 322 } | Popular Tags |