KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > db > explorer > ConnectionManager


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.api.db.explorer;
21
22 import javax.swing.SwingUtilities JavaDoc;
23 import org.netbeans.lib.ddl.DBConnection;
24 import org.netbeans.modules.db.explorer.ConnectionList;
25 import org.netbeans.modules.db.explorer.actions.ConnectUsingDriverAction;
26 import org.netbeans.modules.db.explorer.infos.RootNodeInfo;
27 import org.netbeans.modules.db.explorer.nodes.RootNode;
28 import org.openide.ErrorManager;
29 import org.openide.util.Mutex;
30
31 /**
32  * Provides access to the list of connections in the Database Explorer.
33  *
34  * <p>The list of connections can be retrieved using the {@link #getConnections}
35  * method. A connection can be also retrieved by name using the
36  * {@link #getConnection} method.</p>
37  *
38  * <p>New connections can be added to the Connection Manager using the
39  * {@link #addConnection} method (new connections can be created using the
40  * {@link DatabaseConnection#create} method.
41  * It is also possible to display the New Database Connection dialog to let the
42  * user create a new database connection using the {@link #showAddConnectionDialog}.
43  * Connections can be realized using the {@link #showConnectionDialog} method.</p>
44  *
45  * <p>Clients can be informed of changes to the ConnectionManager by registering
46  * a {@link ConnectionListener} using the {@link #addConnectionListener} method.</p>
47  *
48  * @see DatabaseConnection
49  *
50  * @author Andrei Badea
51  */

52 public final class ConnectionManager {
53     
54     /**
55      * The ConnectionManager singleton instance.
56      */

57     private static ConnectionManager DEFAULT;
58     
59     /**
60      * Gets the ConnectionManager singleton instance.
61      */

62     public static synchronized ConnectionManager getDefault() {
63         if (DEFAULT == null) {
64             DEFAULT = new ConnectionManager();
65         }
66         return DEFAULT;
67     }
68     
69     /**
70      * Returns the list of connections in the Database Explorer.
71      *
72      * @return a non-null array of connections.
73      */

74     public DatabaseConnection[] getConnections() {
75         DBConnection[] conns = ConnectionList.getDefault().getConnections();
76         DatabaseConnection[] dbconns = new DatabaseConnection[conns.length];
77         for (int i = 0; i < conns.length; i++) {
78             dbconns[i] = ((org.netbeans.modules.db.explorer.DatabaseConnection)conns[i]).getDatabaseConnection();
79         }
80         return dbconns;
81     }
82     
83     /**
84      * Returns the connection with the specified name.
85      *
86      * @param name the connection name
87      *
88      * @throws NullPointerException if the specified database name is null.
89      */

90     public DatabaseConnection getConnection(String JavaDoc name) {
91         if (name == null) {
92             throw new NullPointerException JavaDoc();
93         }
94         DBConnection[] conns = ConnectionList.getDefault().getConnections();
95         for (int i = 0; i < conns.length; i++) {
96             DatabaseConnection dbconn = ((org.netbeans.modules.db.explorer.DatabaseConnection)conns[i]).getDatabaseConnection();
97             if (name.equals(dbconn.getName())) {
98                 return dbconn;
99             }
100         }
101         return null;
102     }
103     
104     /**
105      * Adds a new connection to Database Explorer. This method does not display any UI and
106      * does not try to connect to the respective database.
107      *
108      * @param dbconn the connection to be added; must not be null.
109      *
110      * @throws NullPointerException if dbconn is null.
111      * @throws DatabaseException if an error occurs while adding the connection.
112      */

113     public void addConnection(DatabaseConnection dbconn) throws DatabaseException {
114         if (dbconn == null) {
115             throw new NullPointerException JavaDoc();
116         }
117         ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnectionNoConnect(dbconn.getDelegate());
118     }
119     
120     /**
121      * Shows the dialog for adding a new connection. The specified driver will be
122      * selected by default in the New Database Connection dialog.
123      *
124      * @param driver the JDBC driver; can be null.
125      */

126     public void showAddConnectionDialog(JDBCDriver driver) {
127         showAddConnectionDialog(driver, null, null, null);
128     }
129     
130     /**
131      * Shows the dialog for adding a new connection with the specified database URL.
132      * The specified driver be filled as the single element of the
133      * Driver combo box of the New Database Connection dialog box.
134      * The database URL will be filled in the Database URL field in the
135      * New Database Connection dialog box.
136      *
137      * @param driver the JDBC driver; can be null.
138      * @param databaseUrl the database URL; can be null.
139      */

140     public void showAddConnectionDialog(JDBCDriver driver, final String JavaDoc databaseUrl) {
141         showAddConnectionDialog(driver, databaseUrl, null, null);
142     }
143     
144     /**
145      * Shows the dialog for adding a new connection with the specified database URL, user and password
146      * The specified driver be filled as the single element of the
147      * Driver combo box of the New Database Connection dialog box.
148      * The database URL will be filled in the Database URL field in the
149      * New Database Connection dialog box.
150      * The user and password will be filled in the User Name and Password
151      * fields in the New Database Connection dialog box.
152      *
153      * @param driver the JDBC driver; can be null.
154      * @param databaseUrl the database URL; can be null.
155      * @param user the database user; can be null.
156      * @param password user's password; can be null.
157      *
158      * @since 1.19
159      */

160     public void showAddConnectionDialog(final JDBCDriver driver, final String JavaDoc databaseUrl, final String JavaDoc user, final String JavaDoc password) {
161         Mutex.EVENT.readAccess(new Runnable JavaDoc() {
162             public void run() {
163                 new ConnectUsingDriverAction.NewConnectionDialogDisplayer().showDialog(driver, databaseUrl, user, password);
164             }
165         });
166     }
167     
168     /**
169      * The counterpart of {@link #showAddConnectionDialog(JDBCDriver) } which returns
170      * the newly created database connection, but must be called from the event dispatching
171      * thread.
172      *
173      * @param driver the JDBC driver; can be null.
174      *
175      * @return the new database connection or null if no database connection
176      * was created (e.g. the user pressed Cancel).
177      *
178      * @throws IllegalStateException if the calling thread is not the event
179      * dispatching thread.
180      *
181      * @since 1.19
182      */

183     public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver) {
184         return showAddConnectionDialogFromEventThread(driver, null, null, null);
185     }
186     
187     /**
188      * The counterpart of {@link #showAddConnectionDialog(JDBCDriver, String) } which returns
189      * the newly created database connection, but must be called from the event dispatching
190      * thread.
191      *
192      * @param driver the JDBC driver; can be null.
193      * @param databaseUrl the database URL; can be null.
194      *
195      * @return the new database connection or null if no database connection
196      * was created (e.g. the user pressed Cancel).
197      *
198      * @throws IllegalStateException if the calling thread is not the event
199      * dispatching thread.
200      *
201      * @since 1.19
202      */

