1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.resource.spi.security;
  6. import javax.resource.spi.ManagedConnectionFactory;
  7. /**
  8. * The class PasswordCredential acts as a holder for username and
  9. * password.
  10. *
  11. * @see javax.resource.spi.ManagedConnectionFactory
  12. *
  13. * @author Rahul Sharma
  14. * @version 0.6
  15. * @since 0.6
  16. */
  17. public final class PasswordCredential implements java.io.Serializable {
  18. private String userName;
  19. private char[] password;
  20. private ManagedConnectionFactory mcf;
  21. /**
  22. * Creates a new <code>PasswordCredential</code> object from the given
  23. * user name and password.
  24. *
  25. * <p> Note that the given user password is cloned before it is stored in
  26. * the new <code>PasswordCredential</code> object.
  27. *
  28. * @param userName the user name
  29. * @param password the user's password
  30. **/
  31. public
  32. PasswordCredential(String userName, char[] password) {
  33. this.userName = userName;
  34. this.password = (char[])password.clone();
  35. }
  36. /**
  37. * Returns the user name.
  38. *
  39. * @return the user name
  40. **/
  41. public
  42. String getUserName() {
  43. return userName;
  44. }
  45. /**
  46. * Returns the user password.
  47. *
  48. * <p> Note that this method returns a reference to the password. It is
  49. * the caller's responsibility to zero out the password information after
  50. * it is no longer needed.
  51. *
  52. * @return the password
  53. **/
  54. public
  55. char[] getPassword() {
  56. return password;
  57. }
  58. /** Gets the target ManagedConnectionFactory for which the user name and
  59. * password has been set by the application server. A ManagedConnection-
  60. * Factory uses this field to find out whether PasswordCredential should
  61. * be used by it for sign-on to the target EIS instance.
  62. *
  63. * @return ManagedConnectionFactory instance for which user name and
  64. * password have been specified
  65. **/
  66. public
  67. ManagedConnectionFactory getManagedConnectionFactory() {
  68. return mcf;
  69. }
  70. /** Sets the target ManagedConenctionFactory instance for which the user
  71. * name and password has been set by the application server.
  72. *
  73. * @param mcf ManagedConnectionFactory instance for which user name
  74. * and password have been specified
  75. **/
  76. public
  77. void setManagedConnectionFactory(ManagedConnectionFactory mcf) {
  78. this.mcf = mcf;
  79. }
  80. /** Compares this PasswordCredential with the specified object for
  81. * equality. The two PasswordCredential instances are the same if
  82. * they are equal in username and password.
  83. *
  84. * @param other Object to which PasswordCredential is to be compared
  85. * @return <tt>true</tt> if and if the specified object is a
  86. * PasswordCredential whose username and password are
  87. * equal to this instance.
  88. **/
  89. public
  90. boolean equals(Object other) {
  91. if (!(other instanceof PasswordCredential))
  92. return false;
  93. PasswordCredential pc = (PasswordCredential)other;
  94. if (!(userName.equals(pc.userName)))
  95. return false;
  96. if (password.length != pc.password.length)
  97. return false;
  98. for (int i = 0; i < password.length;i++) {
  99. if (password[i] != pc.password[i])
  100. return false;
  101. }
  102. return true;
  103. }
  104. /** Returns the hash code for this PasswordCredential
  105. *
  106. * @return hash code for this PasswordCredential
  107. **/
  108. public
  109. int hashCode() {
  110. String s = userName;
  111. s += new String(password);
  112. return s.hashCode();
  113. }
  114. }