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.api.db.explorer;
043:
044: import javax.swing.SwingUtilities;
045: import org.netbeans.lib.ddl.DBConnection;
046: import org.netbeans.modules.db.explorer.ConnectionList;
047: import org.netbeans.modules.db.explorer.actions.ConnectUsingDriverAction;
048: import org.netbeans.modules.db.explorer.infos.RootNodeInfo;
049: import org.netbeans.modules.db.explorer.nodes.RootNode;
050: import org.openide.util.Exceptions;
051: import org.openide.util.Mutex;
052:
053: /**
054: * Provides access to the list of connections in the Database Explorer.
055: *
056: * <p>The list of connections can be retrieved using the {@link #getConnections}
057: * method. A connection can be also retrieved by name using the
058: * {@link #getConnection} method.</p>
059: *
060: * <p>New connections can be added to the Connection Manager using the
061: * {@link #addConnection} method (new connections can be created using the
062: * {@link DatabaseConnection#create} method.
063: * It is also possible to display the New Database Connection dialog to let the
064: * user create a new database connection using the {@link #showAddConnectionDialog}.
065: * Connections can be realized using the {@link #showConnectionDialog} method.</p>
066: *
067: * <p>Clients can be informed of changes to the ConnectionManager by registering
068: * a {@link ConnectionListener} using the {@link #addConnectionListener} method.</p>
069: *
070: * @see DatabaseConnection
071: *
072: * @author Andrei Badea
073: */
074: public final class ConnectionManager {
075:
076: /**
077: * The ConnectionManager singleton instance.
078: */
079: private static ConnectionManager DEFAULT;
080:
081: /**
082: * Gets the ConnectionManager singleton instance.
083: */
084: public static synchronized ConnectionManager getDefault() {
085: if (DEFAULT == null) {
086: DEFAULT = new ConnectionManager();
087: }
088: return DEFAULT;
089: }
090:
091: /**
092: * Returns the list of connections in the Database Explorer.
093: *
094: * @return a non-null array of connections.
095: */
096: public DatabaseConnection[] getConnections() {
097: DBConnection[] conns = ConnectionList.getDefault()
098: .getConnections();
099: DatabaseConnection[] dbconns = new DatabaseConnection[conns.length];
100: for (int i = 0; i < conns.length; i++) {
101: dbconns[i] = ((org.netbeans.modules.db.explorer.DatabaseConnection) conns[i])
102: .getDatabaseConnection();
103: }
104: return dbconns;
105: }
106:
107: /**
108: * Returns the connection with the specified name.
109: *
110: * @param name the connection name
111: *
112: * @throws NullPointerException if the specified database name is null.
113: */
114: public DatabaseConnection getConnection(String name) {
115: if (name == null) {
116: throw new NullPointerException();
117: }
118: DBConnection[] conns = ConnectionList.getDefault()
119: .getConnections();
120: for (int i = 0; i < conns.length; i++) {
121: DatabaseConnection dbconn = ((org.netbeans.modules.db.explorer.DatabaseConnection) conns[i])
122: .getDatabaseConnection();
123: if (name.equals(dbconn.getName())) {
124: return dbconn;
125: }
126: }
127: return null;
128: }
129:
130: /**
131: * Adds a new connection to Database Explorer. This method does not display any UI and
132: * does not try to connect to the respective database.
133: *
134: * @param dbconn the connection to be added; must not be null.
135: *
136: * @throws NullPointerException if dbconn is null.
137: * @throws DatabaseException if an error occurs while adding the connection.
138: */
139: public void addConnection(DatabaseConnection dbconn)
140: throws DatabaseException {
141: if (dbconn == null) {
142: throw new NullPointerException();
143: }
144: ((RootNodeInfo) RootNode.getInstance().getInfo())
145: .addConnectionNoConnect(dbconn.getDelegate());
146: }
147:
148: /**
149: * Shows the dialog for adding a new connection. The specified driver will be
150: * selected by default in the New Database Connection dialog.
151: *
152: * @param driver the JDBC driver; can be null.
153: */
154: public void showAddConnectionDialog(JDBCDriver driver) {
155: showAddConnectionDialog(driver, null, null, null);
156: }
157:
158: /**
159: * Shows the dialog for adding a new connection with the specified database URL.
160: * The specified driver be filled as the single element of the
161: * Driver combo box of the New Database Connection dialog box.
162: * The database URL will be filled in the Database URL field in the
163: * New Database Connection dialog box.
164: *
165: * @param driver the JDBC driver; can be null.
166: * @param databaseUrl the database URL; can be null.
167: */
168: public void showAddConnectionDialog(JDBCDriver driver,
169: final String databaseUrl) {
170: showAddConnectionDialog(driver, databaseUrl, null, null);
171: }
172:
173: /**
174: * Shows the dialog for adding a new connection with the specified database URL, user and password
175: * The specified driver be filled as the single element of the
176: * Driver combo box of the New Database Connection dialog box.
177: * The database URL will be filled in the Database URL field in the
178: * New Database Connection dialog box.
179: * The user and password will be filled in the User Name and Password
180: * fields in the New Database Connection dialog box.
181: *
182: * @param driver the JDBC driver; can be null.
183: * @param databaseUrl the database URL; can be null.
184: * @param user the database user; can be null.
185: * @param password user's password; can be null.
186: *
187: * @since 1.19
188: */
189: public void showAddConnectionDialog(final JDBCDriver driver,
190: final String databaseUrl, final String user,
191: final String password) {
192: Mutex.EVENT.readAccess(new Runnable() {
193: public void run() {
194: new ConnectUsingDriverAction.NewConnectionDialogDisplayer()
195: .showDialog(driver, databaseUrl, user, password);
196: }
197: });
198: }
199:
200: /**
201: * The counterpart of {@link #showAddConnectionDialog(JDBCDriver) } which returns
202: * the newly created database connection, but must be called from the event dispatching
203: * thread.
204: *
205: * @param driver the JDBC driver; can be null.
206: *
207: * @return the new database connection or null if no database connection
208: * was created (e.g. the user pressed Cancel).
209: *
210: * @throws IllegalStateException if the calling thread is not the event
211: * dispatching thread.
212: *
213: * @since 1.19
214: */
215: public DatabaseConnection showAddConnectionDialogFromEventThread(
216: JDBCDriver driver) {
217: return showAddConnectionDialogFromEventThread(driver, null,
218: null, null);
219: }
220:
221: /**
222: * The counterpart of {@link #showAddConnectionDialog(JDBCDriver, String) } which returns
223: * the newly created database connection, but must be called from the event dispatching
224: * thread.
225: *
226: * @param driver the JDBC driver; can be null.
227: * @param databaseUrl the database URL; can be null.
228: *
229: * @return the new database connection or null if no database connection
230: * was created (e.g. the user pressed Cancel).
231: *
232: * @throws IllegalStateException if the calling thread is not the event
233: * dispatching thread.
234: *
235: * @since 1.19
236: */
237: public DatabaseConnection showAddConnectionDialogFromEventThread(
238: JDBCDriver driver, String databaseUrl) {
239: return showAddConnectionDialogFromEventThread(driver,
240: databaseUrl, null, null);
241: }
242:
243: /**
244: * The counterpart of {@link #showAddConnectionDialog(JDBCDriver, String, String, String) }
245: * which returns the newly created database connection, but must be called
246: * from the event dispatching thread.
247: *
248: * @param driver the JDBC driver; can be null.
249: * @param databaseUrl the database URL; can be null.
250: * @param user the database user; can be null.
251: * @param password user's password; can be null.
252: *
253: * @return the new database connection or null if no database connection
254: * was created (e.g. the user pressed Cancel).
255: *
256: * @throws IllegalStateException if the calling thread is not the event
257: * dispatching thread.
258: *
259: * @since 1.19
260: */
261: public DatabaseConnection showAddConnectionDialogFromEventThread(
262: JDBCDriver driver, String databaseUrl, String user,
263: String password) {
264: if (!SwingUtilities.isEventDispatchThread()) {
265: throw new IllegalStateException(
266: "The current thread is not the event dispatching thread."); // NOI18N
267: }
268: org.netbeans.modules.db.explorer.DatabaseConnection internalDBConn = new ConnectUsingDriverAction.NewConnectionDialogDisplayer()
269: .showDialog(driver, databaseUrl, user, password);
270: if (internalDBConn != null) {
271: return internalDBConn.getDatabaseConnection();
272: }
273: return null;
274: }
275:
276: /**
277: * Shows the Connect dialog for the specified connection if not all data
278: * needed to connect, such as the user name or password,
279: * are known), or displays a modal progress dialog and attempts
280: * to connect to the database immediately.
281: *
282: * @param dbconn the database connection to be connected
283: *
284: * @throws NullPointerException if the dbconn parameter is null
285: * @throws IllegalStateException if this connection is not added to the
286: * ConnectionManager.
287: */
288: public void showConnectionDialog(DatabaseConnection dbconn) {
289: if (dbconn == null) {
290: throw new NullPointerException();
291: }
292: if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
293: throw new IllegalStateException(
294: "This connection is not added to the ConnectionManager."); // NOI18N
295: }
296: dbconn.getDelegate().showConnectionDialog();
297: }
298:
299: /**
300: * Disconnects this connection from the database. Does not do anything
301: * if not connected.
302: *
303: * @param dbconn the database connection to be connected
304: *
305: * @throws NullPointerException if the dbconn parameter is null
306: * @throws IllegalStateException if this connection is not added to the
307: * ConnectionManager.
308: */
309: public void disconnect(DatabaseConnection dbconn) {
310: if (dbconn == null) {
311: throw new NullPointerException();
312: }
313: if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
314: throw new IllegalStateException(
315: "This connection is not added to the ConnectionManager."); // NOI18N
316: }
317: try {
318: dbconn.getDelegate().disconnect();
319: } catch (DatabaseException e) {
320: // XXX maybe shouldn't catch the exception
321: Exceptions.printStackTrace(e);
322: }
323: }
324:
325: /**
326: * Selects the node corresponding to the specified connection in the
327: * Runtime tab.
328: *
329: * @param dbconn the connection to select
330: *
331: * @throws NullPointerException if the dbconn parameter is null
332: * @throws IllegalStateException if this connection is not added to the
333: * ConnectionManager.
334: */
335: public void selectConnectionInExplorer(DatabaseConnection dbconn) {
336: if (dbconn == null) {
337: throw new NullPointerException();
338: }
339: if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
340: throw new IllegalStateException(
341: "This connection is not added to the ConnectionManager."); // NOI18N
342: }
343: dbconn.getDelegate().selectInExplorer();
344: }
345:
346: /**
347: * Registers a ConnectionListener.
348: */
349: public void addConnectionListener(ConnectionListener listener) {
350: ConnectionList.getDefault().addConnectionListener(listener);
351: }
352:
353: /**
354: * Unregisters the specified connection listener.
355: */
356: public void removeConnectionListener(ConnectionListener listener) {
357: ConnectionList.getDefault().removeConnectionListener(listener);
358: }
359: }
|