KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > infos > ConnectionNodeInfo


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.db.explorer.infos;
21
22 import java.io.IOException JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.text.MessageFormat JavaDoc;
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 //commented out for 3.6 release, need to solve for next Studio release
38
//import org.netbeans.modules.db.explorer.PointbasePlus;
39
//import org.openide.nodes.Node;
40
//import org.netbeans.modules.db.explorer.nodes.ConnectionNode;
41

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 JavaDoc dbsys) throws DatabaseException {
50         String JavaDoc drvurl = getDriver();
51         String JavaDoc dburl = getDatabase();
52         
53         try {
54 //commented out for 3.6 release, need to solve for next Studio release
55
// check if there is connected connection by Pointbase driver
56
// Pointbase driver doesn't permit the concurrently connection
57
// if (drvurl.startsWith(PointbasePlus.DRIVER)) {
58
// Node n[] = getParent().getNode().getChildren().getNodes();
59
// for (int i = 0; i < n.length; i++)
60
// if (n[i] instanceof ConnectionNode) {
61
// ConnectionNodeInfo cinfo = (ConnectionNodeInfo)((ConnectionNode)n[i]).getInfo();
62
// if (cinfo.getDriver().startsWith(PointbasePlus.DRIVER))
63
// if (!(cinfo.getDatabase().equals(dburl)&&cinfo.getUser().equals(getUser())))
64
// if ((cinfo.getConnection()!=null))
65
// throw new Exception(bundle.getString("EXC_PBConcurrentConn")); // NOI18N
66
// }
67
// }
68

69             DatabaseConnection con = new DatabaseConnection(drvurl, dburl, getUser(), getPassword());
70             Connection JavaDoc connection = con.createJDBCConnection();
71             
72             finishConnect(dbsys, con, connection);
73         } catch (Exception JavaDoc e) {
74             DatabaseException dbe = new DatabaseException(e.getMessage());
75             dbe.initCause(e);
76             throw dbe;
77         }
78     }
79
80     /*
81      * Connects this connection node to the database.
82      */

83     public void connect() throws DatabaseException {
84         connect((String JavaDoc)null);
85     }
86
87     /*
88      * Connect to this node a DBConnection which is already connected to the
89      * database. Used when adding a new connection: the newly added DBConnection is already
90      * connected to the database, so this methods helps avoiding connecting to the
91      * database once more.
92      */

93     public void connect(DBConnection conn) throws DatabaseException {
94         try {
95             String JavaDoc dbsys = null;
96             DatabaseConnection con = (DatabaseConnection) conn;
97             
98             Connection JavaDoc 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)")) //NOI18N
116
//hack for Sybase ASE - I don't guess why spec.getMetaData doesn't work
117
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); // fires change
124
} catch (DatabaseProductNotFoundException e) {
125             setReadOnly(false);
126             connect("GenericDatabaseSystem"); //NOI18N
127
} catch (Exception JavaDoc e) {
128             throw new DatabaseException(e.getMessage());
129         }
130     }
131     
132     public void finishConnect(String JavaDoc dbsys, DatabaseConnection con, Connection JavaDoc 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)")) //NOI18N
150
//hack for Sybase ASE - I don't guess why spec.getMetaData doesn't work
151
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); // fires change
158
} catch (DatabaseProductNotFoundException e) {
159             setReadOnly(false);
160             connect("GenericDatabaseSystem"); //NOI18N
161
} catch (Exception JavaDoc e) {
162             throw new DatabaseException(e.getMessage());
163         }
164     }
165     
166     public void disconnect() throws DatabaseException {
167         Connection JavaDoc connection = getConnection();
168         if (connection != null) {
169             String JavaDoc message = null;
170             try {
171                 connection.close();
172                 setConnection(null); // fires change
173
} catch (Exception JavaDoc exc) {
174                 // connection is broken, connection state has been changed
175
setConnection(null); // fires change
176

177                 message = MessageFormat.format(bundle().getString("EXC_ConnectionError"), new String JavaDoc[] {exc.getMessage()}); // NOI18N
178
}
179             
180             // XXX hack for Derby
181
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 JavaDoc {
190         try {
191             DatabaseConnection cinfo = (DatabaseConnection) getDatabaseConnection();
192             ConnectionList.getDefault().remove(cinfo);
193         } catch (Exception JavaDoc e) {
194             throw new IOException JavaDoc(e.getMessage());
195         }
196     }
197
198     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc obj) {
199         if (key.equals(USER) || key.equals(DRIVER) || key.equals(DATABASE) || key.equals(SCHEMA)) {
200             String JavaDoc newVal = (String JavaDoc)obj;
201             updateConnection((String JavaDoc)key, newVal);
202         }
203         return super.put(key, obj);
204     }
205     
206     private void updateConnection(String JavaDoc key, String JavaDoc 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