KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > util > PasswordField


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

16 package org.jmanage.core.util;
17
18 import java.io.*;
19 import java.util.Arrays JavaDoc;
20
21 /**
22  * This class prompts the user for a password and attempts to mask input with ""
23  */

24
25 public class PasswordField {
26
27     /**
28      *@param prompt The prompt to display to the user.
29      *@return The password as entered by the user.
30      */

31     public static char[] getPassword(String JavaDoc prompt) throws IOException {
32         /* password holder */
33         char[] password = new char[1000];
34         MaskingThread maskingthread = new MaskingThread(prompt);
35         /* block until enter is pressed */
36         Thread JavaDoc thread = new Thread JavaDoc(maskingthread);
37         thread.start();
38         int index = 0;
39         for(; true; index++) {
40             char c = (char) System.in.read();
41             // assume enter pressed, stop masking
42
maskingthread.stopMasking();
43
44             if (c == '\r') {
45                 c = (char) System.in.read();
46                 if (c == '\n') {
47                     break;
48                 } else {
49                     continue;
50                 }
51             } else if (c == '\n') {
52                 break;
53             } else {
54                 /* store the password */
55                 password[index] = c;
56             }
57         }
58         char[] passwordEntered = new char[index];
59         for(int i=0; i<passwordEntered.length; i++){
60             passwordEntered[i] = password[i];
61         }
62         /* clear password */
63         Arrays.fill(password, '\0');
64
65         return passwordEntered;
66     }
67
68     /**
69      * This class attempts to erase characters echoed to the console.
70      */

71     private static class MaskingThread extends Thread JavaDoc {
72         private boolean stop = false;
73         private String JavaDoc prompt;
74
75         /**
76          *@param prompt The prompt displayed to the user
77          */

78         public MaskingThread(String JavaDoc prompt) {
79             this.prompt = prompt;
80         }
81
82
83         /**
84          * Begin masking until asked to stop.
85          */

86         public void run() {
87             while (!stop) {
88                 try {
89                     // attempt masking at this rate
90
this.sleep(1);
91                 } catch (InterruptedException JavaDoc iex) {
92                     iex.printStackTrace();
93                 }
94                 if (!stop) {
95                     System.out.print("\r" + prompt + " \r" + prompt);
96                 }
97                 System.out.flush();
98             }
99         }
100
101
102         /**
103          * Instruct the thread to stop masking.
104          */

105         public void stopMasking() {
106             this.stop = true;
107         }
108     }
109 }
110
111
Popular Tags