1 19 20 package org.netbeans.api.db.explorer; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import javax.swing.SwingUtilities ; 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 42 public final class JDBCDriverManager { 43 44 47 private static JDBCDriverManager DEFAULT = null; 48 49 private Lookup.Result result = getLookupResult(); 50 51 54 private List listeners = new ArrayList (1); 55 56 62 public static synchronized JDBCDriverManager getDefault() { 63 if (DEFAULT == null) { 64 JDBCDriverConvertor.importOldDrivers(); 65 DEFAULT = new JDBCDriverManager(); 66 } 67 return DEFAULT; 68 } 69 70 73 private JDBCDriverManager() { 74 result.allInstances(); 78 79 result.addLookupListener(new LookupListener() { 80 public void resultChanged(LookupEvent e) { 81 fireListeners(); 82 } 83 }); 84 } 85 86 91 public JDBCDriver[] getDrivers() { 92 Collection drivers = result.allInstances(); 93 return (JDBCDriver[])drivers.toArray(new JDBCDriver[drivers.size()]); 94 } 95 96 105 public JDBCDriver[] getDrivers(String drvClass) { 106 if (drvClass == null) { 107 throw new NullPointerException (); 108 } 109 LinkedList result = new LinkedList (); 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 128 public void addDriver(JDBCDriver driver) throws DatabaseException { 129 if (driver == null) { 130 throw new NullPointerException (); 131 } 132 try { 133 JDBCDriverConvertor.create(driver); 134 } catch (IOException ioe) { 135 throw new DatabaseException(ioe); 136 } 137 } 138 139 146 public void removeDriver(JDBCDriver driver) throws DatabaseException { 147 try { 148 JDBCDriverConvertor.remove(driver); 149 } catch (IOException ioe) { 150 throw new DatabaseException(ioe); 151 } 152 } 153 154 157 public void showAddDriverDialog() { 158 if (!SwingUtilities.isEventDispatchThread()) { 159 SwingUtilities.invokeLater(new Runnable () { 160 public void run() { 161 new AddDriverAction.AddDriverDialogDisplayer().showDialog(); 162 } 163 }); 164 } else { 165 new AddDriverAction.AddDriverDialogDisplayer().showDialog(); 166 } 167 } 168 169 172 public void addDriverListener(JDBCDriverListener listener) { 173 synchronized (listeners) { 174 listeners.add(listener); 175 } 176 } 177 178 181 public void removeDriverListener(JDBCDriverListener listener) { 182 synchronized (listeners) { 183 listeners.remove(listener); 184 } 185 } 186 187 private void fireListeners() { 188 List listenersCopy; 189 190 synchronized (listeners) { 191 listenersCopy = new ArrayList (listeners); 192 } 193 194 for (Iterator 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 |