1 19 20 package org.netbeans.modules.db.explorer.infos; 21 22 import java.io.IOException ; 23 import java.sql.Connection ; 24 import java.text.MessageFormat ; 25 26 import org.netbeans.lib.ddl.DBConnection; 27 import org.netbeans.lib.ddl.DatabaseProductNotFoundException; 28 import org.netbeans.lib.ddl.impl.DriverSpecification; 29 import org.netbeans.lib.ddl.impl.Specification; 30 import org.netbeans.lib.ddl.impl.SpecificationFactory; 31 32 import org.netbeans.api.db.explorer.DatabaseException; 33 import org.netbeans.modules.db.explorer.DatabaseConnection; 34 import org.netbeans.modules.db.explorer.ConnectionList; 35 import org.netbeans.modules.db.explorer.DerbyConectionEventListener; 36 37 42 import org.openide.nodes.Children; 43 import org.openide.nodes.Node; 44 45 public class ConnectionNodeInfo extends DatabaseNodeInfo implements ConnectionOperations { 46 47 static final long serialVersionUID =-8322295510950137669L; 48 49 private void connect(String dbsys) throws DatabaseException { 50 String drvurl = getDriver(); 51 String dburl = getDatabase(); 52 53 try { 54 69 DatabaseConnection con = new DatabaseConnection(drvurl, dburl, getUser(), getPassword()); 70 Connection connection = con.createJDBCConnection(); 71 72 finishConnect(dbsys, con, connection); 73 } catch (Exception e) { 74 DatabaseException dbe = new DatabaseException(e.getMessage()); 75 dbe.initCause(e); 76 throw dbe; 77 } 78 } 79 80 83 public void connect() throws DatabaseException { 84 connect((String )null); 85 } 86 87 93 public void connect(DBConnection conn) throws DatabaseException { 94 try { 95 String dbsys = null; 96 DatabaseConnection con = (DatabaseConnection) conn; 97 98 Connection connection = con.getConnection(); 99 100 SpecificationFactory factory = (SpecificationFactory) getSpecificationFactory(); 101 Specification spec; 102 DriverSpecification drvSpec; 103 104 if (dbsys != null) { 105 spec = (Specification) factory.createSpecification(con, dbsys, connection); 106 } else { 107 setReadOnly(false); 108 spec = (Specification) factory.createSpecification(con, connection); 109 } 110 put(DBPRODUCT, spec.getProperties().get(DBPRODUCT)); 111 112 setSpecification(spec); 113 114 drvSpec = factory.createDriverSpecification(spec.getMetaData().getDriverName().trim()); 115 if (spec.getMetaData().getDriverName().trim().equals("jConnect (TM) for JDBC (TM)")) drvSpec.setMetaData(connection.getMetaData()); 118 else 119 drvSpec.setMetaData(spec.getMetaData()); 120 drvSpec.setCatalog(connection.getCatalog()); 121 drvSpec.setSchema(getSchema()); 122 setDriverSpecification(drvSpec); 123 setConnection(connection); } catch (DatabaseProductNotFoundException e) { 125 setReadOnly(false); 126 connect("GenericDatabaseSystem"); } catch (Exception e) { 128 throw new DatabaseException(e.getMessage()); 129 } 130 } 131 132 public void finishConnect(String dbsys, DatabaseConnection con, Connection connection) throws DatabaseException { 133 try { 134 SpecificationFactory factory = (SpecificationFactory) getSpecificationFactory(); 135 Specification spec; 136 DriverSpecification drvSpec; 137 138 if (dbsys != null) { 139 spec = (Specification) factory.createSpecification(con, dbsys, connection); 140 } else { 141 setReadOnly(false); 142 spec = (Specification) factory.createSpecification(con, connection); 143 } 144 put(DBPRODUCT, spec.getProperties().get(DBPRODUCT)); 145 146 setSpecification(spec); 147 148 drvSpec = factory.createDriverSpecification(spec.getMetaData().getDriverName().trim()); 149 if (spec.getMetaData().getDriverName().trim().equals("jConnect (TM) for JDBC (TM)")) drvSpec.setMetaData(connection.getMetaData()); 152 else 153 drvSpec.setMetaData(spec.getMetaData()); 154 drvSpec.setCatalog(connection.getCatalog()); 155 drvSpec.setSchema(getSchema()); 156 setDriverSpecification(drvSpec); 157 setConnection(connection); } catch (DatabaseProductNotFoundException e) { 159 setReadOnly(false); 160 connect("GenericDatabaseSystem"); } catch (Exception e) { 162 throw new DatabaseException(e.getMessage()); 163 } 164 } 165 166 public void disconnect() throws DatabaseException { 167 Connection connection = getConnection(); 168 if (connection != null) { 169 String message = null; 170 try { 171 connection.close(); 172 setConnection(null); } catch (Exception exc) { 174 setConnection(null); 177 message = MessageFormat.format(bundle().getString("EXC_ConnectionError"), new String [] {exc.getMessage()}); } 179 180 DerbyConectionEventListener.getDefault().afterDisconnect(getDatabaseConnection(), connection); 182 183 if (message != null) { 184 throw new DatabaseException(message); 185 } 186 } 187 } 188 189 public void delete() throws IOException { 190 try { 191 DatabaseConnection cinfo = (DatabaseConnection) getDatabaseConnection(); 192 ConnectionList.getDefault().remove(cinfo); 193 } catch (Exception e) { 194 throw new IOException (e.getMessage()); 195 } 196 } 197 198 public Object put(Object key, Object obj) { 199 if (key.equals(USER) || key.equals(DRIVER) || key.equals(DATABASE) || key.equals(SCHEMA)) { 200 String newVal = (String )obj; 201 updateConnection((String )key, newVal); 202 } 203 return super.put(key, obj); 204 } 205 206 private void updateConnection(String key, String newVal) { 207 DatabaseConnection infoConn = getDatabaseConnection(); 208 DatabaseConnection connFromList = ConnectionList.getDefault().getConnection(infoConn); 209 if (connFromList != null) { 210 if (key.equals(SCHEMA)) 211 connFromList.setSchema(newVal); 212 else if (key.equals(USER)) 213 connFromList.setUser(newVal); 214 else if (key.equals(DRIVER)) { 215 connFromList.setDriver(newVal); 216 } else if (key.equals(DATABASE)) { 217 connFromList.setDatabase(newVal); 218 } 219 } 220 setName(infoConn.getName()); 221 } 222 223 public void refreshChildren() throws DatabaseException { 224 Children children = getNode().getChildren(); 225 Node[] nodes = children.getNodes(); 226 for (int i = 0; i < nodes.length; i++) { 227 DatabaseNodeInfo info = (DatabaseNodeInfo)nodes[i].getCookie(DatabaseNodeInfo.class); 228 info.refreshChildren(); 229 } 230 } 231 } 232 | Popular Tags |