KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 public class DBViewerHelper {
25
26     private static final String JavaDoc SYS_TBL_PREFIX = "SYS.";
27
28     private static final int COUNT_COL = 1;
29
30     /**
31      * List the databases given the derby home directory.
32      *
33      * @param derbySysHome
34      * @return
35      */

36     public Collection JavaDoc getDerbyDatabases(String JavaDoc derbySysHome) {
37         Vector JavaDoc databases = new Vector JavaDoc();
38         File JavaDoc f = new File JavaDoc(derbySysHome);
39         // Check if this is a directory
40
if (f.isDirectory()) {
41             // Check for folders only
42
File JavaDoc[] files = f.listFiles();
43             for (int i = 0; i < files.length; i++) {
44                 if (files[i].isDirectory()) {
45                     databases.add(files[i].getName());
46                 }
47             }
48         }
49
50         return databases;
51     }
52
53     /**
54      * @param derbySysHome
55      * @param dbName
56      * @return
57      */

58     public boolean isDBValid(String JavaDoc derbySysHome, String JavaDoc dbName) {
59         if ((derbySysHome == null) || (derbySysHome.trim().length() == 0)) {
60             return false;
61         }
62         if ((dbName == null) || (dbName.trim().length() == 0)) {
63             return false;
64         }
65
66         Collection JavaDoc databases = getDerbyDatabases(derbySysHome);
67         return databases.contains(dbName);
68     }
69
70     /**
71      * @param dbName
72      * @param tblName
73      * @return
74      */

75     public boolean isTblValid(String JavaDoc dbName, String JavaDoc tblName) {
76         if ((dbName == null) || (dbName.trim().length() == 0)) {
77             return false;
78         }
79         if ((tblName == null) || (tblName.trim().length() == 0)) {
80             return false;
81         }
82         return true;
83
84         // Removed this code because it doesn't seem necessary and causes a
85
// weird ClassCastException when rs.next() is called.
86
/*
87          else {
88          if (tblName.toUpperCase().startsWith(SYS_TBL_PREFIX)) {
89          tblName = tblName.substring(SYS_TBL_PREFIX.length());
90          }
91          }
92
93          Connection conn = null;
94          PreparedStatement ps = null;
95          ResultSet rs = null;
96          try {
97          conn = DerbyConnectionUtil.getDerbyConnection(dbName);
98          ps = conn.prepareStatement("SELECT count(*) FROM SYS.SYSTABLES"
99          + " 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 }
135
Popular Tags