KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > CommandUtil


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 com.sun.enterprise.cli.commands;
25
26 import java.io.*;
27 import com.sun.enterprise.cli.framework.InputsAndOutputs;
28
29 /**
30  * This class contains useful api used for the commands module.
31  * These methods are:
32  * getPassword() - this method prompts the user for a password and attempts
33  * to mask inputs with "".
34  * @version $Revision: 1.3 $
35  */

36 public class CommandUtil
37 {
38
39     /**
40      * This method prompts the user to enter the password and attempts
41      * to mask inputs with "".
42      * @param prompt - prompt to display on the output device.
43      */

44     public String JavaDoc getPassword (String JavaDoc prompt) throws IOException
45     {
46     //password holder
47
String JavaDoc password = "";
48     final MaskingThread maskingThread = new MaskingThread(prompt);
49     final Thread JavaDoc thread = new Thread JavaDoc(maskingThread);
50     thread.start();
51     //block until enter is pressed;
52
while (true)
53     {
54         char ch = (char)InputsAndOutputs.getInstance().getUserInput().getChar();
55         //char ch = (char)System.in.read();
56

57         //assume enter pressed, stop masking
58
maskingThread.stopMasking();
59
60         if (ch == '\r') break;
61         else if (ch == '\n') break;
62         else
63             {
64         //store the password
65
password += ch;
66         }
67     }
68     InputsAndOutputs.getInstance().getUserOutput().println("");
69     return password;
70     }
71
72
73     public static void main(String JavaDoc argv[])
74     {
75     CommandUtil passfield = new CommandUtil();
76     String JavaDoc password = null;
77     try
78     {
79         password = passfield.getPassword("Enter your password: " );
80     }
81     catch (IOException ioe)
82     {
83         ioe.printStackTrace();
84     }
85     System.out.println("The password entered is " + password);
86     }
87 }
88
89
90 /**
91  * This is an inner class used only in CommandUtil.java.
92  * This class attempts to erase characters echoed ot the console.
93  */

94 class MaskingThread extends Thread JavaDoc
95 {
96     private boolean stop = false;
97     private int index;
98     private String JavaDoc prompt;
99
100     /**
101      * Constructor
102      * @param prompt The prompt displayed to the user
103      */

104     public MaskingThread(String JavaDoc prompt)
105     {
106     this.prompt = prompt;
107     }
108
109     /**
110      * Begin masking until asked to stop.
111      */

112     public void run()
113     {
114     while (!stop)
115     {
116         try
117             {
118         //attempt masking at this rate
119
this.sleep(1);
120         }
121         catch (InterruptedException JavaDoc iex)
122             {
123         iex.printStackTrace();
124         }
125         if (!stop)
126         {
127         InputsAndOutputs.getInstance().getUserOutput().print(" ");
128         InputsAndOutputs.getInstance().getUserOutput().print( "\r" + prompt );
129         //System.out.print(" ");
130
//System.out.print("\r" + prompt );
131
}
132         InputsAndOutputs.getInstance().getUserOutput().flush();
133         //System.out.flush();
134
}
135     }
136
137     /**
138      * Instruct the thread to stop masking.
139      */

140     public void stopMasking()
141     {
142     this.stop = true;
143     }
144 }
145
Popular Tags