001: /**
002: * com.mckoi.database.GTCurrentConnectionsDataSource 23 Mar 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import java.util.ArrayList;
026: import java.util.Date;
027:
028: /**
029: * An implementation of MutableTableDataSource that presents the current
030: * list of connections on the database.
031: * <p>
032: * NOTE: This is not designed to be a long kept object. It must not last
033: * beyond the lifetime of a transaction.
034: *
035: * @author Tobias Downer
036: */
037:
038: final class GTCurrentConnectionsDataSource extends GTDataSource {
039:
040: /**
041: * The DatabaseConnection object that this is table is modelling the
042: * information within.
043: */
044: private DatabaseConnection database;
045:
046: /**
047: * The list of info keys/values in this object.
048: */
049: private ArrayList key_value_pairs;
050:
051: /**
052: * Constructor.
053: */
054: public GTCurrentConnectionsDataSource(DatabaseConnection connection) {
055: super (connection.getSystem());
056: this .database = connection;
057: this .key_value_pairs = new ArrayList();
058: }
059:
060: /**
061: * Initialize the data source.
062: */
063: public GTCurrentConnectionsDataSource init() {
064:
065: UserManager user_manager = database.getDatabase()
066: .getUserManager();
067: // Synchronize over the user manager while we inspect the information,
068: synchronized (user_manager) {
069: for (int i = 0; i < user_manager.userCount(); ++i) {
070: User user = user_manager.userAt(i);
071: key_value_pairs.add(user.getUserName());
072: key_value_pairs.add(user.getConnectionString());
073: key_value_pairs
074: .add(new Date(user.getLastCommandTime()));
075: key_value_pairs.add(new Date(user.getTimeConnected()));
076: }
077: }
078:
079: return this ;
080: }
081:
082: // ---------- Implemented from GTDataSource ----------
083:
084: public DataTableDef getDataTableDef() {
085: return DEF_DATA_TABLE_DEF;
086: }
087:
088: public int getRowCount() {
089: return key_value_pairs.size() / 4;
090: }
091:
092: public TObject getCellContents(final int column, final int row) {
093: switch (column) {
094: case 0: // username
095: return columnValue(column, (String) key_value_pairs
096: .get(row * 4));
097: case 1: // host_string
098: return columnValue(column, (String) key_value_pairs
099: .get((row * 4) + 1));
100: case 2: // last_command
101: return columnValue(column, (Date) key_value_pairs
102: .get((row * 4) + 2));
103: case 3: // time_connected
104: return columnValue(column, (Date) key_value_pairs
105: .get((row * 4) + 3));
106: default:
107: throw new Error("Column out of bounds.");
108: }
109: }
110:
111: // ---------- Overwritten from GTDataSource ----------
112:
113: public void dispose() {
114: super .dispose();
115: key_value_pairs = null;
116: database = null;
117: }
118:
119: // ---------- Static ----------
120:
121: /**
122: * The data table def that describes this table of data source.
123: */
124: static final DataTableDef DEF_DATA_TABLE_DEF;
125:
126: static {
127:
128: DataTableDef def = new DataTableDef();
129: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
130: "sUSRCurrentConnections"));
131:
132: // Add column definitions
133: def.addColumn(stringColumn("username"));
134: def.addColumn(stringColumn("host_string"));
135: def.addColumn(dateColumn("last_command"));
136: def.addColumn(dateColumn("time_connected"));
137:
138: // Set to immutable
139: def.setImmutable();
140:
141: DEF_DATA_TABLE_DEF = def;
142:
143: }
144:
145: }
|