source: release-kits/shared/ant-installer/src/org/tp23/antinstaller/renderer/text/PasswordTextInputRenderer.java@ 17514

Last change on this file since 17514 was 17514, checked in by oranfry, 16 years ago

changes to the way ant-installer loads and reloads the language packs, and a new attribute to the select input which triggers it to change the language to the input value

File size: 4.3 KB
Line 
1/*
2 * Copyright 2005 Paul Hinds
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 */
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 public PasswordTextInputRenderer() {
33 }
34
35 public void setContext(InstallerContext ctx) {
36 this.ctx = ctx;
37 }
38
39 public void renderOutput(OutputField field, BufferedReader reader, PrintStream out) throws IOException {
40 PasswordTextInput iField = (PasswordTextInput) field;
41 StringBuffer displayText = new StringBuffer();
42 displayText.append(field.getDisplayText());
43 displayText.append(" [");
44 displayText.append(org.tp23.antinstaller.Installer.langPack.getString("_default_"));
45 displayText.append(":");
46 displayText.append(iField.getDefaultValue());
47 displayText.append("]");
48
49 String input = null;
50 if(OutputField.isTrue(iField.getTextMask())){
51 input = new PasswordField().getPassword(displayText.toString());
52 System.out.print("\r ");
53 }
54 else {
55 out.println(displayText.toString());
56 input = reader.readLine();
57 }
58
59 out.println();
60 out.println();
61 if(input == null || input.equals("")){
62 input = iField.getDefaultValue();
63 }
64 iField.setInputResult(input);
65 }
66 public void renderError(OutputField field, BufferedReader reader, PrintStream out) throws IOException{
67 out.println(getErrorMessage());
68 renderOutput(field, reader, out);
69 }
70 public boolean isAbort(){
71 return false;
72 }
73
74 protected String getErrorMessage(){
75 return org.tp23.antinstaller.Installer.langPack.getString("notCorrectPasswordFormat");
76 }
77
78 // shame this does not work
79 // does any one know a way to not echo passwords?
80// private String readInput(InputStreamReader reader, PrintStream out) throws IOException{
81// StringBuffer sb = new StringBuffer();
82// char c = 0;
83// while((c=(char)reader.read())!='\n'){
84// if(c==8)sb.setLength(sb.length()-1);
85// sb.append(c);
86// out.print((char)8);
87// out.flush();
88// }
89// return sb.toString();
90// }
91
92 /*
93 *
94 * Taken from the SUN website
95 * @author Paul Hinds
96 * @version $Id: PasswordTextInputRenderer.java,v 1.4 2007/01/04 22:57:18 teknopaul Exp $
97 */
98 class MaskingThread extends Thread {
99 private boolean stop = false;
100 private int index;
101 private String prompt;
102
103 public MaskingThread(String prompt) {
104 this.prompt = prompt;
105 }
106
107 public void run() {
108 while (!stop) {
109 try {
110 // attempt masking at this rate
111 this.sleep(1);
112 }
113 catch (InterruptedException iex) {
114 iex.printStackTrace();
115 }
116 if (!stop) {
117 System.out.print("\r" + prompt + " \r" + prompt);
118 }
119 System.out.flush();
120 }
121 }
122
123 public void stopMasking() {
124 this.stop = true;
125 }
126 }
127
128 public class PasswordField {
129
130 /**
131 *@param prompt The prompt to display to the user.
132 *@return The password as entered by the user.
133 */
134 String getPassword(String prompt) throws IOException {
135 // password holder
136 StringBuffer password = new StringBuffer();
137 MaskingThread maskingthread = new MaskingThread(prompt);
138 Thread thread = new Thread(maskingthread);
139 thread.start();
140 // block until enter is pressed
141 while (true) {
142 char c = (char) System.in.read();
143 // assume enter pressed, stop masking
144 maskingthread.stopMasking();
145 if (c == '\r') {
146 c = (char) System.in.read();
147 if (c == '\n') {
148 break;
149 }
150 else {
151 continue;
152 }
153 }
154 else if (c == '\n') {
155 break;
156 }
157 else {
158 // store the password
159 password.append(c);
160 }
161 }
162 return password.toString();
163 }
164 }
165}
Note: See TracBrowser for help on using the repository browser.