KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > ConnectionDialogCommon


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.util;
33
34 import java.io.EOFException JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.ObjectInputStream JavaDoc;
40 import java.io.ObjectOutputStream JavaDoc;
41 import java.lang.reflect.Constructor JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Hashtable JavaDoc;
44
45 import org.hsqldb.lib.java.JavaSystem;
46
47 // sqlbob@users 20020407 - patch 1.7.0 - reengineering
48
// fredt@users - 20040508 - modified patch by lonbinder@users for saving settings
49
// weconsultants@users - 20041114 - patch 1.8.0 - Added MySQL Connector/J jbcDriver and granulated imports
50

51 /**
52  * Common code in the Swing and AWT versions of ConnectionDialog
53  *
54  * New class based on Hypersonic original
55  *
56  * @author Thomas Mueller (Hypersonic SQL Group)
57  * @version 1.7.2
58  * @since 1.7.0
59  */

60 class ConnectionDialogCommon {
61
62     private static String JavaDoc[][] connTypes;
63     private static final String JavaDoc[][] 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 JavaDoc[][] getTypes() {
121
122         return sJDBCTypes;
123 /*
124
125         if (connTypes == null) {
126
127
128             // Pluggable connection types:
129             Vector plugTypes = new Vector();
130
131             try {
132                 plugTypes = (Vector) Class.forName(
133                     System.getProperty(
134                         "org.hsqldb.util.ConnectionTypeClass")).newInstance();
135             } catch (Exception e) {
136                 ;
137             }
138
139             connTypes =
140                 new String[(plugTypes.size() / 3) + sJDBCTypes.length][3];
141
142             int i = 0;
143
144             for (int j = 0; j < plugTypes.size(); i++) {
145                 connTypes[i] = new String[3];
146                 connTypes[i][0] = plugTypes.elementAt(j++).toString();
147                 connTypes[i][1] = plugTypes.elementAt(j++).toString();
148                 connTypes[i][2] = plugTypes.elementAt(j++).toString();
149             }
150
151             for (int j = 0; j < sJDBCTypes.length; i++, j++) {
152                 connTypes[i] = new String[3];
153                 connTypes[i][0] = sJDBCTypes[j][0];
154                 connTypes[i][1] = sJDBCTypes[j][1];
155                 connTypes[i][2] = sJDBCTypes[j][2];
156             }
157         }
158
159         return (connTypes);
160  */

161     }
162
163     private static final String JavaDoc fileName = "hsqlprefs.dat";
164     private static File JavaDoc recentSettings = null;
165
166     static Hashtable JavaDoc loadRecentConnectionSettings() throws IOException JavaDoc {
167
168         Hashtable JavaDoc list = new Hashtable JavaDoc();
169
170         try {
171             if (recentSettings == null) {
172                 setHomeDir();
173
174                 if (homedir == null) {
175                     return list;
176                 }
177
178                 recentSettings = new File JavaDoc(homedir, fileName);
179
180                 if (!recentSettings.exists()) {
181                     JavaSystem.createNewFile(recentSettings);
182
183                     // Changed back to what I recived from you
184
// recentSettings.createNewFile();
185
return list;
186                 }
187             }
188         } catch (Throwable JavaDoc e) {
189             return list;
190         }
191
192         FileInputStream JavaDoc in = null;
193         ObjectInputStream JavaDoc objStream = null;
194
195         try {
196             in = new FileInputStream JavaDoc(recentSettings);
197             objStream = new ObjectInputStream JavaDoc(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 JavaDoc eof) {
210
211             // reached end of file -- this is not clean but it works
212
} catch (ClassNotFoundException JavaDoc cnfe) {
213             throw (IOException JavaDoc) new IOException JavaDoc("Unrecognized class type "
214                                                 + cnfe.getMessage());
215         } catch (ClassCastException JavaDoc cce) {
216             throw (IOException JavaDoc) new IOException JavaDoc("Unrecognized class type "
217                                                 + cce.getMessage());
218         } catch (Throwable JavaDoc 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 JavaDoc emptySettingName = "Recent settings...";
233
234     /**
235      * Adds the new settings name if it does not nexist, or overwrites the old one.
236      */

237     static void addToRecentConnectionSettings(Hashtable JavaDoc settings,
238             ConnectionSetting newSetting) throws IOException JavaDoc {
239         settings.put(newSetting.getName(), newSetting);
240         ConnectionDialogCommon.storeRecentConnectionSettings(settings);
241     }
242
243     /**
244      * Here's a non-secure method of storing recent connection settings.
245      *
246      * @param settings ConnectionSetting[]
247      * @throw IOException if something goes wrong while writing
248      */

249     private static void storeRecentConnectionSettings(Hashtable JavaDoc settings) {
250
251         try {
252             if (recentSettings == null) {
253                 setHomeDir();
254
255                 if (homedir == null) {
256                     return;
257                 }
258
259                 recentSettings = new File JavaDoc(homedir, fileName);
260
261                 if (!recentSettings.exists()) {
262
263 // recentSettings.createNewFile();
264
}
265             }
266
267             if (settings == null || settings.size() == 0) {
268                 return;
269             }
270
271             // setup a stream to a physical file on the filesystem
272
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(recentSettings);
273             ObjectOutputStream JavaDoc objStream = new ObjectOutputStream JavaDoc(out);
274             Enumeration JavaDoc 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 JavaDoc t) {}
284     }
285
286     /**
287      * Removes the recent connection settings file store.
288      */

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 JavaDoc(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 JavaDoc t) {}
312     }
313
314     private static String JavaDoc homedir = null;
315
316     public static void setHomeDir() {
317
318 //#ifdef JAVA1TARGET
319
/*
320 */

321
322 //#else
323
if (homedir == null) {
324             try {
325                 Class JavaDoc c =
326                     Class.forName("sun.security.action.GetPropertyAction");
327                 Constructor JavaDoc constructor = c.getConstructor(new Class JavaDoc[]{
328                     String JavaDoc.class });
329                 java.security.PrivilegedAction JavaDoc a =
330                     (java.security.PrivilegedAction JavaDoc) constructor.newInstance(
331                         new Object JavaDoc[]{ "user.home" });
332
333                 homedir =
334                     (String JavaDoc) java.security.AccessController.doPrivileged(a);
335             } catch (Exception JavaDoc e) {
336                 System.err.println(
337                     "No access to home directory. Continuing without...");
338             }
339         }
340
341 //#endif
342
}
343 }
344
Popular Tags