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 java.io.File;
019: import java.util.Collection;
020: import java.util.Vector;
021:
022: public class DBViewerHelper {
023:
024: private static final String SYS_TBL_PREFIX = "SYS.";
025:
026: private static final int COUNT_COL = 1;
027:
028: /**
029: * List the databases given the derby home directory.
030: *
031: * @param derbySysHome
032: * @return
033: */
034: public Collection getDerbyDatabases(String derbySysHome) {
035: Vector databases = new Vector();
036: File f = new File(derbySysHome);
037: // Check if this is a directory
038: if (f.isDirectory()) {
039: // Check for folders only
040: File[] files = f.listFiles();
041: for (int i = 0; i < files.length; i++) {
042: if (files[i].isDirectory()) {
043: databases.add(files[i].getName());
044: }
045: }
046: }
047:
048: return databases;
049: }
050:
051: /**
052: * @param derbySysHome
053: * @param dbName
054: * @return
055: */
056: public boolean isDBValid(String derbySysHome, String dbName) {
057: if ((derbySysHome == null)
058: || (derbySysHome.trim().length() == 0)) {
059: return false;
060: }
061: if ((dbName == null) || (dbName.trim().length() == 0)) {
062: return false;
063: }
064:
065: Collection databases = getDerbyDatabases(derbySysHome);
066: return databases.contains(dbName);
067: }
068:
069: /**
070: * @param dbName
071: * @param tblName
072: * @return
073: */
074: public boolean isTblValid(String dbName, String tblName) {
075: if ((dbName == null) || (dbName.trim().length() == 0)) {
076: return false;
077: }
078: if ((tblName == null) || (tblName.trim().length() == 0)) {
079: return false;
080: }
081: return true;
082:
083: // Removed this code because it doesn't seem necessary and causes a
084: // weird ClassCastException when rs.next() is called.
085: /*
086: else {
087: if (tblName.toUpperCase().startsWith(SYS_TBL_PREFIX)) {
088: tblName = tblName.substring(SYS_TBL_PREFIX.length());
089: }
090: }
091:
092: Connection conn = null;
093: PreparedStatement ps = null;
094: ResultSet rs = null;
095: try {
096: conn = DerbyConnectionUtil.getDerbyConnection(dbName);
097: ps = conn.prepareStatement("SELECT count(*) FROM SYS.SYSTABLES"
098: + " WHERE tablename=?");
099: ps.setString(1, tblName.toUpperCase());
100: rs = ps.executeQuery();
101: if (rs.next()) {
102: int count = rs.getInt(COUNT_COL);
103: if (count == 1) {
104: return true;
105: }
106: }
107: } catch (Throwable e) {
108: e.printStackTrace();
109: System.out.println("ERROR: " + e.getMessage());
110: // Assume table is not valid
111: return false;
112: } finally {
113: // close DB connection
114: try {
115: if (rs != null) {
116: rs.close();
117: }
118: if (ps != null) {
119: ps.close();
120: }
121: if (conn != null) {
122: conn.close();
123: }
124: } catch (SQLException e) {
125: // problem closing DB connection
126: }
127: }
128:
129: return false;
130: */
131: }
132:
133: }
|