KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > internaldb > InternalDBHelper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.internaldb;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Map JavaDoc;
30
31 public class InternalDBHelper {
32     private final static Log log = LogFactory.getLog(InternalDBHelper.class);
33
34     private static final int RDBMS_DERBY = 1;
35
36     private static final int RDBMS_MSSQL = 2;
37
38     private static final String JavaDoc JNDI_DERBY = "java:comp/env/SystemDatasource";
39
40     private static final Map JavaDoc derbyDBInfo = new Hashtable JavaDoc();
41
42     /**
43      * Returns the database metadata as a map.
44      */

45     public Map JavaDoc getDBInfo() {
46         derbyDBInfo.clear();
47         Connection JavaDoc conn = null;
48         try {
49             conn = DerbyConnectionUtil.getSystemDBConnection();
50             DatabaseMetaData JavaDoc dbMD = (DatabaseMetaData JavaDoc) conn.getMetaData();
51
52             // DB
53
derbyDBInfo.put("URL", removeNull(dbMD.getURL()));
54             derbyDBInfo.put("Username", removeNull(dbMD.getUserName()));
55             derbyDBInfo.put("Read Only", removeNull(String.valueOf(dbMD
56                     .isReadOnly())));
57             derbyDBInfo.put("DB Product Name", removeNull(dbMD
58                     .getDatabaseProductName()));
59             derbyDBInfo.put("DB Product Version", removeNull(dbMD
60                     .getDatabaseProductVersion()));
61             derbyDBInfo.put("DB Major Version", removeNull(String.valueOf(dbMD
62                     .getDatabaseMajorVersion())));
63             derbyDBInfo.put("DB Minor Version", removeNull(String.valueOf(dbMD
64                     .getDatabaseMinorVersion())));
65
66             // Driver
67
derbyDBInfo.put("Driver Name", removeNull(dbMD.getDriverName()));
68             derbyDBInfo.put("Driver Version", removeNull(dbMD
69                     .getDriverVersion()));
70             derbyDBInfo.put("Driver Major Version", removeNull(String
71                     .valueOf(dbMD.getDriverMajorVersion())));
72             derbyDBInfo.put("Driver Minor Version", removeNull(String
73                     .valueOf(dbMD.getDriverMinorVersion())));
74
75             // JDBC
76
derbyDBInfo.put("JDBC Major Version", removeNull(String
77                     .valueOf(dbMD.getJDBCMajorVersion())));
78             derbyDBInfo.put("JDBC Minor Version", removeNull(String
79                     .valueOf(dbMD.getJDBCMinorVersion())));
80
81             // Functions
82
derbyDBInfo.put("Numeric Functions", removeNull(dbMD
83                     .getNumericFunctions()));
84             derbyDBInfo.put("String Functions", removeNull(dbMD
85                     .getStringFunctions()));
86             derbyDBInfo.put("System Functions", removeNull(dbMD
87                     .getSystemFunctions()));
88             derbyDBInfo.put("Time Date Functions", removeNull(dbMD
89                     .getTimeDateFunctions()));
90
91             // Etc
92
derbyDBInfo.put("Supported SQL Keywords", removeNull(dbMD
93                     .getSQLKeywords().replace(',', ' ')));
94             derbyDBInfo.put("Supported Types", removeNull(getColumnData(dbMD
95                     .getTypeInfo(), "TYPE_NAME")));
96             derbyDBInfo.put("Table Types", removeNull(getColumnData(dbMD
97                     .getTableTypes(), "TABLE_TYPE")));
98             derbyDBInfo.put("Schemas", removeNull(getColumnData(dbMD
99                     .getSchemas(), "TABLE_SCHEM")));
100             String JavaDoc tx = null;
101
102             switch (dbMD.getDefaultTransactionIsolation()) {
103             case Connection.TRANSACTION_NONE:
104                 tx = "not supported";
105                 break;
106             case Connection.TRANSACTION_READ_COMMITTED:
107                 tx = "dirty reads are prevented; non-repeatable reads and phantom reads can occur";
108                 break;
109             case Connection.TRANSACTION_READ_UNCOMMITTED:
110                 tx = "dirty reads, non-repeatable reads and phantom reads can occur";
111                 break;
112             case Connection.TRANSACTION_REPEATABLE_READ:
113                 tx = "dirty reads and non-repeatable reads are prevented; phantom reads can occur";
114                 break;
115             case Connection.TRANSACTION_SERIALIZABLE:
116                 tx = "dirty reads, non-repeatable reads and phantom reads are prevented";
117                 break;
118             default:
119                 tx = "";
120                 break;
121             }
122
123             derbyDBInfo.put("Default Transaction Isolation", removeNull(tx));
124             String JavaDoc holdability = null;
125
126             switch (dbMD.getResultSetHoldability()) {
127             case ResultSet.HOLD_CURSORS_OVER_COMMIT:
128                 holdability = "hold cursors over commit";
129                 break;
130             case ResultSet.CLOSE_CURSORS_AT_COMMIT:
131                 holdability = "close cursors at commit";
132                 break;
133             default:
134                 holdability = "";
135                 break;
136             }
137             derbyDBInfo.put("Result Set Holdability", removeNull(holdability));
138             String JavaDoc sqlStateType = null;
139
140             switch (dbMD.getSQLStateType()) {
141             case DatabaseMetaData.sqlStateXOpen:
142                 sqlStateType = "X/Open SQL CLI";
143                 break;
144             case DatabaseMetaData.sqlStateSQL99:
145                 sqlStateType = "SQL99";
146                 break;
147             default:
148                 sqlStateType = "";
149                 break;
150             }
151             derbyDBInfo.put("SQL State Type", removeNull(sqlStateType));
152         } catch (SQLException JavaDoc e) {
153             printSQLError((SQLException JavaDoc) e);
154         } finally {
155             // close DB connection
156
try {
157                 if (conn != null) {
158                     conn.close();
159                 }
160             } catch (SQLException JavaDoc e) {
161                 // problem closing DB connection
162
}
163         }
164
165         return derbyDBInfo;
166     }
167
168     private String JavaDoc removeNull(String JavaDoc s) {
169         return ((s == null) ? "" : s);
170     }
171
172     /**
173      * Get a specific column data as a string separated by ','.
174      */

175     private String JavaDoc getColumnData(ResultSet JavaDoc rs, String JavaDoc colName) {
176         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
177         try {
178             ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
179
180             // 1) Get column number
181
int selectedCol = -1;
182             int numberOfColumns = rsmd.getColumnCount();
183             for (int i = 1; i <= numberOfColumns; i++) {
184                 String JavaDoc columnName = rsmd.getColumnName(i);
185                 if (columnName.equals(colName)) {
186                     selectedCol = i;
187                     break;
188                 }
189             }
190
191             // 2) Get data
192
boolean firstData = true;
193             while (rs.next()) {
194                 for (int i = 1; i <= numberOfColumns; i++) {
195                     if (i == selectedCol) {
196                         if (firstData) {
197                             firstData = false;
198                         } else {
199                             result.append(',');
200                         }
201                         String JavaDoc columnValue = rs.getString(i);
202                         result.append(columnValue);
203                     }
204                 }
205             }
206         } catch (SQLException JavaDoc e) {
207             printSQLError((SQLException JavaDoc) e);
208         }
209
210         return result.toString();
211     }
212
213     /**
214      * Print the SQL exception including chained exceptions
215      * if there is one.
216      *
217      * @param e
218      */

219     private void printSQLError(SQLException JavaDoc e) {
220         while (e != null) {
221             log.error(e.toString(), e);
222             e = e.getNextException();
223         }
224     }
225
226 }
Popular Tags