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 java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Iterator;
048: import java.util.LinkedList;
049: import java.util.List;
050: import javax.swing.SwingUtilities;
051: import org.netbeans.modules.db.explorer.actions.AddDriverAction;
052: import org.netbeans.modules.db.explorer.driver.JDBCDriverConvertor;
053: import org.openide.util.Lookup;
054: import org.openide.util.LookupEvent;
055: import org.openide.util.LookupListener;
056: import org.openide.util.lookup.Lookups;
057:
058: /**
059: * This class manages the list of JDBC drivers registered in the Database Explorer.
060: */
061: public final class JDBCDriverManager {
062:
063: /**
064: * The JDBCDriverManager singleton instance.
065: */
066: private static JDBCDriverManager DEFAULT = null;
067:
068: private Lookup.Result result = getLookupResult();
069:
070: /**
071: * The list of listeners.
072: */
073: private List/*<JDBCDriverListener>*/listeners = new ArrayList(1);
074:
075: /**
076: *
077: * Gets the JDBCDriverManager singleton instance.
078: *
079: * @return the JDBCDriverManager singleton instance.
080: */
081: public static synchronized JDBCDriverManager getDefault() {
082: if (DEFAULT == null) {
083: JDBCDriverConvertor.importOldDrivers();
084: DEFAULT = new JDBCDriverManager();
085: }
086: return DEFAULT;
087: }
088:
089: /**
090: * Private constructor.
091: */
092: private JDBCDriverManager() {
093: // issue 75204: forces the DataObject's corresponding to the JDBCDriver's
094: // to be initialized and held strongly so the same JDBCDriver is
095: // returns as long as it is held strongly
096: result.allInstances();
097:
098: result.addLookupListener(new LookupListener() {
099: public void resultChanged(LookupEvent e) {
100: fireListeners();
101: }
102: });
103: }
104:
105: /**
106: * Gets the registered JDBC drivers.
107: *
108: * @return a non-null array of JDBCDriver instances.
109: */
110: public JDBCDriver[] getDrivers() {
111: Collection drivers = result.allInstances();
112: return (JDBCDriver[]) drivers.toArray(new JDBCDriver[drivers
113: .size()]);
114: }
115:
116: /**
117: * Gets the registered JDBC drivers with the specified class name.
118: *
119: * @param drvClass driver class name; must not be null.
120: *
121: * @return a non-null array of JDBCDriver instances with the specified class name.
122: *
123: * @throws NullPointerException if the specified class name is null.
124: */
125: public JDBCDriver[] getDrivers(String drvClass) {
126: if (drvClass == null) {
127: throw new NullPointerException();
128: }
129: LinkedList result = new LinkedList();
130: JDBCDriver[] drvs = getDrivers();
131: for (int i = 0; i < drvs.length; i++) {
132: if (drvClass.equals(drvs[i].getClassName())) {
133: result.add(drvs[i]);
134: }
135: }
136: return (JDBCDriver[]) result.toArray(new JDBCDriver[result
137: .size()]);
138: }
139:
140: /**
141: * Adds a new JDBC driver.
142: *
143: * @param driver the JDBCDriver instance describing the driver to be added;
144: * must not be null.
145: *
146: * @throws NullPointerException if the specified driver is null.
147: * DatabaseException if an error occurred while adding the driver.
148: */
149: public void addDriver(JDBCDriver driver) throws DatabaseException {
150: if (driver == null) {
151: throw new NullPointerException();
152: }
153: try {
154: JDBCDriverConvertor.create(driver);
155: } catch (IOException ioe) {
156: throw new DatabaseException(ioe);
157: }
158: }
159:
160: /**
161: * Removes a JDBC driver.
162: *
163: * @param driver the JDBCDriver instance to be removed.
164: *
165: * @throws DatabaseException if an error occurred while adding the driver.
166: */
167: public void removeDriver(JDBCDriver driver)
168: throws DatabaseException {
169: try {
170: JDBCDriverConvertor.remove(driver);
171: } catch (IOException ioe) {
172: throw new DatabaseException(ioe);
173: }
174: }
175:
176: /**
177: * Shows the Add Driver dialog, allowing the user to add a new JDBC driver.
178: */
179: public void showAddDriverDialog() {
180: if (!SwingUtilities.isEventDispatchThread()) {
181: SwingUtilities.invokeLater(new Runnable() {
182: public void run() {
183: new AddDriverAction.AddDriverDialogDisplayer()
184: .showDialog();
185: }
186: });
187: } else {
188: new AddDriverAction.AddDriverDialogDisplayer().showDialog();
189: }
190: }
191:
192: /**
193: * Registers a JDBCDriverListener.
194: */
195: public void addDriverListener(JDBCDriverListener listener) {
196: synchronized (listeners) {
197: listeners.add(listener);
198: }
199: }
200:
201: /**
202: * Unregisters the specified JDBCDriverListener.
203: */
204: public void removeDriverListener(JDBCDriverListener listener) {
205: synchronized (listeners) {
206: listeners.remove(listener);
207: }
208: }
209:
210: private void fireListeners() {
211: List/*<JDBCDriverListener>*/listenersCopy;
212:
213: synchronized (listeners) {
214: listenersCopy = new ArrayList(listeners);
215: }
216:
217: for (Iterator i = listenersCopy.iterator(); i.hasNext();) {
218: JDBCDriverListener listener = (JDBCDriverListener) i.next();
219: listener.driversChanged();
220: }
221: }
222:
223: private synchronized Lookup.Result getLookupResult() {
224: return Lookups.forPath(JDBCDriverConvertor.DRIVERS_PATH)
225: .lookupResult(JDBCDriver.class);
226: }
227: }
|