1 21 22 package org.apache.derby.jdbc; 23 24 import org.apache.derby.iapi.reference.Attribute; 25 26 import java.sql.Connection ; 27 import java.sql.DriverManager ; 28 import java.sql.SQLException ; 29 30 import java.io.PrintWriter ; 31 import java.util.Properties ; 32 33 34 import javax.sql.DataSource ; 35 36 37 import org.apache.derby.iapi.reference.Attribute; 38 import org.apache.derby.iapi.reference.MessageId; 39 import org.apache.derby.iapi.reference.SQLState; 40 import org.apache.derby.iapi.error.ExceptionSeverity; 41 import org.apache.derby.iapi.services.i18n.MessageService; 42 import org.apache.derby.impl.jdbc.Util; 43 44 194 public class EmbeddedDataSource extends ReferenceableDataSource implements 195 javax.sql.DataSource 196 { 197 198 private static final long serialVersionUID = -4945135214995641181L; 199 200 201 202 206 private String databaseName; 207 208 212 private String dataSourceName; 213 214 218 private String description; 219 220 224 private String createDatabase; 225 226 230 private String shutdownDatabase; 231 232 236 private String connectionAttributes; 237 238 241 private boolean attributesAsPassword; 242 243 244 transient private PrintWriter printer; 245 transient private int loginTimeout; 246 247 transient InternalDriver driver; 250 251 transient private String jdbcurl; 252 253 256 public EmbeddedDataSource() { 257 259 } 263 264 265 public boolean equals(Object p0) { 270 if (p0 instanceof EmbeddedDataSource) { 271 EmbeddedDataSource ds = (EmbeddedDataSource)p0; 272 273 boolean match = true; 274 275 if (databaseName != null) { 276 if (!(databaseName.equals(ds.databaseName))) 277 match = false; 278 } else if (ds.databaseName != null) 279 match = false; 280 281 if (dataSourceName != null) { 282 if (!(dataSourceName.equals(ds.dataSourceName))) 283 match = false; 284 } else if (ds.dataSourceName != null) 285 match = false; 286 287 if (description != null) { 288 if (!(description.equals(ds.description))) 289 match = false; 290 } else if (ds.description != null) 291 match = false; 292 293 if (createDatabase != null) { 294 if (!(createDatabase.equals(ds.createDatabase))) 295 match = false; 296 } else if (ds.createDatabase != null) 297 match = false; 298 299 if (shutdownDatabase != null) { 300 if (!(shutdownDatabase.equals(ds.shutdownDatabase))) 301 match = false; 302 } else if (ds.shutdownDatabase != null) 303 match = false; 304 305 if (connectionAttributes != null) { 306 if (!(connectionAttributes.equals(ds.connectionAttributes))) 307 match = false; 308 } else if (ds.connectionAttributes != null) 309 match = false; 310 311 if (loginTimeout != ds.loginTimeout) 312 match = false; 313 314 return match; 315 316 } 317 318 return false; 319 } 320 321 324 325 334 public final void setCreateDatabase(String create) { 335 if (create != null && create.toLowerCase(java.util.Locale.ENGLISH).equals("create")) 336 createDatabase = create; 337 else 338 createDatabase = null; 339 } 340 341 public final String getCreateDatabase() { 342 return createDatabase; 343 } 344 345 346 353 public final void setShutdownDatabase(String shutdown) { 354 if (shutdown != null && shutdown.equalsIgnoreCase("shutdown")) 355 shutdownDatabase = shutdown; 356 else 357 shutdownDatabase = null; 358 } 359 360 public final String getShutdownDatabase() { 361 return shutdownDatabase; 362 } 363 364 381 public final void setConnectionAttributes(String prop) { 382 connectionAttributes = prop; 383 update(); 384 } 385 386 public final String getConnectionAttributes() { 387 return connectionAttributes; 388 } 389 390 391 398 public final void setAttributesAsPassword(boolean attributesAsPassword) { 399 this.attributesAsPassword = attributesAsPassword; 400 update(); 401 } 402 403 406 public final boolean getAttributesAsPassword() { 407 return attributesAsPassword; 408 } 409 410 413 414 415 421 public final Connection getConnection() throws SQLException 422 { 423 return this.getConnection(getUser(), getPassword(), false); 424 } 425 426 438 public final Connection getConnection(String username, String password) 439 throws SQLException 440 { 441 return this.getConnection(username, password, true); 442 } 443 444 447 final Connection getConnection(String username, String password, boolean requestPassword) 448 throws SQLException { 449 450 Properties info = new Properties (); 451 if (username != null) 452 info.put(Attribute.USERNAME_ATTR, username); 453 454 if (!requestPassword || !attributesAsPassword) 455 { 456 if (password != null) 457 info.put(Attribute.PASSWORD_ATTR, password); 458 } 459 460 if (createDatabase != null) 461 info.put(Attribute.CREATE_ATTR, "true"); 462 if (shutdownDatabase != null) 463 info.put(Attribute.SHUTDOWN_ATTR, "true"); 464 465 String url = jdbcurl; 466 467 if (attributesAsPassword && requestPassword && password != null) { 468 469 470 StringBuffer sb = new StringBuffer (url.length() + password.length() + 1); 471 472 sb.append(url); 473 sb.append(';'); 474 sb.append(password); 476 url = sb.toString(); 477 478 } 479 Connection conn = findDriver().connect(url, info); 480 481 if (conn == null) 484 throw Util.generateCsSQLException(SQLState.PROPERTY_INVALID_VALUE,Attribute.DBNAME_ATTR,getDatabaseName()); 485 486 return conn; 487 } 488 489 InternalDriver findDriver() throws SQLException 490 { 491 String url = jdbcurl; 492 493 if (driver == null || !driver.acceptsURL(url)) 494 { 495 synchronized(this) 496 { 497 if (driver == null || !driver.acceptsURL(url)) 500 { 501 502 new org.apache.derby.jdbc.EmbeddedDriver(); 503 504 507 AutoloadedDriver autoloadedDriver = 508 (AutoloadedDriver) DriverManager.getDriver(url); 509 driver = (InternalDriver) autoloadedDriver.getDriverModule(); 510 } 512 } 513 } 514 return driver; 515 } 517 518 void update() 519 { 520 StringBuffer sb = new StringBuffer (64); 521 522 sb.append(Attribute.PROTOCOL); 523 524 525 String dbName = getDatabaseName(); 527 528 if (dbName != null) { 529 dbName = dbName.trim(); 530 } 531 532 if (dbName == null || dbName.length() == 0) { 533 537 dbName = " "; 541 } 542 543 sb.append(dbName); 544 545 546 String connAttrs = getConnectionAttributes(); 547 if (connAttrs != null) { 548 connAttrs = connAttrs.trim(); 549 if (connAttrs.length() != 0) { 550 sb.append(';'); 551 sb.append(connectionAttributes); 552 } 553 } 554 555 jdbcurl = sb.toString(); 556 } 557 } 558 | Popular Tags |