1
16package org.tp23.antinstaller.renderer.text;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.PrintStream;
21import java.util.ResourceBundle;
22
23import org.tp23.antinstaller.InstallerContext;
24import org.tp23.antinstaller.input.OutputField;
25import org.tp23.antinstaller.input.PasswordTextInput;
26
27
28public class PasswordTextInputRenderer extends ValidatedTextInputRenderer
29
30 implements TextOutputFieldRenderer {
31
32 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
33
34 public PasswordTextInputRenderer() {
35 }
36
37 public void setContext(InstallerContext ctx) {
38 this.ctx = ctx;
39 }
40
41 public void renderOutput(OutputField field, BufferedReader reader, PrintStream out) throws IOException {
42 PasswordTextInput iField = (PasswordTextInput) field;
43 StringBuffer displayText = new StringBuffer();
44 displayText.append(field.getDisplayText());
45 displayText.append(" [");
46 displayText.append(res.getString("_default_"));
47 displayText.append(":");
48 displayText.append(iField.getDefaultValue());
49 displayText.append("]");
50
51 String input = null;
52 if(OutputField.isTrue(iField.getTextMask())){
53 input = new PasswordField().getPassword(displayText.toString());
54 System.out.print("\r ");
55 }
56 else {
57 out.println(displayText.toString());
58 input = reader.readLine();
59 }
60
61 out.println();
62 out.println();
63 if(input == null || input.equals("")){
64 input = iField.getDefaultValue();
65 }
66 iField.setInputResult(input);
67 }
68 public void renderError(OutputField field, BufferedReader reader, PrintStream out) throws IOException{
69 out.println(getErrorMessage());
70 renderOutput(field, reader, out);
71 }
72 public boolean isAbort(){
73 return false;
74 }
75
76 protected String getErrorMessage(){
77 return res.getString("notCorrectPasswordFormat");
78 }
79
80
94
00 class MaskingThread extends Thread {
01 private boolean stop = false;
02 private int index;
03 private String prompt;
04
05 public MaskingThread(String prompt) {
06 this.prompt = prompt;
07 }
08
09 public void run() {
10 while (!stop) {
11 try {
12 this.sleep(1);
14 }
15 catch (InterruptedException iex) {
16 iex.printStackTrace();
17 }
18 if (!stop) {
19 System.out.print("\r" + prompt + " \r" + prompt);
20 }
21 System.out.flush();
22 }
23 }
24
25 public void stopMasking() {
26 this.stop = true;
27 }
28 }
29
30 public class PasswordField {
31
32
36 String getPassword(String prompt) throws IOException {
37 StringBuffer password = new StringBuffer();
39 MaskingThread maskingthread = new MaskingThread(prompt);
40 Thread thread = new Thread(maskingthread);
41 thread.start();
42 while (true) {
44 char c = (char) System.in.read();
45 maskingthread.stopMasking();
47 if (c == '\r') {
48 c = (char) System.in.read();
49 if (c == '\n') {
50 break;
51 }
52 else {
53 continue;
54 }
55 }
56 else if (c == '\n') {
57 break;
58 }
59 else {
60 password.append(c);
62 }
63 }
64 return password.toString();
65 }
66 }
67}
68