1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.IOException ; 25 import java.io.ObjectInput ; 26 import java.io.ObjectOutput ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.Vector ; 33 import java.util.jar.JarFile ; 34 import org.netbeans.api.db.explorer.DatabaseException; 35 import org.netbeans.api.db.explorer.JDBCDriver; 36 import org.netbeans.api.db.explorer.JDBCDriverManager; 37 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; 38 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 39 import org.netbeans.modules.db.util.DriverListUtil; 40 import org.openide.ErrorManager; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.Repository; 43 import org.openide.options.SystemOption; 44 import org.openide.util.NbBundle; 45 46 50 public class DatabaseOption extends SystemOption { 51 52 private static boolean debugMode; 53 private static Vector drivers; 54 private static Vector connections; 55 private static int fetchlimit = 100; 56 private static int fetchstep = 200; 57 private static boolean autoConn = true; 58 59 public static final String PROP_DEBUG_MODE = "debugMode"; 61 static final long serialVersionUID =-13629330831657810L; 62 63 public DatabaseOption() { 64 super(); 65 drivers = new Vector (); 66 connections = new Vector (); 67 debugMode = false; 68 69 deleteAdaptorsFolder(); 70 } 71 72 73 public Vector getAvailableDrivers() { 74 if (drivers.size() == 0) { 75 Map xxx = (Map ) DatabaseNodeInfo.getGlobalNodeInfo(DatabaseNode.DRIVER_LIST); 77 drivers = createDrivers(xxx); 78 } 79 80 return drivers; 81 } 82 83 public boolean getDebugMode() { 84 return debugMode; 85 } 86 87 public void setDebugMode(boolean flag) { 88 if (debugMode == flag) 89 return; 90 91 debugMode = flag; 92 firePropertyChange(PROP_DEBUG_MODE, !debugMode ? Boolean.TRUE : Boolean.FALSE, debugMode ? Boolean.TRUE : Boolean.FALSE); 93 } 94 95 98 public void setAvailableDrivers(Vector c) { 99 drivers = c; 100 } 101 102 107 Vector getConnections() { 108 if (connections == null) 109 connections = new Vector (); 110 111 return connections; 112 } 113 114 public void save() { 115 firePropertyChange(null, null, null); 116 } 117 118 119 public String displayName() { 120 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("OptionName"); } 122 123 124 public String toString() { 125 return (drivers != null ? drivers.size() : 0) + " drivers, " + (connections != null ? connections.size() : 0) + " connections"; } 127 128 131 public void writeExternal(ObjectOutput out) throws IOException { 132 super.writeExternal(out); 133 134 out.writeObject(null); 135 out.writeObject(getConnections()); 136 } 137 138 141 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 142 super.readExternal(in); 143 144 drivers = (Vector ) in.readObject(); 145 if (drivers != null) 146 lookForDrivers(); 147 148 connections = (Vector ) in.readObject(); 149 } 150 151 private Vector createDrivers(Map drvMap) { 152 Vector def = (Vector ) drvMap.get("defaultdriverlist"); Vector rvec = null; 154 if (def != null && def.size() > 0) { 155 rvec = new Vector (def.size()); 156 Enumeration defe = def.elements(); 157 while (defe.hasMoreElements()) { 158 Object rit = defe.nextElement(); 159 String name = (String ) ((Map )rit).get("name"); String drv = (String ) ((Map )rit).get("driver"); String prefix = (String ) ((Map )rit).get("prefix"); String adaptor = (String ) ((Map )rit).get("adaptor"); rit = new DatabaseDriver(name, drv, prefix, adaptor); 164 if (rit != null) 165 rvec.add(rit); 166 } 167 } else 168 rvec = new Vector (); 169 170 return rvec; 171 } 172 173 private void lookForDrivers() { 174 StringBuffer sb = new StringBuffer (); 175 sb.append(File.separator); 176 sb.append("lib"); 177 sb.append(File.separator); 178 sb.append("ext"); 179 String libext = sb.toString(); 180 String nbhome = System.getProperty("netbeans.home"); 181 182 preinstallDrivers(nbhome + libext); 183 } 184 185 private void preinstallDrivers(String dirPath) { 186 File dir = new File (dirPath); 187 if (!dir.isDirectory()) 188 return; 189 190 File [] files = dir.listFiles(new FileFilter () { 191 public boolean accept(File f) { 192 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); } 194 }); 195 196 for (int i = 0; i < files.length; i++) { 197 JarFile jf; 198 String drv; 199 200 try { 201 jf = new JarFile (files[i]); 202 Set drvs = DriverListUtil.getDrivers(); 203 Iterator it = drvs.iterator(); 204 while (it.hasNext()) { 205 drv = (String ) it.next(); 206 if (jf.getEntry(drv.replace('.', '/') + ".class") != null) { String driverName = DriverListUtil.findFreeName(DriverListUtil.getName(drv)); 208 JDBCDriver driver = JDBCDriver.create(driverName, driverName, drv, new URL [] {files[i].toURI().toURL()}); 209 try { 210 JDBCDriverManager.getDefault().addDriver(driver); 211 } catch (DatabaseException e) { 212 ErrorManager.getDefault().notify(e); 213 } 214 } 215 } 216 jf.close(); 217 } catch (IOException exc) { 218 } 220 } 221 } 222 223 private void deleteAdaptorsFolder() { 224 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Database"); try { 226 if (fo != null) 227 fo.delete(); 228 } catch (IOException exc) { 229 } 231 } 232 } 233 | Popular Tags |