001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.io.IOException;
045: import java.sql.Connection;
046: import java.text.MessageFormat;
047:
048: import org.netbeans.lib.ddl.DBConnection;
049: import org.netbeans.lib.ddl.DatabaseProductNotFoundException;
050: import org.netbeans.lib.ddl.impl.DriverSpecification;
051: import org.netbeans.lib.ddl.impl.Specification;
052: import org.netbeans.lib.ddl.impl.SpecificationFactory;
053:
054: import org.netbeans.api.db.explorer.DatabaseException;
055: import org.netbeans.modules.db.explorer.DatabaseConnection;
056: import org.netbeans.modules.db.explorer.ConnectionList;
057: import org.netbeans.modules.db.explorer.DerbyConectionEventListener;
058:
059: //commented out for 3.6 release, need to solve for next Studio release
060: //import org.netbeans.modules.db.explorer.PointbasePlus;
061: //import org.openide.nodes.Node;
062: //import org.netbeans.modules.db.explorer.nodes.ConnectionNode;
063:
064: import org.openide.nodes.Children;
065: import org.openide.nodes.Node;
066:
067: public class ConnectionNodeInfo extends DatabaseNodeInfo implements
068: ConnectionOperations {
069:
070: static final long serialVersionUID = -8322295510950137669L;
071:
072: private void connect(String dbsys) throws DatabaseException {
073: String drvurl = getDriver();
074: String dburl = getDatabase();
075:
076: try {
077: //commented out for 3.6 release, need to solve for next Studio release
078: // check if there is connected connection by Pointbase driver
079: // Pointbase driver doesn't permit the concurrently connection
080: // if (drvurl.startsWith(PointbasePlus.DRIVER)) {
081: // Node n[] = getParent().getNode().getChildren().getNodes();
082: // for (int i = 0; i < n.length; i++)
083: // if (n[i] instanceof ConnectionNode) {
084: // ConnectionNodeInfo cinfo = (ConnectionNodeInfo)((ConnectionNode)n[i]).getInfo();
085: // if (cinfo.getDriver().startsWith(PointbasePlus.DRIVER))
086: // if (!(cinfo.getDatabase().equals(dburl)&&cinfo.getUser().equals(getUser())))
087: // if ((cinfo.getConnection()!=null))
088: // throw new Exception(bundle.getString("EXC_PBConcurrentConn")); // NOI18N
089: // }
090: // }
091:
092: DatabaseConnection con = new DatabaseConnection(drvurl,
093: dburl, getUser(), getPassword());
094: Connection connection = con.createJDBCConnection();
095:
096: finishConnect(dbsys, con, connection);
097: } catch (Exception e) {
098: DatabaseException dbe = new DatabaseException(e
099: .getMessage());
100: dbe.initCause(e);
101: throw dbe;
102: }
103: }
104:
105: /*
106: * Connects this connection node to the database.
107: */
108: public void connect() throws DatabaseException {
109: connect((String) null);
110: }
111:
112: /*
113: * Connect to this node a DBConnection which is already connected to the
114: * database. Used when adding a new connection: the newly added DBConnection is already
115: * connected to the database, so this methods helps avoiding connecting to the
116: * database once more.
117: */
118: public void connect(DBConnection conn) throws DatabaseException {
119: try {
120: DatabaseConnection con = (DatabaseConnection) conn;
121:
122: Connection connection = con.getConnection();
123:
124: SpecificationFactory factory = (SpecificationFactory) getSpecificationFactory();
125: Specification spec;
126: DriverSpecification drvSpec;
127:
128: setReadOnly(false);
129: spec = (Specification) factory.createSpecification(con,
130: connection);
131: put(DBPRODUCT, spec.getProperties().get(DBPRODUCT));
132:
133: setSpecification(spec);
134:
135: drvSpec = factory.createDriverSpecification(spec
136: .getMetaData().getDriverName().trim());
137: if (spec.getMetaData().getDriverName().trim().equals(
138: "jConnect (TM) for JDBC (TM)")) //NOI18N
139: //hack for Sybase ASE - I don't guess why spec.getMetaData doesn't work
140: drvSpec.setMetaData(connection.getMetaData());
141: else
142: drvSpec.setMetaData(spec.getMetaData());
143: drvSpec.setCatalog(connection.getCatalog());
144: drvSpec.setSchema(getSchema());
145: setDriverSpecification(drvSpec);
146: setConnection(connection); // fires change
147: } catch (DatabaseProductNotFoundException e) {
148: setReadOnly(false);
149: connect("GenericDatabaseSystem"); //NOI18N
150: } catch (Exception e) {
151: throw new DatabaseException(e.getMessage());
152: }
153: }
154:
155: public void finishConnect(String dbsys, DatabaseConnection con,
156: Connection connection) throws DatabaseException {
157: try {
158: SpecificationFactory factory = (SpecificationFactory) getSpecificationFactory();
159: Specification spec;
160: DriverSpecification drvSpec;
161:
162: if (dbsys != null) {
163: spec = (Specification) factory.createSpecification(con,
164: dbsys, connection);
165: } else {
166: setReadOnly(false);
167: spec = (Specification) factory.createSpecification(con,
168: connection);
169: }
170: put(DBPRODUCT, spec.getProperties().get(DBPRODUCT));
171:
172: setSpecification(spec);
173:
174: drvSpec = factory.createDriverSpecification(spec
175: .getMetaData().getDriverName().trim());
176: if (spec.getMetaData().getDriverName().trim().equals(
177: "jConnect (TM) for JDBC (TM)")) //NOI18N
178: //hack for Sybase ASE - I don't guess why spec.getMetaData doesn't work
179: drvSpec.setMetaData(connection.getMetaData());
180: else
181: drvSpec.setMetaData(spec.getMetaData());
182: drvSpec.setCatalog(connection.getCatalog());
183: drvSpec.setSchema(getSchema());
184: setDriverSpecification(drvSpec);
185: setConnection(connection); // fires change
186: } catch (DatabaseProductNotFoundException e) {
187: setReadOnly(false);
188: connect("GenericDatabaseSystem"); //NOI18N
189: } catch (Exception e) {
190: throw new DatabaseException(e.getMessage());
191: }
192: }
193:
194: public void disconnect() throws DatabaseException {
195: Connection connection = getConnection();
196: if (connection != null) {
197: String message = null;
198: try {
199: connection.close();
200: setConnection(null); // fires change
201: } catch (Exception exc) {
202: // connection is broken, connection state has been changed
203: setConnection(null); // fires change
204:
205: message = MessageFormat.format(bundle().getString(
206: "EXC_ConnectionError"), new String[] { exc
207: .getMessage() }); // NOI18N
208: }
209:
210: // XXX hack for Derby
211: DerbyConectionEventListener.getDefault().afterDisconnect(
212: getDatabaseConnection(), connection);
213:
214: if (message != null) {
215: throw new DatabaseException(message);
216: }
217: }
218: }
219:
220: public void delete() throws IOException {
221: try {
222: DatabaseConnection cinfo = (DatabaseConnection) getDatabaseConnection();
223: ConnectionList.getDefault().remove(cinfo);
224: } catch (Exception e) {
225: throw new IOException(e.getMessage());
226: }
227: }
228:
229: public Object put(Object key, Object obj) {
230: if (key.equals(USER) || key.equals(DRIVER)
231: || key.equals(DATABASE) || key.equals(SCHEMA)
232: || key.equals(REMEMBER_PWD)) {
233: updateConnection((String) key, obj);
234: }
235: return super .put(key, obj);
236: }
237:
238: private void updateConnection(String key, Object newVal) {
239: DatabaseConnection infoConn = getDatabaseConnection();
240: DatabaseConnection connFromList = ConnectionList.getDefault()
241: .getConnection(infoConn);
242: if (connFromList != null) {
243: if (key.equals(SCHEMA))
244: connFromList.setSchema((String) newVal);
245: else if (key.equals(USER))
246: connFromList.setUser((String) newVal);
247: else if (key.equals(DRIVER)) {
248: connFromList.setDriver((String) newVal);
249: } else if (key.equals(DATABASE)) {
250: connFromList.setDatabase((String) newVal);
251: } else if (key.equals(REMEMBER_PWD)) {
252: connFromList.setRememberPassword(((Boolean) newVal)
253: .booleanValue());
254: }
255: }
256: setName(infoConn.getName());
257: }
258:
259: public void refreshChildren() throws DatabaseException {
260: Children children = getNode().getChildren();
261: Node[] nodes = children.getNodes();
262: for (int i = 0; i < nodes.length; i++) {
263: DatabaseNodeInfo info = (DatabaseNodeInfo) nodes[i]
264: .getCookie(DatabaseNodeInfo.class);
265: info.refreshChildren();
266: }
267: }
268: }
|