1 21 22 package org.apache.derby.jdbc; 23 24 import org.apache.derby.iapi.jdbc.JDBCBoot; 25 import org.apache.derby.iapi.reference.Attribute; 26 import org.apache.derby.iapi.reference.MessageId; 27 28 import java.sql.Connection ; 29 import java.sql.SQLException ; 30 31 import java.io.PrintWriter ; 32 import java.util.Properties ; 33 34 35 import javax.sql.DataSource ; 36 37 import org.apache.derby.iapi.reference.SQLState; 38 import org.apache.derby.iapi.services.i18n.MessageService; 39 import org.apache.derby.impl.jdbc.Util; 40 41 60 public final class EmbeddedSimpleDataSource implements DataSource { 61 62 private String password; 63 64 private String user; 65 66 71 private String databaseName; 72 73 78 private String dataSourceName; 79 80 85 private String description; 86 87 92 private String createDatabase; 93 94 99 private String shutdownDatabase; 100 101 106 private String connectionAttributes; 107 108 109 transient private PrintWriter printer; 110 111 transient private int loginTimeout; 112 113 transient private InternalDriver driver; 116 117 transient private String jdbcurl; 118 119 122 public EmbeddedSimpleDataSource() { 123 } 124 125 128 129 140 public int getLoginTimeout() throws SQLException { 141 return loginTimeout; 142 } 143 144 158 public void setLoginTimeout(int seconds) throws SQLException { 159 loginTimeout = seconds; 160 } 161 162 179 public PrintWriter getLogWriter() throws SQLException { 180 return printer; 181 } 182 183 201 public void setLogWriter(PrintWriter out) throws SQLException { 202 printer = out; 203 } 204 205 208 217 public final synchronized void setDatabaseName(String databaseName) { 218 this.databaseName = databaseName; 219 update(); 220 } 221 222 public String getDatabaseName() { 223 return databaseName; 224 } 225 226 233 public final void setDataSourceName(String dsn) { 234 dataSourceName = dsn; 235 } 236 237 238 public final String getDataSourceName() { 239 return dataSourceName; 240 } 241 242 249 public final void setDescription(String desc) { 250 description = desc; 251 } 252 253 254 public final String getDescription() { 255 return description; 256 } 257 258 262 public final void setUser(String user) { 263 this.user = user; 264 } 265 266 267 public final String getUser() { 268 return user; 269 } 270 271 276 public final void setPassword(String password) { 277 this.password = password; 278 } 279 280 281 public final String getPassword() { 282 return password; 283 } 284 285 295 public final void setCreateDatabase(String create) { 296 if (create != null 297 && create.toLowerCase(java.util.Locale.ENGLISH) 298 .equals("create")) 299 createDatabase = create; 300 else 301 createDatabase = null; 302 } 303 304 305 public final String getCreateDatabase() { 306 return createDatabase; 307 } 308 309 317 public final void setShutdownDatabase(String shutdown) { 318 if (shutdown != null && shutdown.equalsIgnoreCase("shutdown")) 319 shutdownDatabase = shutdown; 320 else 321 shutdownDatabase = null; 322 } 323 324 325 public final String getShutdownDatabase() { 326 return shutdownDatabase; 327 } 328 329 351 public final void setConnectionAttributes(String prop) { 352 connectionAttributes = prop; 353 update(); 354 } 355 356 357 public final String getConnectionAttributes() { 358 return connectionAttributes; 359 } 360 361 364 365 372 public final Connection getConnection() throws SQLException { 373 return this.getConnection(getUser(), getPassword()); 374 } 375 376 391 public final Connection getConnection(String username, String password) 392 throws SQLException { 393 394 Properties info = new Properties (); 395 if (username != null) 396 info.put(Attribute.USERNAME_ATTR, username); 397 398 if (password != null) 399 info.put(Attribute.PASSWORD_ATTR, password); 400 401 if (createDatabase != null) 402 info.put(Attribute.CREATE_ATTR, "true"); 403 if (shutdownDatabase != null) 404 info.put(Attribute.SHUTDOWN_ATTR, "true"); 405 406 Connection conn = findDriver().connect(jdbcurl, info); 407 408 if (conn == null) 411 throw Util.generateCsSQLException(SQLState.PROPERTY_INVALID_VALUE, 412 Attribute.DBNAME_ATTR, getDatabaseName()); 413 414 return conn; 415 } 416 417 private InternalDriver findDriver() throws SQLException { 418 String url = jdbcurl; 419 420 if (driver == null || !driver.acceptsURL(url)) { 421 synchronized (this) { 422 if (driver == null || !driver.acceptsURL(url)) { 425 426 427 new JDBCBoot().boot(Attribute.PROTOCOL, System.err); 428 429 432 driver = InternalDriver.activeDriver(); 433 434 if (driver == null) 435 throw new SQLException (MessageService.getTextMessage(MessageId.CORE_JDBC_DRIVER_UNREGISTERED)); 436 } 437 } 438 } 439 return driver; 440 } 442 443 private void update() { 444 StringBuffer sb = new StringBuffer (64); 445 446 sb.append(Attribute.PROTOCOL); 447 448 String dbName = getDatabaseName(); 450 451 if (dbName != null) { 452 dbName = dbName.trim(); 453 } 454 455 if (dbName == null || dbName.length() == 0) { 456 460 dbName = " "; 467 } 468 469 sb.append(dbName); 470 471 String connAttrs = getConnectionAttributes(); 472 if (connAttrs != null) { 473 connAttrs = connAttrs.trim(); 474 if (connAttrs.length() != 0) { 475 sb.append(';'); 476 sb.append(connectionAttributes); 477 } 478 } 479 480 jdbcurl = sb.toString(); 481 } 482 } 483 484 | Popular Tags |