KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import org.netbeans.modules.db.explorer.actions.AddDriverAction;
30 import org.netbeans.modules.db.explorer.driver.JDBCDriverConvertor;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.DataFolder;
34 import org.openide.loaders.FolderLookup;
35 import org.openide.util.Lookup;
36 import org.openide.util.LookupEvent;
37 import org.openide.util.LookupListener;
38
39 /**
40  * This class manages the list of JDBC drivers registered in the Database Explorer.
41  */

42 public final class JDBCDriverManager {
43     
44     /**
45      * The JDBCDriverManager singleton instance.
46      */

47     private static JDBCDriverManager DEFAULT = null;
48     
49     private Lookup.Result result = getLookupResult();
50     
51     /**
52      * The list of listeners.
53      */

54     private List JavaDoc/*<JDBCDriverListener>*/ listeners = new ArrayList JavaDoc(1);
55     
56     /**
57      *
58      * Gets the JDBCDriverManager singleton instance.
59      *
60      * @return the JDBCDriverManager singleton instance.
61      */

62     public static synchronized JDBCDriverManager getDefault() {
63         if (DEFAULT == null) {
64             JDBCDriverConvertor.importOldDrivers();
65             DEFAULT = new JDBCDriverManager();
66         }
67         return DEFAULT;
68     }
69
70     /**
71      * Private constructor.
72      */

73     private JDBCDriverManager() {
74         // issue 75204: forces the DataObject's corresponding to the JDBCDriver's
75
// to be initialized and held strongly so the same JDBCDriver is
76
// returns as long as it is held strongly
77
result.allInstances();
78
79         result.addLookupListener(new LookupListener() {
80             public void resultChanged(LookupEvent e) {
81                 fireListeners();
82             }
83         });
84     }
85     
86     /**
87      * Gets the registered JDBC drivers.
88      *
89      * @return a non-null array of JDBCDriver instances.
90      */

91     public JDBCDriver[] getDrivers() {
92         Collection JavaDoc drivers = result.allInstances();
93         return (JDBCDriver[])drivers.toArray(new JDBCDriver[drivers.size()]);
94     }
95     
96     /**
97      * Gets the registered JDBC drivers with the specified class name.
98      *
99      * @param drvClass driver class name; must not be null.
100      *
101      * @return a non-null array of JDBCDriver instances with the specified class name.
102      *
103      * @throws NullPointerException if the specified class name is null.
104      */

105     public JDBCDriver[] getDrivers(String JavaDoc drvClass) {
106         if (drvClass == null) {
107             throw new NullPointerException JavaDoc();
108         }
109         LinkedList JavaDoc result = new LinkedList JavaDoc();
110         JDBCDriver[] drvs = getDrivers();
111         for (int i = 0; i < drvs.length; i++) {
112             if (drvClass.equals(drvs[i].getClassName())) {
113                 result.add(drvs[i]);
114             }
115         }
116         return (JDBCDriver[])result.toArray(new JDBCDriver[result.size()]);
117     }
118
119     /**
120      * Adds a new JDBC driver.
121      *
122      * @param driver the JDBCDriver instance describing the driver to be added;
123      * must not be null.
124      *
125      * @throws NullPointerException if the specified driver is null.
126      * DatabaseException if an error occurred while adding the driver.
127      */

128     public void addDriver(JDBCDriver driver) throws DatabaseException {
129         if (driver == null) {
130             throw new NullPointerException JavaDoc();
131         }
132         try {
133             JDBCDriverConvertor.create(driver);
134         } catch (IOException JavaDoc ioe) {
135             throw new DatabaseException(ioe);
136         }
137     }
138     
139     /**
140      * Removes a JDBC driver.
141      *
142      * @param driver the JDBCDriver instance to be removed.
143      *
144      * @throws DatabaseException if an error occurred while adding the driver.
145      */

146     public void removeDriver(JDBCDriver driver) throws DatabaseException {
147         try {
148             JDBCDriverConvertor.remove(driver);
149         } catch (IOException JavaDoc ioe) {
150             throw new DatabaseException(ioe);
151         }
152     }
153     
154     /**
155      * Shows the Add Driver dialog, allowing the user to add a new JDBC driver.
156      */

157     public void showAddDriverDialog() {
158         if (!SwingUtilities.isEventDispatchThread()) {
159             SwingUtilities.invokeLater(new Runnable JavaDoc() {
160                 public void run() {
161                     new AddDriverAction.AddDriverDialogDisplayer().showDialog();
162                 }
163             });
164         } else {
165             new AddDriverAction.AddDriverDialogDisplayer().showDialog();
166         }
167     }
168     
169     /**
170      * Registers a JDBCDriverListener.
171      */

172     public void addDriverListener(JDBCDriverListener listener) {
173         synchronized (listeners) {
174             listeners.add(listener);
175         }
176     }
177     
178     /**
179      * Unregisters the specified JDBCDriverListener.
180      */

181     public void removeDriverListener(JDBCDriverListener listener) {
182         synchronized (listeners) {
183             listeners.remove(listener);
184         }
185     }
186     
187     private void fireListeners() {
188         List JavaDoc/*<JDBCDriverListener>*/ listenersCopy;
189         
190         synchronized (listeners) {
191             listenersCopy = new ArrayList JavaDoc(listeners);
192         }
193         
194         for (Iterator JavaDoc i= listeners.iterator(); i.hasNext();) {
195             JDBCDriverListener listener = (JDBCDriverListener)i.next();
196             listener.driversChanged();
197         }
198     }
199     
200     private synchronized Lookup.Result getLookupResult() {
201         if (result == null) {
202             FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(JDBCDriverConvertor.DRIVERS_PATH);
203             DataFolder folder = DataFolder.findFolder(fo);
204             result = new FolderLookup(folder).getLookup().lookup(new Lookup.Template(JDBCDriver.class));
205         }
206         return result;
207     }
208 }
209
Popular Tags