KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > GTCurrentConnectionsDataSource


1 /**
2  * com.mckoi.database.GTCurrentConnectionsDataSource 23 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29
30 /**
31  * An implementation of MutableTableDataSource that presents the current
32  * list of connections on the database.
33  * <p>
34  * NOTE: This is not designed to be a long kept object. It must not last
35  * beyond the lifetime of a transaction.
36  *
37  * @author Tobias Downer
38  */

39
40 final class GTCurrentConnectionsDataSource extends GTDataSource {
41
42   /**
43    * The DatabaseConnection object that this is table is modelling the
44    * information within.
45    */

46   private DatabaseConnection database;
47
48   /**
49    * The list of info keys/values in this object.
50    */

51   private ArrayList JavaDoc key_value_pairs;
52
53   /**
54    * Constructor.
55    */

56   public GTCurrentConnectionsDataSource(DatabaseConnection connection) {
57     super(connection.getSystem());
58     this.database = connection;
59     this.key_value_pairs = new ArrayList JavaDoc();
60   }
61
62   /**
63    * Initialize the data source.
64    */

65   public GTCurrentConnectionsDataSource init() {
66
67     UserManager user_manager = database.getDatabase().getUserManager();
68     // Synchronize over the user manager while we inspect the information,
69
synchronized (user_manager) {
70       for (int i = 0; i < user_manager.userCount(); ++i) {
71         User user = user_manager.userAt(i);
72         key_value_pairs.add(user.getUserName());
73         key_value_pairs.add(user.getConnectionString());
74         key_value_pairs.add(new Date JavaDoc(user.getLastCommandTime()));
75         key_value_pairs.add(new Date JavaDoc(user.getTimeConnected()));
76       }
77     }
78
79     return this;
80   }
81
82   // ---------- Implemented from GTDataSource ----------
83

84   public DataTableDef getDataTableDef() {
85     return DEF_DATA_TABLE_DEF;
86   }
87
88   public int getRowCount() {
89     return key_value_pairs.size() / 4;
90   }
91
92   public TObject getCellContents(final int column, final int row) {
93     switch (column) {
94       case 0: // username
95
return columnValue(column, (String JavaDoc) key_value_pairs.get(row * 4));
96       case 1: // host_string
97
return columnValue(column, (String JavaDoc) key_value_pairs.get((row * 4) + 1));
98       case 2: // last_command
99
return columnValue(column, (Date JavaDoc) key_value_pairs.get((row * 4) + 2));
100       case 3: // time_connected
101
return columnValue(column, (Date JavaDoc) key_value_pairs.get((row * 4) + 3));
102       default:
103         throw new Error JavaDoc("Column out of bounds.");
104     }
105   }
106
107   // ---------- Overwritten from GTDataSource ----------
108

109   public void dispose() {
110     super.dispose();
111     key_value_pairs = null;
112     database = null;
113   }
114
115   // ---------- Static ----------
116

117   /**
118    * The data table def that describes this table of data source.
119    */

120   static final DataTableDef DEF_DATA_TABLE_DEF;
121
122   static {
123
124     DataTableDef def = new DataTableDef();
125     def.setTableName(
126              new TableName(Database.SYSTEM_SCHEMA, "sUSRCurrentConnections"));
127
128     // Add column definitions
129
def.addColumn(stringColumn("username"));
130     def.addColumn(stringColumn("host_string"));
131     def.addColumn(dateColumn("last_command"));
132     def.addColumn(dateColumn("time_connected"));
133
134     // Set to immutable
135
def.setImmutable();
136
137     DEF_DATA_TABLE_DEF = def;
138
139   }
140
141 }
142
Popular Tags