KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > db > DBMetaDataProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.editor.completion.db;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.TreeMap JavaDoc;
31 import java.util.WeakHashMap JavaDoc;
32 import org.netbeans.api.db.explorer.DatabaseConnection;
33 import org.netbeans.modules.db.api.explorer.MetaDataListener;
34 import org.openide.ErrorManager;
35
36 /**
37  * Provides metadata information for a database connection.
38  *
39  * <p>This class is not thread-safe, its methods should be called from the same
40  * thread.</p>
41  *
42  * @author Andrei Badea
43  */

44 public class DBMetaDataProvider {
45     
46     // The return types of the methods returning a series of objects are array
47
// instead of List in order to be consistent with dbschema.
48

49     // Maps java.sql.Connection-s to DB metadata providers.
50
private static final Map JavaDoc/*<Connection, DBMetadataProvider>*/ CONN_TO_PROVIDER = new WeakHashMap JavaDoc();
51     
52     // must be a weak reference -- we don't want to prevent the connection from being GCd
53
// it is OK to be a weak reference -- the connection is hold strongly by the DB Explorer
54
// while connected
55
private final Reference JavaDoc/*<Connection>*/ conn;
56     private final String JavaDoc driverClass;
57     
58     private Map JavaDoc/*<String, Catalog>*/ catalogs;
59     
60     /**
61      * Returns a DB metadata provider for the given connection.
62      */

63     public static synchronized DBMetaDataProvider get(Connection JavaDoc conn, String JavaDoc driverClass) {
64         assert conn != null;
65         DBMetaDataProvider provider = (DBMetaDataProvider)CONN_TO_PROVIDER.get(conn);
66         if (provider == null) {
67             provider = new DBMetaDataProvider(conn, driverClass);
68             CONN_TO_PROVIDER.put(conn, provider);
69         }
70         return provider;
71     }
72     
73     public static MetaDataListener createMetaDataListener() {
74         return new MetaDataListenerImpl();
75     }
76     
77     public DBMetaDataProvider(Connection JavaDoc conn, String JavaDoc driverClass) {
78         this.conn = new WeakReference JavaDoc(conn);
79         this.driverClass = driverClass;
80     }
81     
82     public String JavaDoc getDefaultCatalog() throws SQLException JavaDoc {
83         return getConnection().getCatalog();
84     }
85     
86     /**
87      * Returns the catalogs. Note that for some databases (e.g. Derby) only a
88      * Catalog instance with a null name will be returned.
89      */

90     public synchronized Catalog[] getCatalogs() throws SQLException JavaDoc {
91         if (catalogs == null) {
92             catalogs = new TreeMap JavaDoc(new CatalogComparator());
93             
94             ResultSet JavaDoc rs = getMetaData().getCatalogs();
95             
96             try {
97                 while (rs.next()) {
98                     String JavaDoc catalogName = rs.getString("TABLE_CAT"); // NOI18N
99
Catalog catalog = new Catalog(this, catalogName);
100                     catalogs.put(catalogName, catalog);
101                     //if (catalogName != null) {
102
// // testing for null since Derby returns only a null catalog here
103
// result.add(catalogName);
104
//}
105
}
106             } finally {
107                 rs.close();
108             }
109             
110             if (catalogs.size() <= 0) {
111                 Catalog defaultCatalog = new Catalog(this, null);
112                 catalogs.put(null, defaultCatalog);
113             }
114         }
115         
116         return (Catalog[])catalogs.values().toArray(new Catalog[catalogs.size()]);
117     }
118     
119     public synchronized Catalog getCatalog(String JavaDoc name) throws SQLException JavaDoc {
120         if (catalogs == null) {
121             getCatalogs();
122         }
123         
124         return (Catalog)catalogs.get(name);
125     }
126     
127     Connection JavaDoc getConnection() {
128         return (Connection JavaDoc)conn.get();
129     }
130     
131     String JavaDoc getDriverClass() {
132         return driverClass;
133     }
134     
135     DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
136         return getConnection().getMetaData();
137     }
138     
139     public String JavaDoc toString() {
140         return "DBMetadataProvider[conn=" + getConnection() + "]"; // NOI18N
141
}
142     
143     private static final class CatalogComparator implements Comparator JavaDoc {
144         
145         public boolean equals(Object JavaDoc that) {
146             return that instanceof CatalogComparator;
147         }
148         
149         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
150             String JavaDoc name1 = (String JavaDoc)o1;
151             String JavaDoc name2 = (String JavaDoc)o2;
152             
153             if (name1 == null) {
154                 return (name2 == null) ? 0 : -1;
155             } else {
156                 return (name2 == null) ? 1 : name1.compareTo(name2);
157             }
158         }
159     }
160
161     private static final class MetaDataListenerImpl implements MetaDataListener {
162         
163         public MetaDataListenerImpl() {
164         }
165
166         public void tablesChanged(final DatabaseConnection dbconn) {
167             try {
168                 Schema schema = getSchema(dbconn);
169                 if (schema != null) {
170                     schema.refresh();
171                 }
172             } catch (SQLException JavaDoc e) {
173                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
174             }
175         }
176
177         public void tableChanged(DatabaseConnection dbconn, String JavaDoc tableName) {
178             tablesChanged(dbconn);
179         }
180
181         private Schema getSchema(DatabaseConnection dbconn) throws SQLException JavaDoc {
182             Connection JavaDoc conn = dbconn.getJDBCConnection();
183             if (conn == null) {
184                 return null;
185             }
186             DBMetaDataProvider provider;
187             synchronized (this) {
188                 provider = (DBMetaDataProvider)CONN_TO_PROVIDER.get(conn);
189             }
190             if (provider == null) {
191                 return null;
192             }
193             Catalog catalog = provider.getCatalog(conn.getCatalog());
194             if (catalog == null) {
195                 return null;
196             }
197             return catalog.getSchema(dbconn.getSchema());
198         }
199     }
200 }
201
Popular Tags