Source Code Cross Referenced for ConnectionManager.java in  » IDE-Netbeans » db » org » netbeans » api » db » explorer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » db » org.netbeans.api.db.explorer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.