KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > security > auth > callback > PasswordCallback


1 /*
2  * @(#)PasswordCallback.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.security.auth.callback;
9
10 /**
11  * <p> Underlying security services instantiate and pass a
12  * <code>PasswordCallback</code> to the <code>handle</code>
13  * method of a <code>CallbackHandler</code> to retrieve password information.
14  *
15  * @version 1.18, 12/19/03
16  * @see javax.security.auth.callback.CallbackHandler
17  */

18 public class PasswordCallback implements Callback JavaDoc, java.io.Serializable JavaDoc {
19
20     private static final long serialVersionUID = 2267422647454909926L;
21
22     /**
23      * @serial
24      * @since 1.4
25      */

26     private String JavaDoc prompt;
27     /**
28      * @serial
29      * @since 1.4
30      */

31     private boolean echoOn;
32     /**
33      * @serial
34      * @since 1.4
35      */

36     private char[] inputPassword;
37
38     /**
39      * Construct a <code>PasswordCallback</code> with a prompt
40      * and a boolean specifying whether the password should be displayed
41      * as it is being typed.
42      *
43      * <p>
44      *
45      * @param prompt the prompt used to request the password. <p>
46      *
47      * @param echoOn true if the password should be displayed
48      * as it is being typed.
49      *
50      * @exception IllegalArgumentException if <code>prompt</code> is null or
51      * if <code>prompt</code> has a length of 0.
52      */

53     public PasswordCallback(String JavaDoc prompt, boolean echoOn) {
54     if (prompt == null || prompt.length() == 0)
55         throw new IllegalArgumentException JavaDoc();
56
57     this.prompt = prompt;
58     this.echoOn = echoOn;
59     }
60
61     /**
62      * Get the prompt.
63      *
64      * <p>
65      *
66      * @return the prompt.
67      */

68     public String JavaDoc getPrompt() {
69     return prompt;
70     }
71
72     /**
73      * Return whether the password
74      * should be displayed as it is being typed.
75      *
76      * <p>
77      *
78      * @return the whether the password
79      * should be displayed as it is being typed.
80      */

81     public boolean isEchoOn() {
82     return echoOn;
83     }
84
85     /**
86      * Set the retrieved password.
87      *
88      * <p> This method makes a copy of the input <i>password</i>
89      * before storing it.
90      *
91      * <p>
92      *
93      * @param password the retrieved password, which may be null.
94      *
95      * @see #getPassword
96      */

97     public void setPassword(char[] password) {
98     this.inputPassword = (password == null ?
99             null : (char[])password.clone());
100     }
101
102     /**
103      * Get the retrieved password.
104      *
105      * <p> This method returns a copy of the retrieved password.
106      *
107      * <p>
108      *
109      * @return the retrieved password, which may be null.
110      *
111      * @see #setPassword
112      */

113     public char[] getPassword() {
114     return (inputPassword == null?
115             null : (char[])inputPassword.clone());
116     }
117
118     /**
119      * Clear the retrieved password.
120      */

121     public void clearPassword() {
122     if (inputPassword != null) {
123         for (int i = 0; i < inputPassword.length; i++)
124         inputPassword[i] = ' ';
125     }
126     }
127 }
128
Popular Tags