1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion.db; 21 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.util.Comparator ; 29 import java.util.Map ; 30 import java.util.TreeMap ; 31 import java.util.WeakHashMap ; 32 import org.netbeans.api.db.explorer.DatabaseConnection; 33 import org.netbeans.modules.db.api.explorer.MetaDataListener; 34 import org.openide.ErrorManager; 35 36 44 public class DBMetaDataProvider { 45 46 49 private static final Map CONN_TO_PROVIDER = new WeakHashMap (); 51 52 private final Reference conn; 56 private final String driverClass; 57 58 private Map catalogs; 59 60 63 public static synchronized DBMetaDataProvider get(Connection conn, String 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 conn, String driverClass) { 78 this.conn = new WeakReference (conn); 79 this.driverClass = driverClass; 80 } 81 82 public String getDefaultCatalog() throws SQLException { 83 return getConnection().getCatalog(); 84 } 85 86 90 public synchronized Catalog[] getCatalogs() throws SQLException { 91 if (catalogs == null) { 92 catalogs = new TreeMap (new CatalogComparator()); 93 94 ResultSet rs = getMetaData().getCatalogs(); 95 96 try { 97 while (rs.next()) { 98 String catalogName = rs.getString("TABLE_CAT"); Catalog catalog = new Catalog(this, catalogName); 100 catalogs.put(catalogName, catalog); 101 } 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 name) throws SQLException { 120 if (catalogs == null) { 121 getCatalogs(); 122 } 123 124 return (Catalog)catalogs.get(name); 125 } 126 127 Connection getConnection() { 128 return (Connection )conn.get(); 129 } 130 131 String getDriverClass() { 132 return driverClass; 133 } 134 135 DatabaseMetaData getMetaData() throws SQLException { 136 return getConnection().getMetaData(); 137 } 138 139 public String toString() { 140 return "DBMetadataProvider[conn=" + getConnection() + "]"; } 142 143 private static final class CatalogComparator implements Comparator { 144 145 public boolean equals(Object that) { 146 return that instanceof CatalogComparator; 147 } 148 149 public int compare(Object o1, Object o2) { 150 String name1 = (String )o1; 151 String name2 = (String )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 e) { 173 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 174 } 175 } 176 177 public void tableChanged(DatabaseConnection dbconn, String tableName) { 178 tablesChanged(dbconn); 179 } 180 181 private Schema getSchema(DatabaseConnection dbconn) throws SQLException { 182 Connection 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 |