203     public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String JavaDoc databaseUrl) {
204         return showAddConnectionDialogFromEventThread(driver, databaseUrl, null, null);
205     }
206     
207     /**
208      * The counterpart of {@link #showAddConnectionDialog(JDBCDriver, String, String, String) }
209      * which returns the newly created database connection, but must be called
210      * from the event dispatching thread.
211      *
212      * @param driver the JDBC driver; can be null.
213      * @param databaseUrl the database URL; can be null.
214      * @param user the database user; can be null.
215      * @param password user's password; can be null.
216      *
217      * @return the new database connection or null if no database connection
218      * was created (e.g. the user pressed Cancel).
219      *
220      * @throws IllegalStateException if the calling thread is not the event
221      * dispatching thread.
222      *
223      * @since 1.19
224      */

225     public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String JavaDoc databaseUrl, String JavaDoc user, String JavaDoc password) {
226         if (!SwingUtilities.isEventDispatchThread()) {
227             throw new IllegalStateException JavaDoc("The current thread is not the event dispatching thread."); // NOI18N
228
}
229         org.netbeans.modules.db.explorer.DatabaseConnection internalDBConn = new ConnectUsingDriverAction.NewConnectionDialogDisplayer().showDialog(driver, databaseUrl, user, password);
230         if (internalDBConn != null) {
231             return internalDBConn.getDatabaseConnection();
232         }
233         return null;
234     }
235     
236     /**
237      * Shows the Connect dialog for the specified connection if not all data
238      * needed to connect, such as the user name or password,
239      * are known), or displays a modal progress dialog and attempts
240      * to connect to the database immediately.
241      *
242      * @param dbconn the database connection to be connected
243      *
244      * @throws NullPointerException if the dbconn parameter is null
245      * @throws IllegalStateException if this connection is not added to the
246      * ConnectionManager.
247      */

248     public void showConnectionDialog(DatabaseConnection dbconn) {
249         if (dbconn == null) {
250             throw new NullPointerException JavaDoc();
251         }
252         if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
253             throw new IllegalStateException JavaDoc("This connection is not added to the ConnectionManager."); // NOI18N
254
}
255         dbconn.getDelegate().showConnectionDialog();
256     }
257
258     /**
259      * Disconnects this connection from the database. Does not do anything
260      * if not connected.
261      *
262      * @param dbconn the database connection to be connected
263      *
264      * @throws NullPointerException if the dbconn parameter is null
265      * @throws IllegalStateException if this connection is not added to the
266      * ConnectionManager.
267      */

268     public void disconnect(DatabaseConnection dbconn) {
269         if (dbconn == null) {
270             throw new NullPointerException JavaDoc();
271         }
272         if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
273             throw new IllegalStateException JavaDoc("This connection is not added to the ConnectionManager."); // NOI18N
274
}
275         try {
276             dbconn.getDelegate().disconnect();
277         } catch (DatabaseException e) {
278             // XXX maybe shouldn't catch the exception
279
ErrorManager.getDefault().notify(e);
280         }
281     }
282     
283     /**
284      * Selects the node corresponding to the specified connection in the
285      * Runtime tab.
286      *
287      * @param dbconn the connection to select
288      *
289      * @throws NullPointerException if the dbconn parameter is null
290      * @throws IllegalStateException if this connection is not added to the
291      * ConnectionManager.
292      */

293     public void selectConnectionInExplorer(DatabaseConnection dbconn) {
294         if (dbconn == null) {
295             throw new NullPointerException JavaDoc();
296         }
297         if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
298             throw new IllegalStateException JavaDoc("This connection is not added to the ConnectionManager."); // NOI18N
299
}
300         dbconn.getDelegate().selectInExplorer();
301     }
302     
303     /**
304      * Registers a ConnectionListener.
305      */

306     public void addConnectionListener(ConnectionListener listener) {
307         ConnectionList.getDefault().addConnectionListener(listener);
308     }
309     
310     /**
311      * Unregisters the specified connection listener.
312      */

313     public void removeConnectionListener(ConnectionListener listener) {
314         ConnectionList.getDefault().removeConnectionListener(listener);
315     }
316 }
317
Popular Tags