001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.internaldb;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: import java.sql.Connection;
022: import java.sql.DatabaseMetaData;
023: import java.sql.ResultSet;
024: import java.sql.ResultSetMetaData;
025: import java.sql.SQLException;
026: import java.util.Hashtable;
027: import java.util.Map;
028:
029: public class InternalDBHelper {
030: private final static Log log = LogFactory
031: .getLog(InternalDBHelper.class);
032:
033: private static final int RDBMS_DERBY = 1;
034:
035: private static final int RDBMS_MSSQL = 2;
036:
037: private static final String JNDI_DERBY = "java:comp/env/SystemDatasource";
038:
039: private static final Map derbyDBInfo = new Hashtable();
040:
041: /**
042: * Returns the database metadata as a map.
043: */
044: public Map getDBInfo() {
045: derbyDBInfo.clear();
046: Connection conn = null;
047: try {
048: conn = DerbyConnectionUtil.getSystemDBConnection();
049: DatabaseMetaData dbMD = (DatabaseMetaData) conn
050: .getMetaData();
051:
052: // DB
053: derbyDBInfo.put("URL", removeNull(dbMD.getURL()));
054: derbyDBInfo.put("Username", removeNull(dbMD.getUserName()));
055: derbyDBInfo.put("Read Only", removeNull(String.valueOf(dbMD
056: .isReadOnly())));
057: derbyDBInfo.put("DB Product Name", removeNull(dbMD
058: .getDatabaseProductName()));
059: derbyDBInfo.put("DB Product Version", removeNull(dbMD
060: .getDatabaseProductVersion()));
061: derbyDBInfo.put("DB Major Version", removeNull(String
062: .valueOf(dbMD.getDatabaseMajorVersion())));
063: derbyDBInfo.put("DB Minor Version", removeNull(String
064: .valueOf(dbMD.getDatabaseMinorVersion())));
065:
066: // Driver
067: derbyDBInfo.put("Driver Name", removeNull(dbMD
068: .getDriverName()));
069: derbyDBInfo.put("Driver Version", removeNull(dbMD
070: .getDriverVersion()));
071: derbyDBInfo.put("Driver Major Version", removeNull(String
072: .valueOf(dbMD.getDriverMajorVersion())));
073: derbyDBInfo.put("Driver Minor Version", removeNull(String
074: .valueOf(dbMD.getDriverMinorVersion())));
075:
076: // JDBC
077: derbyDBInfo.put("JDBC Major Version", removeNull(String
078: .valueOf(dbMD.getJDBCMajorVersion())));
079: derbyDBInfo.put("JDBC Minor Version", removeNull(String
080: .valueOf(dbMD.getJDBCMinorVersion())));
081:
082: // Functions
083: derbyDBInfo.put("Numeric Functions", removeNull(dbMD
084: .getNumericFunctions()));
085: derbyDBInfo.put("String Functions", removeNull(dbMD
086: .getStringFunctions()));
087: derbyDBInfo.put("System Functions", removeNull(dbMD
088: .getSystemFunctions()));
089: derbyDBInfo.put("Time Date Functions", removeNull(dbMD
090: .getTimeDateFunctions()));
091:
092: // Etc
093: derbyDBInfo.put("Supported SQL Keywords", removeNull(dbMD
094: .getSQLKeywords().replace(',', ' ')));
095: derbyDBInfo.put("Supported Types",
096: removeNull(getColumnData(dbMD.getTypeInfo(),
097: "TYPE_NAME")));
098: derbyDBInfo.put("Table Types", removeNull(getColumnData(
099: dbMD.getTableTypes(), "TABLE_TYPE")));
100: derbyDBInfo.put("Schemas", removeNull(getColumnData(dbMD
101: .getSchemas(), "TABLE_SCHEM")));
102: String tx = null;
103:
104: switch (dbMD.getDefaultTransactionIsolation()) {
105: case Connection.TRANSACTION_NONE:
106: tx = "not supported";
107: break;
108: case Connection.TRANSACTION_READ_COMMITTED:
109: tx = "dirty reads are prevented; non-repeatable reads and phantom reads can occur";
110: break;
111: case Connection.TRANSACTION_READ_UNCOMMITTED:
112: tx = "dirty reads, non-repeatable reads and phantom reads can occur";
113: break;
114: case Connection.TRANSACTION_REPEATABLE_READ:
115: tx = "dirty reads and non-repeatable reads are prevented; phantom reads can occur";
116: break;
117: case Connection.TRANSACTION_SERIALIZABLE:
118: tx = "dirty reads, non-repeatable reads and phantom reads are prevented";
119: break;
120: default:
121: tx = "";
122: break;
123: }
124:
125: derbyDBInfo.put("Default Transaction Isolation",
126: removeNull(tx));
127: String holdability = null;
128:
129: switch (dbMD.getResultSetHoldability()) {
130: case ResultSet.HOLD_CURSORS_OVER_COMMIT:
131: holdability = "hold cursors over commit";
132: break;
133: case ResultSet.CLOSE_CURSORS_AT_COMMIT:
134: holdability = "close cursors at commit";
135: break;
136: default:
137: holdability = "";
138: break;
139: }
140: derbyDBInfo.put("Result Set Holdability",
141: removeNull(holdability));
142: String sqlStateType = null;
143:
144: switch (dbMD.getSQLStateType()) {
145: case DatabaseMetaData.sqlStateXOpen:
146: sqlStateType = "X/Open SQL CLI";
147: break;
148: case DatabaseMetaData.sqlStateSQL99:
149: sqlStateType = "SQL99";
150: break;
151: default:
152: sqlStateType = "";
153: break;
154: }
155: derbyDBInfo.put("SQL State Type", removeNull(sqlStateType));
156: } catch (SQLException e) {
157: printSQLError((SQLException) e);
158: } finally {
159: // close DB connection
160: try {
161: if (conn != null) {
162: conn.close();
163: }
164: } catch (SQLException e) {
165: // problem closing DB connection
166: }
167: }
168:
169: return derbyDBInfo;
170: }
171:
172: private String removeNull(String s) {
173: return ((s == null) ? "" : s);
174: }
175:
176: /**
177: * Get a specific column data as a string separated by ','.
178: */
179: private String getColumnData(ResultSet rs, String colName) {
180: StringBuffer result = new StringBuffer();
181: try {
182: ResultSetMetaData rsmd = rs.getMetaData();
183:
184: // 1) Get column number
185: int selectedCol = -1;
186: int numberOfColumns = rsmd.getColumnCount();
187: for (int i = 1; i <= numberOfColumns; i++) {
188: String columnName = rsmd.getColumnName(i);
189: if (columnName.equals(colName)) {
190: selectedCol = i;
191: break;
192: }
193: }
194:
195: // 2) Get data
196: boolean firstData = true;
197: while (rs.next()) {
198: for (int i = 1; i <= numberOfColumns; i++) {
199: if (i == selectedCol) {
200: if (firstData) {
201: firstData = false;
202: } else {
203: result.append(',');
204: }
205: String columnValue = rs.getString(i);
206: result.append(columnValue);
207: }
208: }
209: }
210: } catch (SQLException e) {
211: printSQLError((SQLException) e);
212: }
213:
214: return result.toString();
215: }
216:
217: /**
218: * Print the SQL exception including chained exceptions
219: * if there is one.
220: *
221: * @param e
222: */
223: private void printSQLError(SQLException e) {
224: while (e != null) {
225: log.error(e.toString(), e);
226: e = e.getNextException();
227: }
228: }
229:
230: }
|