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