KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > resource > spi > security > PasswordCredential


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package javax.resource.spi.security;
25
26 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
27
28 /**
29  * The class PasswordCredential acts as a holder for username and
30  * password.
31  *
32  * @see javax.resource.spi.ManagedConnectionFactory
33  *
34  * @author Rahul Sharma
35  * @version 0.6
36  * @since 0.6
37  */

38
39 public final class PasswordCredential implements java.io.Serializable JavaDoc {
40
41   private String JavaDoc userName;
42   private char[] password;
43   private ManagedConnectionFactory JavaDoc mcf;
44
45   /**
46    * Creates a new <code>PasswordCredential</code> object from the given
47    * user name and password.
48    *
49    * <p> Note that the given user password is cloned before it is stored in
50    * the new <code>PasswordCredential</code> object.
51    *
52    * @param userName the user name
53    * @param password the user's password
54   **/

55   public
56   PasswordCredential(String JavaDoc userName, char[] password) {
57     this.userName = userName;
58     this.password = (char[])password.clone();
59   }
60
61   /**
62    * Returns the user name.
63    *
64    * @return the user name
65   **/

66   public
67   String JavaDoc getUserName() {
68     return userName;
69   }
70
71   /**
72    * Returns the user password.
73    *
74    * <p> Note that this method returns a reference to the password. It is
75    * the caller's responsibility to zero out the password information after
76    * it is no longer needed.
77    *
78    * @return the password
79   **/

80   public
81   char[] getPassword() {
82     return password;
83   }
84
85   /** Gets the target ManagedConnectionFactory for which the user name and
86    * password has been set by the application server. A ManagedConnection-
87    * Factory uses this field to find out whether PasswordCredential should
88    * be used by it for sign-on to the target EIS instance.
89    *
90    * @return ManagedConnectionFactory instance for which user name and
91    * password have been specified
92    **/

93   public
94   ManagedConnectionFactory JavaDoc getManagedConnectionFactory() {
95     return mcf;
96   }
97
98  /** Sets the target ManagedConenctionFactory instance for which the user
99   * name and password has been set by the application server.
100    *
101    * @param mcf ManagedConnectionFactory instance for which user name
102    * and password have been specified
103    **/

104   public
105   void setManagedConnectionFactory(ManagedConnectionFactory JavaDoc mcf) {
106     this.mcf = mcf;
107   }
108
109   /** Compares this PasswordCredential with the specified object for
110    * equality. The two PasswordCredential instances are the same if
111    * they are equal in username and password.
112    *
113    * @param other Object to which PasswordCredential is to be compared
114    * @return <tt>true</tt> if and if the specified object is a
115    * PasswordCredential whose username and password are
116    * equal to this instance.
117   **/

118   public
119   boolean equals(Object JavaDoc other) {
120     if (!(other instanceof PasswordCredential JavaDoc))
121       return false;
122
123     PasswordCredential JavaDoc pc = (PasswordCredential JavaDoc)other;
124
125     if (!(userName.equals(pc.userName)))
126       return false;
127
128     if (password.length != pc.password.length)
129       return false;
130     
131     for (int i = 0; i < password.length;i++) {
132       if (password[i] != pc.password[i])
133     return false;
134     }
135
136     return true;
137   }
138
139   /** Returns the hash code for this PasswordCredential
140    *
141    * @return hash code for this PasswordCredential
142   **/

143   public
144   int hashCode() {
145     String JavaDoc s = userName;
146     s += new String JavaDoc(password);
147     return s.hashCode();
148   }
149
150 }
151
152
Popular Tags