1 30 31 32 package org.hsqldb.util; 33 34 import java.io.EOFException ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.IOException ; 39 import java.io.ObjectInputStream ; 40 import java.io.ObjectOutputStream ; 41 import java.lang.reflect.Constructor ; 42 import java.util.Enumeration ; 43 import java.util.Hashtable ; 44 45 import org.hsqldb.lib.java.JavaSystem; 46 47 51 60 class ConnectionDialogCommon { 61 62 private static String [][] connTypes; 63 private static final String [][] sJDBCTypes = { 64 { 65 "HSQL Database Engine In-Memory", "org.hsqldb.jdbcDriver", 66 "jdbc:hsqldb:mem:." 67 }, { 68 "HSQL Database Engine Standalone", "org.hsqldb.jdbcDriver", 69 "jdbc:hsqldb:file:\u00ABdatabase/path?\u00BB" 70 }, { 71 "HSQL Database Engine Server", "org.hsqldb.jdbcDriver", 72 "jdbc:hsqldb:hsql://localhost/" 73 }, { 74 "HSQL Database Engine WebServer", "org.hsqldb.jdbcDriver", 75 "jdbc:hsqldb:http://\u00ABhostname/?\u00BB" 76 }, { 77 "JDBC-ODBC Bridge from Sun", "sun.jdbc.odbc.JdbcOdbcDriver", 78 "jdbc:odbc:\u00ABdatabase?\u00BB" 79 }, { 80 "Cloudscape RMI", "RmiJdbc.RJDriver", 81 "jdbc:rmi://\u00ABhost?\u00BB:1099/jdbc:cloudscape:" 82 + "\u00ABdatabase?\u00BB;create=true" 83 }, { 84 "IBM DB2", "COM.ibm.db2.jdbc.app.DB2Driver", 85 "jdbc:db2:\u00ABdatabase?\u00BB" 86 }, { 87 "IBM DB2 (thin)", "COM.ibm.db2.jdbc.net.DB2Driver", 88 "jdbc:db2://\u00ABhost?\u00BB:6789/\u00ABdatabase?\u00BB" 89 }, { 90 "Informix", "com.informix.jdbc.IfxDriver", 91 "jdbc:informix-sqli://\u00ABhost?\u00BB:1533/\u00ABdatabase?\u00BB:" 92 + "INFORMIXSERVER=\u00ABserver?\u00BB" 93 }, { 94 "InstantDb", "jdbc.idbDriver", 95 "jdbc:idb:\u00ABdatabase?\u00BB.prp" 96 }, { 97 "MySQL Connector/J", "com.mysql.jdbc.Driver", 98 "jdbc:mysql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" 99 }, { 100 "MM.MySQL", "org.gjt.mm.mysql.Driver", 101 "jdbc:mysql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" 102 }, { 103 "Oracle", "oracle.jdbc.driver.OracleDriver", 104 "jdbc:oracle:oci8:@\u00ABdatabase?\u00BB" 105 }, { 106 "Oracle (thin)", "oracle.jdbc.driver.OracleDriver", 107 "jdbc:oracle:thin:@\u00ABhost?\u00BB:1521:\u00ABdatabase?\u00BB" 108 }, { 109 "PointBase", "com.pointbase.jdbc.jdbcUniversalDriver", 110 "jdbc:pointbase://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" 111 }, { 112 "PostgreSQL", "org.postgresql.Driver", 113 "jdbc:postgresql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" 114 }, { 115 "PostgreSQL v6.5", "postgresql.Driver", 116 "jdbc:postgresql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" 117 } 118 }; 119 120 static String [][] getTypes() { 121 122 return sJDBCTypes; 123 161 } 162 163 private static final String fileName = "hsqlprefs.dat"; 164 private static File recentSettings = null; 165 166 static Hashtable loadRecentConnectionSettings() throws IOException { 167 168 Hashtable list = new Hashtable (); 169 170 try { 171 if (recentSettings == null) { 172 setHomeDir(); 173 174 if (homedir == null) { 175 return list; 176 } 177 178 recentSettings = new File (homedir, fileName); 179 180 if (!recentSettings.exists()) { 181 JavaSystem.createNewFile(recentSettings); 182 183 return list; 186 } 187 } 188 } catch (Throwable e) { 189 return list; 190 } 191 192 FileInputStream in = null; 193 ObjectInputStream objStream = null; 194 195 try { 196 in = new FileInputStream (recentSettings); 197 objStream = new ObjectInputStream (in); 198 199 list.clear(); 200 201 while (true) { 202 ConnectionSetting setting = 203 (ConnectionSetting) objStream.readObject(); 204 205 if (!emptySettingName.equals(setting.getName())) { 206 list.put(setting.getName(), setting); 207 } 208 } 209 } catch (EOFException eof) { 210 211 } catch (ClassNotFoundException cnfe) { 213 throw (IOException ) new IOException ("Unrecognized class type " 214 + cnfe.getMessage()); 215 } catch (ClassCastException cce) { 216 throw (IOException ) new IOException ("Unrecognized class type " 217 + cce.getMessage()); 218 } catch (Throwable t) {} 219 finally { 220 if (objStream != null) { 221 objStream.close(); 222 } 223 224 if (in != null) { 225 in.close(); 226 } 227 } 228 229 return list; 230 } 231 232 static String emptySettingName = "Recent settings..."; 233 234 237 static void addToRecentConnectionSettings(Hashtable settings, 238 ConnectionSetting newSetting) throws IOException { 239 settings.put(newSetting.getName(), newSetting); 240 ConnectionDialogCommon.storeRecentConnectionSettings(settings); 241 } 242 243 249 private static void storeRecentConnectionSettings(Hashtable settings) { 250 251 try { 252 if (recentSettings == null) { 253 setHomeDir(); 254 255 if (homedir == null) { 256 return; 257 } 258 259 recentSettings = new File (homedir, fileName); 260 261 if (!recentSettings.exists()) { 262 263 } 265 } 266 267 if (settings == null || settings.size() == 0) { 268 return; 269 } 270 271 FileOutputStream out = new FileOutputStream (recentSettings); 273 ObjectOutputStream objStream = new ObjectOutputStream (out); 274 Enumeration en = settings.elements(); 275 276 while (en.hasMoreElements()) { 277 objStream.writeObject(en.nextElement()); 278 } 279 280 objStream.flush(); 281 objStream.close(); 282 out.close(); 283 } catch (Throwable t) {} 284 } 285 286 289 static void deleteRecentConnectionSettings() { 290 291 try { 292 if (recentSettings == null) { 293 setHomeDir(); 294 295 if (homedir == null) { 296 return; 297 } 298 299 recentSettings = new File (homedir, fileName); 300 } 301 302 if (!recentSettings.exists()) { 303 recentSettings = null; 304 305 return; 306 } 307 308 recentSettings.delete(); 309 310 recentSettings = null; 311 } catch (Throwable t) {} 312 } 313 314 private static String homedir = null; 315 316 public static void setHomeDir() { 317 318 321 322 if (homedir == null) { 324 try { 325 Class c = 326 Class.forName("sun.security.action.GetPropertyAction"); 327 Constructor constructor = c.getConstructor(new Class []{ 328 String .class }); 329 java.security.PrivilegedAction a = 330 (java.security.PrivilegedAction ) constructor.newInstance( 331 new Object []{ "user.home" }); 332 333 homedir = 334 (String ) java.security.AccessController.doPrivileged(a); 335 } catch (Exception e) { 336 System.err.println( 337 "No access to home directory. Continuing without..."); 338 } 339 } 340 341 } 343 } 344 | Popular Tags |