KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > DatabaseInfo


1 /**
2  * Title: AdminGui
3  * Description: Obtaining DatabaseManager parameters from DatabaseManager and
4  * configuration file.
5  * @Author tufeX, tufex@uns.ns.ac.yu & Vladimir Radisic
6  * @Version 1.1.1
7  */

8
9 package org.enhydra.server;
10
11 import java.util.Date JavaDoc;
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 /**
20  * This class is used for obtaining information about particular application
21  * connected to its database properties. Information are provided from the
22  * DatabaseManager instance associated to specified application.
23  */

24 public class DatabaseInfo {
25
26 /**
27  * Array of database names used within application.
28  */

29   private String JavaDoc[] logicalDbNames = null;
30
31 /**
32  * Current database. This variable points to database, which parmeters are
33  * currently holding in the temporary private variables of the object.
34  */

35   private String JavaDoc currentDatabase = null;
36
37 /**
38  * Temporary storage for current database parameters obtained from
39  * DatabaseManager.
40  */

41   private String JavaDoc defaultLogicalDbName = "N/A";
42   private String JavaDoc type = "N/A";
43   private String JavaDoc dbType = "N/A";
44   private String JavaDoc activeConnections = "N/A";
45   private String JavaDoc peakConnections = "N/A";
46   private Date JavaDoc maxConnectionCountDate = null;;
47   private String JavaDoc totalRequests = "N/A";
48
49 /**
50  * Application databaseManager reference
51  */

52   private DatabaseManager dm;
53
54 /**
55  * Storage for application configure parameters represented as Config object
56  */

57   private Config config;
58
59 /**
60  * Construction with asociated application and coresponded configuration
61  * parameters represented via Config object.
62  * @param app associate aplication
63  * @param appConfig cofiguration file parameters represented as Config object
64  * @exceptions DatabaseManagerException
65  * @exceptions KeywordValueException
66  */

67   public DatabaseInfo(Application app, Config appConfig)
68       throws DatabaseManagerException, KeywordValueException {
69
70     if (app != null) {
71       dm = app.getDatabaseManager(); // getting DatabaseManager
72
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 JavaDoc tempDefaultDb = (String JavaDoc)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 instanceof com.lutris.appserver.server.sql.StandardDatabaseManager)
95
if(dm.getClass().getName().equals("com.lutris.appserver.server.sql.StandardDatabaseManager"))
96           this.type = "Enhydra Standard Database Manager"; //FIXME, hard coded
97
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 /**
110  * Fills the private arguments with parameters obtained from application
111  * Databasemanager for specified database name.
112  * @param dbName given database name.
113  * @exceptions DatabaseManagerException
114  */

115   private void getDatabaseManagerParameters(String JavaDoc 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 JavaDoc 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 /**
157  * Checks existence of given database name in the list of databases which
158  * correspond to application and which are obtained from DatabaseManager.
159  * @param dBname the name of the database which is checked.
160  * @return true = database name exists, false = database name does not exist.
161  */

162 private boolean existDbName(String JavaDoc 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 /**
173  * Returns list of logical database names obtained from application DatabaseManager
174  * object.
175  * @return list of logical database names represented as array of Strings, or null
176  * if application has no corresponding databases.
177  */

178   public String JavaDoc[] getLogicalDbNames() {
179     return logicalDbNames;
180   }
181
182 /**
183  * Gets the type of DatabaseManager instance.
184  * @return type of DatabaseManager instance represented as String, or N/A if
185  * this property is not available.
186  */

187   public String JavaDoc getDbManagerType() {
188       return this.type;
189   }
190
191 /**
192  * Gets database type for given database name from DatabaseManager instance.
193  * @param dBname name of database.
194  * @return database type represented as String, or N/A if this property is not
195  * available.
196  * @exceptions DatabaseManagerException
197  */

198   public String JavaDoc getDbType(String JavaDoc 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 /**
209  * Gets the number of currently active connections for the supplied logical
210  * database name.
211  * @param dBname name of database.
212  * @return number of currently active connections represented as String, or
213  * N/A if this property is not available.
214  * @exceptions DatabaseManagerException
215  */

216   public String JavaDoc getActiveConnections(String JavaDoc 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 /**
227  * Gets the maximum number of concurent connections that existed at any time
228  * since this object was created.
229  * @param dBname name of database.
230  * @return maximum number of concurent connections represented as String, or
231  * N/A if this property is not available.
232  * @exceptions DatabaseManagerException
233  */

234   public String JavaDoc getPeakConnections(String JavaDoc 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 /**
245  * Gets the date and time when maximum number of concurent connections occurs
246  * since this object was created.
247  * @param dBname name of database.
248  * @return date and time when maximum number of concurent connections occurs
249  * represented as Date object, or null if this property is not implemented
250  * @exceptions DatabaseManagerException
251  */

252   public Date JavaDoc getPeakConnectionsDate(String JavaDoc 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 /**
263  * Gets the number of requests made to the database since startup time
264  * @param dBname name of database.
265  * @return number of requests made to the database represented as String, or
266  * N/A if this property is not available.
267  * @exceptions DatabaseManagerException
268  */

269   public String JavaDoc getTotalRequests(String JavaDoc 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 /**
280  * Sets the default logical database name.
281  * @param dBname name of database which will be set as default.
282  * @exceptions DatabaseManagerException
283  * @exception KeywordValueException
284  */

285   public void setDefaultLogicalDbName(String JavaDoc 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 /**
295  * Gets the default logical database name.
296  * @return name of database which is set as default.
297  */

298   public String JavaDoc getDefaultLogicalDbName() {
299     return this.defaultLogicalDbName;
300   }
301
302 /**
303  * Reset the maximum connection count for the given logical database name.
304  * @param dBname name of database which connection count will be reset.
305  * @exceptions DatabaseManagerException
306  */

307   public void resetMaxConnectionCount(String JavaDoc dBname) throws DatabaseManagerException {
308     if(this.existDbName(dBname)) {
309       this.dm.resetMaxConnectionCount(dBname);
310       this.getDatabaseManagerParameters(dBname);
311     }
312   }
313
314 /**
315  * Used for logging of messages.
316  * @param msg message for logging represented as String.
317  */

318   private void log(String JavaDoc msg) {
319   // FIXME.DEBUG.Log to file
320
System.err.println("EnhydraServer, Database Info: "+msg);
321   }
322 }
Popular Tags