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 | */
|
---|
16 | package org.tp23.antinstaller.renderer.text;
|
---|
17 |
|
---|
18 | import java.io.BufferedReader;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.PrintStream;
|
---|
21 | import java.util.ResourceBundle;
|
---|
22 |
|
---|
23 | import org.tp23.antinstaller.InstallerContext;
|
---|
24 | import org.tp23.antinstaller.input.OutputField;
|
---|
25 | import org.tp23.antinstaller.input.PasswordTextInput;
|
---|
26 |
|
---|
27 |
|
---|
28 | public 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 | // shame this does not work
|
---|
81 | // does any one know a way to not echo passwords?
|
---|
82 | // private String readInput(InputStreamReader reader, PrintStream out) throws IOException{
|
---|
83 | // StringBuffer sb = new StringBuffer();
|
---|
84 | // char c = 0;
|
---|
85 | // while((c=(char)reader.read())!='\n'){
|
---|
86 | // if(c==8)sb.setLength(sb.length()-1);
|
---|
87 | // sb.append(c);
|
---|
88 | // out.print((char)8);
|
---|
89 | // out.flush();
|
---|
90 | // }
|
---|
91 | // return sb.toString();
|
---|
92 | // }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | *
|
---|
96 | * Taken from the SUN website
|
---|
97 | * @author Paul Hinds
|
---|
98 | * @version $Id: PasswordTextInputRenderer.java,v 1.4 2007/01/04 22:57:18 teknopaul Exp $
|
---|
99 | */
|
---|
100 | class MaskingThread extends Thread {
|
---|
101 | private boolean stop = false;
|
---|
102 | private int index;
|
---|
103 | private String prompt;
|
---|
104 |
|
---|
105 | public MaskingThread(String prompt) {
|
---|
106 | this.prompt = prompt;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void run() {
|
---|
110 | while (!stop) {
|
---|
111 | try {
|
---|
112 | // attempt masking at this rate
|
---|
113 | this.sleep(1);
|
---|
114 | }
|
---|
115 | catch (InterruptedException iex) {
|
---|
116 | iex.printStackTrace();
|
---|
117 | }
|
---|
118 | if (!stop) {
|
---|
119 | System.out.print("\r" + prompt + " \r" + prompt);
|
---|
120 | }
|
---|
121 | System.out.flush();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void stopMasking() {
|
---|
126 | this.stop = true;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public class PasswordField {
|
---|
131 |
|
---|
132 | /**
|
---|
133 | *@param prompt The prompt to display to the user.
|
---|
134 | *@return The password as entered by the user.
|
---|
135 | */
|
---|
136 | String getPassword(String prompt) throws IOException {
|
---|
137 | // password holder
|
---|
138 | StringBuffer password = new StringBuffer();
|
---|
139 | MaskingThread maskingthread = new MaskingThread(prompt);
|
---|
140 | Thread thread = new Thread(maskingthread);
|
---|
141 | thread.start();
|
---|
142 | // block until enter is pressed
|
---|
143 | while (true) {
|
---|
144 | char c = (char) System.in.read();
|
---|
145 | // assume enter pressed, stop masking
|
---|
146 | maskingthread.stopMasking();
|
---|
147 | if (c == '\r') {
|
---|
148 | c = (char) System.in.read();
|
---|
149 | if (c == '\n') {
|
---|
150 | break;
|
---|
151 | }
|
---|
152 | else {
|
---|
153 | continue;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | else if (c == '\n') {
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | // store the password
|
---|
161 | password.append(c);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | return password.toString();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|