KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileReader JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.DriverManager JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.Properties JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 /* $Id: RCData.java,v 1.13 2006/07/27 20:04:31 fredt Exp $ */
45
46 /**
47  * All the info we need to connect up to a database.
48  *
49  * @author Blaine Simpson unsaved@users
50  */

51 public class RCData {
52
53     public static final String JavaDoc DEFAULT_JDBC_DRIVER = "org.hsqldb.jdbcDriver";
54
55     /**
56      * Just for testing and debugging.
57      */

58     public void report() {
59         System.err.println("urlid: " + id + ", url: " + url + ", username: "
60                            + username + ", password: " + password);
61     }
62
63     /**
64      * Creates a RCDataObject by looking up the given key in the
65      * given authentication file.
66      *
67      * @param dbKey Key to look up in the file.
68      * @param file File containing the authentication information.
69      */

70     public RCData(File JavaDoc file, String JavaDoc dbKey) throws Exception JavaDoc {
71
72         if (file == null) {
73             throw new IllegalArgumentException JavaDoc("RC file name not specified");
74         }
75
76         if (!file.canRead()) {
77             throw new IOException JavaDoc("Please set up authentication file '"
78                                   + file + "'");
79         }
80
81         // System.err.println("Using RC file '" + file + "'");
82
StringTokenizer JavaDoc tokenizer = null;
83         boolean thisone = false;
84         String JavaDoc s;
85         String JavaDoc keyword, value;
86         int linenum = 0;
87         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
88
89         while ((s = br.readLine()) != null) {
90             ++linenum;
91
92             s = s.trim();
93
94             if (s.length() == 0) {
95                 continue;
96             }
97
98             if (s.charAt(0) == '#') {
99                 continue;
100             }
101
102             tokenizer = new StringTokenizer JavaDoc(s);
103
104             if (tokenizer.countTokens() == 1) {
105                 keyword = tokenizer.nextToken();
106                 value = "";
107             } else if (tokenizer.countTokens() > 1) {
108                 keyword = tokenizer.nextToken();
109                 value = tokenizer.nextToken("").trim();
110             } else {
111                 try {
112                     br.close();
113                 } catch (IOException JavaDoc e) {}
114
115                 throw new Exception JavaDoc("Corrupt line " + linenum + " in '"
116                                     + file + "': " + s);
117             }
118
119             if (dbKey == null) {
120                 if (keyword.equals("urlid")) {
121                     System.out.println(value);
122                 }
123
124                 continue;
125             }
126
127             if (keyword.equals("urlid")) {
128                 if (value.equals(dbKey)) {
129                     if (id == null) {
130                         id = dbKey;
131                         thisone = true;
132                     } else {
133                         try {
134                             br.close();
135                         } catch (IOException JavaDoc e) {}
136
137                         throw new Exception JavaDoc("Key '" + dbKey + " redefined at"
138                                             + " line " + linenum + " in '"
139                                             + file);
140                     }
141                 } else {
142                     thisone = false;
143                 }
144
145                 continue;
146             }
147
148             if (thisone) {
149                 if (keyword.equals("url")) {
150                     url = value;
151                 } else if (keyword.equals("username")) {
152                     username = value;
153                 } else if (keyword.equals("driver")) {
154                     driver = value;
155                 } else if (keyword.equals("charset")) {
156                     charset = value;
157                 } else if (keyword.equals("truststore")) {
158                     truststore = value;
159                 } else if (keyword.equals("password")) {
160                     password = value;
161                 } else {
162                     try {
163                         br.close();
164                     } catch (IOException JavaDoc e) {}
165
166                     throw new Exception JavaDoc("Bad line " + linenum + " in '"
167                                         + file + "': " + s);
168                 }
169             }
170         }
171
172         try {
173             br.close();
174         } catch (IOException JavaDoc e) {}
175
176         if (dbKey == null) {
177             return;
178         }
179
180         if (url == null || username == null || password == null) {
181             throw new Exception JavaDoc("url or username or password not set "
182                                 + "for '" + dbKey + "' in file '" + file
183                                 + "'");
184         }
185     }
186
187     /**
188      * <p>Creates a new <code>RCData</code> object.
189      *
190      * <p>The parameters driver, charset, and truststore are optional. Setting
191      * these parameters to <code>NULL</code> will set them to their default
192      * values.
193      *
194      * @param id The identifier for these connection settings
195      * @param url The URL of the database to connect to
196      * @param username The username to log in as
197      * @param password The password of the username
198      * @param driver The JDBC driver to use
199      * @param charset The character set to use
200      * @param truststore The trust store to use
201      * @throws Exception if the a non-optional parameter is set to <code>NULL</code>
202      */

203     public RCData(String JavaDoc id, String JavaDoc url, String JavaDoc username, String JavaDoc password,
204                   String JavaDoc driver, String JavaDoc charset,
205                   String JavaDoc truststore) throws Exception JavaDoc {
206
207         this.id = id;
208         this.url = url;
209         this.username = username;
210         this.password = password;
211         this.driver = driver;
212         this.charset = charset;
213         this.truststore = truststore;
214
215         if (id == null || url == null || username == null
216                 || password == null) {
217             throw new Exception JavaDoc("id, url, username, or password was not set");
218         }
219     }
220
221     String JavaDoc id = null;
222     String JavaDoc url = null;
223     String JavaDoc username = null;
224     String JavaDoc password = null;
225     String JavaDoc driver = null;
226     String JavaDoc charset = null;
227     String JavaDoc truststore = null;
228
229     /**
230      * Gets a JDBC Connection using the data of this RCData object.
231      *
232      * @return New JDBC Connection
233      */

234     public Connection JavaDoc getConnection()
235     throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc,
236            IllegalAccessException JavaDoc, SQLException JavaDoc {
237         return getConnection(null, null, null);
238     }
239
240     /**
241      * Gets a JDBC Connection using the data of this RCData object with
242      * specified override elements
243      *
244      * @return New JDBC Connection
245      */

246     public Connection JavaDoc getConnection(String JavaDoc curDriver, String JavaDoc curCharset,
247                                     String JavaDoc curTrustStore)
248                                     throws ClassNotFoundException JavaDoc,
249                                            InstantiationException JavaDoc,
250                                            IllegalAccessException JavaDoc,
251                                            SQLException JavaDoc {
252
253         Properties JavaDoc sysProps = System.getProperties();
254
255         if (curDriver == null) {
256
257             // If explicit driver not specified
258
curDriver = ((driver == null) ? DEFAULT_JDBC_DRIVER
259                                           : driver);
260         }
261
262         if (curCharset == null && charset != null) {
263             curCharset = charset;
264         }
265
266         if (curTrustStore == null && truststore != null) {
267             curTrustStore = truststore;
268         }
269
270         if (curCharset == null) {
271             sysProps.remove("sqlfile.charset");
272         } else {
273             sysProps.put("sqlfile.charset", curCharset);
274         }
275
276         if (curTrustStore == null) {
277             sysProps.remove("javax.net.ssl.trustStore");
278         } else {
279             sysProps.put("javax.net.ssl.trustStore", curTrustStore);
280         }
281
282         // As described in the JDBC FAQ:
283
// http://java.sun.com/products/jdbc/jdbc-frequent.html;
284
// Why doesn't calling class.forName() load my JDBC driver?
285
// There is a bug in the JDK 1.1.x that can cause Class.forName()
286
// to fail. // new org.hsqldb.jdbcDriver();
287
Class.forName(curDriver).newInstance();
288
289         return DriverManager.getConnection(url, username, password);
290     }
291 }
292
Popular Tags