001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.io.EOFException;
034: import java.io.File;
035: import java.io.FileInputStream;
036: import java.io.FileOutputStream;
037: import java.io.IOException;
038: import java.io.ObjectInputStream;
039: import java.io.ObjectOutputStream;
040: import java.lang.reflect.Constructor;
041: import java.util.Enumeration;
042: import java.util.Hashtable;
043:
044: import org.hsqldb.lib.java.JavaSystem;
045:
046: // sqlbob@users 20020407 - patch 1.7.0 - reengineering
047: // fredt@users - 20040508 - modified patch by lonbinder@users for saving settings
048: // weconsultants@users - 20041114 - patch 1.8.0 - Added MySQL Connector/J jbcDriver and granulated imports
049:
050: /**
051: * Common code in the Swing and AWT versions of ConnectionDialog
052: *
053: * New class based on Hypersonic original
054: *
055: * @author Thomas Mueller (Hypersonic SQL Group)
056: * @version 1.7.2
057: * @since 1.7.0
058: */
059: class ConnectionDialogCommon {
060:
061: private static String[][] connTypes;
062: private static final String[][] sJDBCTypes = {
063: { "HSQL Database Engine In-Memory",
064: "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:." },
065: { "HSQL Database Engine Standalone",
066: "org.hsqldb.jdbcDriver",
067: "jdbc:hsqldb:file:\u00ABdatabase/path?\u00BB" },
068: { "HSQL Database Engine Server", "org.hsqldb.jdbcDriver",
069: "jdbc:hsqldb:hsql://localhost/" },
070: { "HSQL Database Engine WebServer",
071: "org.hsqldb.jdbcDriver",
072: "jdbc:hsqldb:http://\u00ABhostname/?\u00BB" },
073: { "JDBC-ODBC Bridge from Sun",
074: "sun.jdbc.odbc.JdbcOdbcDriver",
075: "jdbc:odbc:\u00ABdatabase?\u00BB" },
076: {
077: "Cloudscape RMI",
078: "RmiJdbc.RJDriver",
079: "jdbc:rmi://\u00ABhost?\u00BB:1099/jdbc:cloudscape:"
080: + "\u00ABdatabase?\u00BB;create=true" },
081: { "IBM DB2", "COM.ibm.db2.jdbc.app.DB2Driver",
082: "jdbc:db2:\u00ABdatabase?\u00BB" },
083: { "IBM DB2 (thin)", "COM.ibm.db2.jdbc.net.DB2Driver",
084: "jdbc:db2://\u00ABhost?\u00BB:6789/\u00ABdatabase?\u00BB" },
085: {
086: "Informix",
087: "com.informix.jdbc.IfxDriver",
088: "jdbc:informix-sqli://\u00ABhost?\u00BB:1533/\u00ABdatabase?\u00BB:"
089: + "INFORMIXSERVER=\u00ABserver?\u00BB" },
090: { "InstantDb", "jdbc.idbDriver",
091: "jdbc:idb:\u00ABdatabase?\u00BB.prp" },
092: { "MySQL Connector/J", "com.mysql.jdbc.Driver",
093: "jdbc:mysql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" },
094: { "MM.MySQL", "org.gjt.mm.mysql.Driver",
095: "jdbc:mysql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" },
096: { "Oracle", "oracle.jdbc.driver.OracleDriver",
097: "jdbc:oracle:oci8:@\u00ABdatabase?\u00BB" },
098: { "Oracle (thin)", "oracle.jdbc.driver.OracleDriver",
099: "jdbc:oracle:thin:@\u00ABhost?\u00BB:1521:\u00ABdatabase?\u00BB" },
100: { "PointBase", "com.pointbase.jdbc.jdbcUniversalDriver",
101: "jdbc:pointbase://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" },
102: { "PostgreSQL", "org.postgresql.Driver",
103: "jdbc:postgresql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" },
104: { "PostgreSQL v6.5", "postgresql.Driver",
105: "jdbc:postgresql://\u00ABhost?\u00BB/\u00ABdatabase?\u00BB" } };
106:
107: static String[][] getTypes() {
108:
109: return sJDBCTypes;
110: /*
111:
112: if (connTypes == null) {
113:
114:
115: // Pluggable connection types:
116: Vector plugTypes = new Vector();
117:
118: try {
119: plugTypes = (Vector) Class.forName(
120: System.getProperty(
121: "org.hsqldb.util.ConnectionTypeClass")).newInstance();
122: } catch (Exception e) {
123: ;
124: }
125:
126: connTypes =
127: new String[(plugTypes.size() / 3) + sJDBCTypes.length][3];
128:
129: int i = 0;
130:
131: for (int j = 0; j < plugTypes.size(); i++) {
132: connTypes[i] = new String[3];
133: connTypes[i][0] = plugTypes.elementAt(j++).toString();
134: connTypes[i][1] = plugTypes.elementAt(j++).toString();
135: connTypes[i][2] = plugTypes.elementAt(j++).toString();
136: }
137:
138: for (int j = 0; j < sJDBCTypes.length; i++, j++) {
139: connTypes[i] = new String[3];
140: connTypes[i][0] = sJDBCTypes[j][0];
141: connTypes[i][1] = sJDBCTypes[j][1];
142: connTypes[i][2] = sJDBCTypes[j][2];
143: }
144: }
145:
146: return (connTypes);
147: */
148: }
149:
150: private static final String fileName = "hsqlprefs.dat";
151: private static File recentSettings = null;
152:
153: static Hashtable loadRecentConnectionSettings() throws IOException {
154:
155: Hashtable list = new Hashtable();
156:
157: try {
158: if (recentSettings == null) {
159: setHomeDir();
160:
161: if (homedir == null) {
162: return list;
163: }
164:
165: recentSettings = new File(homedir, fileName);
166:
167: if (!recentSettings.exists()) {
168: JavaSystem.createNewFile(recentSettings);
169:
170: // Changed back to what I recived from you
171: // recentSettings.createNewFile();
172: return list;
173: }
174: }
175: } catch (Throwable e) {
176: return list;
177: }
178:
179: FileInputStream in = null;
180: ObjectInputStream objStream = null;
181:
182: try {
183: in = new FileInputStream(recentSettings);
184: objStream = new ObjectInputStream(in);
185:
186: list.clear();
187:
188: while (true) {
189: ConnectionSetting setting = (ConnectionSetting) objStream
190: .readObject();
191:
192: if (!emptySettingName.equals(setting.getName())) {
193: list.put(setting.getName(), setting);
194: }
195: }
196: } catch (EOFException eof) {
197:
198: // reached end of file -- this is not clean but it works
199: } catch (ClassNotFoundException cnfe) {
200: throw (IOException) new IOException(
201: "Unrecognized class type " + cnfe.getMessage());
202: } catch (ClassCastException cce) {
203: throw (IOException) new IOException(
204: "Unrecognized class type " + cce.getMessage());
205: } catch (Throwable t) {
206: } finally {
207: if (objStream != null) {
208: objStream.close();
209: }
210:
211: if (in != null) {
212: in.close();
213: }
214: }
215:
216: return list;
217: }
218:
219: static String emptySettingName = "Recent settings...";
220:
221: /**
222: * Adds the new settings name if it does not nexist, or overwrites the old one.
223: */
224: static void addToRecentConnectionSettings(Hashtable settings,
225: ConnectionSetting newSetting) throws IOException {
226: settings.put(newSetting.getName(), newSetting);
227: ConnectionDialogCommon.storeRecentConnectionSettings(settings);
228: }
229:
230: /**
231: * Here's a non-secure method of storing recent connection settings.
232: *
233: * @param settings ConnectionSetting[]
234: * @throw IOException if something goes wrong while writing
235: */
236: private static void storeRecentConnectionSettings(Hashtable settings) {
237:
238: try {
239: if (recentSettings == null) {
240: setHomeDir();
241:
242: if (homedir == null) {
243: return;
244: }
245:
246: recentSettings = new File(homedir, fileName);
247:
248: if (!recentSettings.exists()) {
249:
250: // recentSettings.createNewFile();
251: }
252: }
253:
254: if (settings == null || settings.size() == 0) {
255: return;
256: }
257:
258: // setup a stream to a physical file on the filesystem
259: FileOutputStream out = new FileOutputStream(recentSettings);
260: ObjectOutputStream objStream = new ObjectOutputStream(out);
261: Enumeration en = settings.elements();
262:
263: while (en.hasMoreElements()) {
264: objStream.writeObject(en.nextElement());
265: }
266:
267: objStream.flush();
268: objStream.close();
269: out.close();
270: } catch (Throwable t) {
271: }
272: }
273:
274: /**
275: * Removes the recent connection settings file store.
276: */
277: static void deleteRecentConnectionSettings() {
278:
279: try {
280: if (recentSettings == null) {
281: setHomeDir();
282:
283: if (homedir == null) {
284: return;
285: }
286:
287: recentSettings = new File(homedir, fileName);
288: }
289:
290: if (!recentSettings.exists()) {
291: recentSettings = null;
292:
293: return;
294: }
295:
296: recentSettings.delete();
297:
298: recentSettings = null;
299: } catch (Throwable t) {
300: }
301: }
302:
303: private static String homedir = null;
304:
305: public static void setHomeDir() {
306:
307: //#ifdef JAVA1TARGET
308: /*
309: */
310:
311: //#else
312: if (homedir == null) {
313: try {
314: Class c = Class
315: .forName("sun.security.action.GetPropertyAction");
316: Constructor constructor = c
317: .getConstructor(new Class[] { String.class });
318: java.security.PrivilegedAction a = (java.security.PrivilegedAction) constructor
319: .newInstance(new Object[] { "user.home" });
320:
321: homedir = (String) java.security.AccessController
322: .doPrivileged(a);
323: } catch (Exception e) {
324: System.err
325: .println("No access to home directory. Continuing without...");
326: }
327: }
328:
329: //#endif
330: }
331: }
|