1 21 package oracle.toplink.essentials.sessions; 23 24 import java.util.*; 25 import java.io.*; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.text.MessageFormat ; 29 import oracle.toplink.essentials.internal.databaseaccess.*; 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.queryframework.*; 32 import oracle.toplink.essentials.sequencing.Sequence; 33 import oracle.toplink.essentials.Version; 34 import oracle.toplink.essentials.internal.helper.*; 35 import oracle.toplink.essentials.internal.localization.*; 36 import oracle.toplink.essentials.internal.security.SecurableObjectHolder; 37 import oracle.toplink.essentials.internal.helper.ConversionManager; 38 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 39 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass; 40 41 50 public abstract class DatasourceLogin implements oracle.toplink.essentials.sessions.Login, Serializable, Cloneable { 51 52 53 private static final String versionStringTemplate = "{0} - {1} (Build {2})"; 54 public static String versionString = null; 55 56 57 protected Properties properties; 58 59 60 protected Platform platform; 61 62 63 protected static String encryptedString = null; 64 65 66 private boolean isEncryptedPasswordSet; 67 private transient SecurableObjectHolder securableObjectHolder; 68 69 70 protected Connector connector; 71 72 73 protected boolean usesExternalConnectionPooling; 74 75 76 protected boolean usesExternalTransactionController; 77 78 86 protected int cacheTransactionIsolation = SYNCRONIZED_OBJECT_LEVEL_READ_WRITE_DATABASE; 87 88 89 public static final int CONCURRENT_READ_WRITE = 1; 90 91 92 public static final int SYNCHRONIZED_WRITE = 2; 93 94 95 public static final int SYNCHRONIZED_READ_ON_WRITE = 3; 96 97 100 public static final int SYNCRONIZED_OBJECT_LEVEL_READ_WRITE = 4; 101 102 105 public static final int SYNCRONIZED_OBJECT_LEVEL_READ_WRITE_DATABASE = 5; 106 107 111 public DatasourceLogin() { 112 this(new DatasourcePlatform()); 113 } 114 115 119 public DatasourceLogin(Platform databasePlatform) { 120 this.platform = databasePlatform; 121 122 this.dontUseExternalConnectionPooling(); 123 this.dontUseExternalTransactionController(); 124 125 this.properties = new Properties(); 126 this.properties.put("user", ""); 127 this.properties.put("password", ""); 128 this.isEncryptedPasswordSet = false; 129 this.securableObjectHolder = new SecurableObjectHolder(); 130 } 131 132 137 protected SecurableObjectHolder getSecurableObjectHolder() { 138 if (securableObjectHolder == null) { 139 securableObjectHolder = new SecurableObjectHolder(); 140 securableObjectHolder.getSecurableObject(); 141 } 142 return securableObjectHolder; 143 } 144 145 150 public Object clone() { 151 DatasourceLogin clone = null; 152 try { 153 clone = (DatasourceLogin)super.clone(); 154 } catch (Exception exception) { 155 } 157 if (getConnector() != null) { 158 clone.setConnector((Connector)getConnector().clone()); 159 } 160 clone.setDatasourcePlatform((Platform)getDatasourcePlatform().clone()); 161 clone.setProperties((Properties)properties.clone()); 162 return clone; 163 } 164 165 169 public Object connectToDatasource(Accessor accessor) throws DatabaseException { 170 return getConnector().connect(prepareProperties(properties)); 171 } 172 173 187 public int getCacheTransactionIsolation() { 188 return cacheTransactionIsolation; 189 } 190 191 195 public Connector getConnector() { 196 return connector; 197 } 198 199 206 public DatabasePlatform getPlatform() { 207 try { 208 return (DatabasePlatform)getDatasourcePlatform(); 209 } catch (ClassCastException wrongType) { 210 throw ValidationException.notSupportedForDatasource(); 211 } 212 } 213 214 219 public Platform getDatasourcePlatform() { 220 return platform; 221 } 222 223 230 public Properties getProperties() { 231 return properties; 232 } 233 234 241 public Object getProperty(String name) { 242 return getProperties().get(name); 243 } 244 245 253 public String getTableQualifier() { 254 return getDatasourcePlatform().getTableQualifier(); 255 } 256 257 263 public String getUserName() { 264 return properties.getProperty("user"); 265 } 266 267 272 public static String getVersion() { 273 if (versionString == null) { 274 Object [] args = { Version.getProduct(), Version.getVersion(), Version.getBuildNumber() }; 275 versionString = MessageFormat.format(versionStringTemplate, args); 276 } 277 return versionString; 278 } 279 280 285 private Properties prepareProperties(Properties properties) { 286 Properties result = (Properties)properties.clone(); 287 String password = result.getProperty("password"); 288 if (password != null) { 289 if (getSecurableObjectHolder().hasSecurableObject() || isEncryptedPasswordSet) { 302 result.put("password", getSecurableObjectHolder().getSecurableObject().decryptPassword(password)); 303 } 304 } 305 306 return result; 307 } 308 309 314 public void removeProperty(String propertyName) { 315 properties.remove(propertyName); 316 } 317 318 332 public void setCacheTransactionIsolation(int cacheTransactionIsolation) { 333 this.cacheTransactionIsolation = cacheTransactionIsolation; 334 } 335 336 349 public void setConnector(Connector connector) { 350 this.connector = connector; 351 } 352 353 359 public void setDefaultNullValue(Class type, Object value) { 360 getDatasourcePlatform().getConversionManager().setDefaultNullValue(type, value); 361 } 362 363 366 public void setPassword(String password) { 367 if (password != null) { 368 setProperty("password", getSecurableObjectHolder().getSecurableObject().encryptPassword(password)); 370 } else { 371 removeProperty("password"); 373 } 374 } 375 376 379 public String getPassword() { 380 return properties.getProperty("password"); 381 } 382 383 386 public void setEncryptedPassword(String password) { 387 isEncryptedPasswordSet = true; 390 391 if (password != null) { 392 setProperty("password", password); 393 } else { removeProperty("password"); 395 } 396 } 397 398 401 public void setEncryptionClassName(String encryptionClassName) { 402 getSecurableObjectHolder().setEncryptionClassName(encryptionClassName); 403 } 404 405 410 public void setPlatform(Platform platform) { 411 setDatasourcePlatform(platform); 412 } 413 414 419 public void setDatasourcePlatform(Platform platform) { 420 this.platform = platform; 421 } 422 423 427 public String getPlatformClassName() { 428 return getDatasourcePlatform().getClass().getName(); 429 } 430 431 436 public void setPlatformClassName(String platformClassName) throws ValidationException { 437 Class platformClass = null; 438 try { 439 platformClass = this.getClass().getClassLoader().loadClass(platformClassName); 441 Platform platform = null; 442 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 443 try { 444 platform = (Platform)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(platformClass)); 445 } catch (PrivilegedActionException exception) { 446 throw exception.getException(); 447 } 448 } else { 449 platform = (Platform)PrivilegedAccessHelper.newInstanceFromClass(platformClass); 450 } 451 usePlatform(platform); 452 } catch(Exception cne) { 453 try { 455 platformClass = ConversionManager.loadClass(platformClassName); 456 Platform platform = null; 457 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 458 try { 459 platform = (Platform)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(platformClass)); 460 } catch (PrivilegedActionException exception) { 461 throw ValidationException.platformClassNotFound(exception.getException(), platformClassName); 462 } 463 } else { 464 platform = (Platform)PrivilegedAccessHelper.newInstanceFromClass(platformClass); 465 } 466 usePlatform(platform); 467 } catch(Exception cne2) { 468 throw ValidationException.platformClassNotFound(cne2, platformClassName); 470 } 471 } 472 } 473 474 478 public void usePlatform(Platform platform) { 479 if (getDatasourcePlatform() != null) { 480 getDatasourcePlatform().copyInto(platform); 481 } 482 setPlatform(platform); 483 } 484 485 490 public void setProperties(Properties properties) { 491 this.properties = properties; 492 } 493 494 499 public void setProperty(String propertyName, Object propertyValue) { 500 properties.put(propertyName, propertyValue); 501 } 502 503 509 public void setTableQualifier(String qualifier) { 510 getDatasourcePlatform().setTableQualifier(qualifier); 511 } 512 513 517 public void setTimestampQuery(ValueReadQuery timestampQuery) { 518 getDatasourcePlatform().setTimestampQuery(timestampQuery); 519 } 520 521 527 public void setUserName(String name) { 528 if (name != null) { 529 setProperty("user", name); 530 } 531 } 532 533 537 public void setUsesExternalConnectionPooling(boolean usesExternalConnectionPooling) { 538 this.usesExternalConnectionPooling = usesExternalConnectionPooling; 539 } 540 541 545 public void setUsesExternalTransactionController(boolean usesExternalTransactionController) { 546 this.usesExternalTransactionController = usesExternalTransactionController; 547 } 548 549 556 public void dontUseExternalConnectionPooling() { 557 setUsesExternalConnectionPooling(false); 558 } 559 560 567 public void dontUseExternalTransactionController() { 568 setUsesExternalTransactionController(false); 569 } 570 571 575 public boolean shouldAllowConcurrentReadWrite() { 576 return getCacheTransactionIsolation() == CONCURRENT_READ_WRITE; 577 } 578 579 583 public boolean shouldSynchronizedReadOnWrite() { 584 return getCacheTransactionIsolation() == SYNCHRONIZED_READ_ON_WRITE; 585 } 586 587 592 public boolean shouldSynchronizeWrites() { 593 return getCacheTransactionIsolation() == SYNCHRONIZED_WRITE; 594 } 595 596 601 public boolean shouldSynchronizeObjectLevelReadWrite(){ 602 return getCacheTransactionIsolation() == SYNCRONIZED_OBJECT_LEVEL_READ_WRITE; 603 } 604 605 610 public boolean shouldSynchronizeObjectLevelReadWriteDatabase(){ 611 return getCacheTransactionIsolation() == SYNCRONIZED_OBJECT_LEVEL_READ_WRITE_DATABASE; 612 } 613 614 622 public boolean shouldUseExternalConnectionPooling() { 623 return usesExternalConnectionPooling; 624 } 625 626 633 public boolean shouldUseExternalTransactionController() { 634 return usesExternalTransactionController; 635 } 636 637 644 public void useExternalConnectionPooling() { 645 setUsesExternalConnectionPooling(true); 646 } 647 648 655 public void useExternalTransactionController() { 656 setUsesExternalTransactionController(true); 657 } 658 659 663 public String toString() { 664 StringWriter stringWriter = new StringWriter(); 665 PrintWriter writer = new PrintWriter(stringWriter); 666 writer.write(Helper.getShortClassName(getClass())); 667 writer.println("("); 668 writer.println("\t" + ToStringLocalization.buildMessage("platform", (Object [])null) + "=> " + getDatasourcePlatform()); 669 if (!shouldUseExternalConnectionPooling()) { 670 writer.println("\t" + ToStringLocalization.buildMessage("user_name", (Object [])null) + "=> \"" + getUserName() + "\""); 671 } 672 writer.print("\t"); 673 getConnector().toString(writer); 674 writer.write(")"); 675 return stringWriter.toString(); 676 } 677 678 681 public Sequence getDefaultSequence() { 682 return getDatasourcePlatform().getDefaultSequence(); 683 } 684 685 688 public void setDefaultSequence(Sequence sequence) { 689 getDatasourcePlatform().setDefaultSequence(sequence); 690 } 691 692 695 public void addSequence(Sequence sequence) { 696 getDatasourcePlatform().addSequence(sequence); 697 } 698 699 702 public Sequence getSequence(String seqName) { 703 return getDatasourcePlatform().getSequence(seqName); 704 } 705 706 709 public Map getSequences() { 710 return getDatasourcePlatform().getSequences(); 711 } 712 713 717 public Sequence removeSequence(String seqName) { 718 return getDatasourcePlatform().removeSequence(seqName); 719 } 720 721 724 public void removeAllSequences() { 725 getDatasourcePlatform().removeAllSequences(); 726 } 727 728 732 public Sequence getDefaultSequenceToWrite() { 733 return getDatasourcePlatform().getDefaultSequenceToWrite(); 734 } 735 736 740 public Map getSequencesToWrite() { 741 return getDatasourcePlatform().getSequencesToWrite(); 742 } 743 744 748 public void setSequences(Map sequences) { 749 getDatasourcePlatform().setSequences(sequences); 750 } 751 } 752 | Popular Tags |