1 30 31 32 package org.hsqldb.util; 33 34 import java.io.BufferedReader ; 35 import java.io.File ; 36 import java.io.FileReader ; 37 import java.io.IOException ; 38 import java.sql.Connection ; 39 import java.sql.DriverManager ; 40 import java.sql.SQLException ; 41 import java.util.Properties ; 42 import java.util.StringTokenizer ; 43 44 45 46 51 public class RCData { 52 53 public static final String DEFAULT_JDBC_DRIVER = "org.hsqldb.jdbcDriver"; 54 55 58 public void report() { 59 System.err.println("urlid: " + id + ", url: " + url + ", username: " 60 + username + ", password: " + password); 61 } 62 63 70 public RCData(File file, String dbKey) throws Exception { 71 72 if (file == null) { 73 throw new IllegalArgumentException ("RC file name not specified"); 74 } 75 76 if (!file.canRead()) { 77 throw new IOException ("Please set up authentication file '" 78 + file + "'"); 79 } 80 81 StringTokenizer tokenizer = null; 83 boolean thisone = false; 84 String s; 85 String keyword, value; 86 int linenum = 0; 87 BufferedReader br = new BufferedReader (new FileReader (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 (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 e) {} 114 115 throw new Exception ("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 e) {} 136 137 throw new Exception ("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 e) {} 165 166 throw new Exception ("Bad line " + linenum + " in '" 167 + file + "': " + s); 168 } 169 } 170 } 171 172 try { 173 br.close(); 174 } catch (IOException e) {} 175 176 if (dbKey == null) { 177 return; 178 } 179 180 if (url == null || username == null || password == null) { 181 throw new Exception ("url or username or password not set " 182 + "for '" + dbKey + "' in file '" + file 183 + "'"); 184 } 185 } 186 187 203 public RCData(String id, String url, String username, String password, 204 String driver, String charset, 205 String truststore) throws Exception { 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 ("id, url, username, or password was not set"); 218 } 219 } 220 221 String id = null; 222 String url = null; 223 String username = null; 224 String password = null; 225 String driver = null; 226 String charset = null; 227 String truststore = null; 228 229 234 public Connection getConnection() 235 throws ClassNotFoundException , InstantiationException , 236 IllegalAccessException , SQLException { 237 return getConnection(null, null, null); 238 } 239 240 246 public Connection getConnection(String curDriver, String curCharset, 247 String curTrustStore) 248 throws ClassNotFoundException , 249 InstantiationException , 250 IllegalAccessException , 251 SQLException { 252 253 Properties sysProps = System.getProperties(); 254 255 if (curDriver == null) { 256 257 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 Class.forName(curDriver).newInstance(); 288 289 return DriverManager.getConnection(url, username, password); 290 } 291 } 292 | Popular Tags |