1 23 24 package com.sun.enterprise.cli.commands; 25 26 import java.io.*; 27 import com.sun.enterprise.cli.framework.InputsAndOutputs; 28 29 36 public class CommandUtil 37 { 38 39 44 public String getPassword (String prompt) throws IOException 45 { 46 String password = ""; 48 final MaskingThread maskingThread = new MaskingThread(prompt); 49 final Thread thread = new Thread (maskingThread); 50 thread.start(); 51 while (true) 53 { 54 char ch = (char)InputsAndOutputs.getInstance().getUserInput().getChar(); 55 57 maskingThread.stopMasking(); 59 60 if (ch == '\r') break; 61 else if (ch == '\n') break; 62 else 63 { 64 password += ch; 66 } 67 } 68 InputsAndOutputs.getInstance().getUserOutput().println(""); 69 return password; 70 } 71 72 73 public static void main(String argv[]) 74 { 75 CommandUtil passfield = new CommandUtil(); 76 String 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 94 class MaskingThread extends Thread 95 { 96 private boolean stop = false; 97 private int index; 98 private String prompt; 99 100 104 public MaskingThread(String prompt) 105 { 106 this.prompt = prompt; 107 } 108 109 112 public void run() 113 { 114 while (!stop) 115 { 116 try 117 { 118 this.sleep(1); 120 } 121 catch (InterruptedException iex) 122 { 123 iex.printStackTrace(); 124 } 125 if (!stop) 126 { 127 InputsAndOutputs.getInstance().getUserOutput().print(" "); 128 InputsAndOutputs.getInstance().getUserOutput().print( "\r" + prompt ); 129 } 132 InputsAndOutputs.getInstance().getUserOutput().flush(); 133 } 135 } 136 137 140 public void stopMasking() 141 { 142 this.stop = true; 143 } 144 } 145 | Popular Tags |