KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > UsernamePasswordCredential


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.security.jaas;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import javax.security.auth.DestroyFailedException JavaDoc;
22 import javax.security.auth.Destroyable JavaDoc;
23 import javax.security.auth.RefreshFailedException JavaDoc;
24 import javax.security.auth.Refreshable JavaDoc;
25
26
27 /**
28  * A username/password credential. Used to store the username/password in the
29  * Subject's private credentials.
30  *
31  * @version $Revision: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class UsernamePasswordCredential implements Destroyable JavaDoc, Refreshable JavaDoc, Serializable JavaDoc {
34
35     private String JavaDoc username;
36     private char[] password;
37     private boolean destroyed;
38
39     public UsernamePasswordCredential(String JavaDoc username, char[] password) {
40         assert username != null;
41         assert password != null;
42
43         this.username = username;
44         this.password = new char[password.length];
45         System.arraycopy(password, 0, this.password, 0, password.length);
46     }
47
48     public String JavaDoc getUsername() {
49         return username;
50     }
51
52     public char[] getPassword() {
53         return password;
54     }
55
56     public void destroy() throws DestroyFailedException JavaDoc {
57         Arrays.fill(password, ' ');
58         
59         username = null;
60         password = null;
61         destroyed = true;
62     }
63
64     public boolean isDestroyed() {
65         return destroyed;
66     }
67
68     public void refresh() throws RefreshFailedException JavaDoc {
69     }
70
71     public boolean isCurrent() {
72         return !destroyed;
73     }
74
75     public boolean equals(Object JavaDoc o) {
76         if (this == o) return true;
77         if (!(o instanceof UsernamePasswordCredential)) return false;
78
79         final UsernamePasswordCredential credential = (UsernamePasswordCredential) o;
80
81         if (destroyed != credential.destroyed) return false;
82         if (!Arrays.equals(password, credential.password)) return false;
83         if (username != null ? !username.equals(credential.username) : credential.username != null) return false;
84
85         return true;
86     }
87
88     public int hashCode() {
89         int result;
90         result = (username != null ? username.hashCode() : 0);
91         result = 29 * result + (destroyed ? 1 : 0);
92         return result;
93     }
94 }
95
Popular Tags