001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.text;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.PrintStream;
021:
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.input.OutputField;
024: import org.tp23.antinstaller.input.PasswordTextInput;
025: import org.tp23.antinstaller.renderer.AIResourceBundle;
026:
027: public class PasswordTextInputRenderer extends
028: ValidatedTextInputRenderer
029:
030: implements TextOutputFieldRenderer {
031:
032: private static final AIResourceBundle res = new AIResourceBundle();
033:
034: public PasswordTextInputRenderer() {
035: }
036:
037: public void setContext(InstallerContext ctx) {
038: this .ctx = ctx;
039: }
040:
041: public void renderOutput(OutputField field, BufferedReader reader,
042: PrintStream out) throws IOException {
043: PasswordTextInput iField = (PasswordTextInput) field;
044: StringBuffer displayText = new StringBuffer();
045: displayText.append(field.getDisplayText());
046: displayText.append(" [");
047: displayText.append(res.getString("selection.default"));
048: displayText.append(":");
049: displayText.append(iField.getDefaultValue());
050: displayText.append("]");
051:
052: String input = null;
053: if (OutputField.isTrue(iField.getTextMask())) {
054: input = new PasswordField().getPassword(displayText
055: .toString());
056: System.out
057: .print("\r ");
058: } else {
059: out.println(displayText.toString());
060: input = reader.readLine();
061: }
062:
063: out.println();
064: out.println();
065: if (input == null || input.equals("")) {
066: input = iField.getDefaultValue();
067: }
068: iField.setInputResult(input);
069: }
070:
071: public void renderError(OutputField field, BufferedReader reader,
072: PrintStream out) throws IOException {
073: out.println(getErrorMessage());
074: renderOutput(field, reader, out);
075: }
076:
077: public boolean isAbort() {
078: return false;
079: }
080:
081: protected String getErrorMessage() {
082: return res.getString("not.correct.password.format");
083: }
084:
085: // shame this does not work
086: // does any one know a way to not echo passwords?
087: // private String readInput(InputStreamReader reader, PrintStream out) throws IOException{
088: // StringBuffer sb = new StringBuffer();
089: // char c = 0;
090: // while((c=(char)reader.read())!='\n'){
091: // if(c==8)sb.setLength(sb.length()-1);
092: // sb.append(c);
093: // out.print((char)8);
094: // out.flush();
095: // }
096: // return sb.toString();
097: // }
098:
099: /*
100: *
101: * Taken from the SUN website
102: * @author Paul Hinds
103: * @version $Id: PasswordTextInputRenderer.java,v 1.4 2007/01/04 22:57:18 teknopaul Exp $
104: */
105: class MaskingThread extends Thread {
106: private boolean stop = false;
107: private int index;
108: private String prompt;
109:
110: public MaskingThread(String prompt) {
111: this .prompt = prompt;
112: }
113:
114: public void run() {
115: while (!stop) {
116: try {
117: // attempt masking at this rate
118: this .sleep(1);
119: } catch (InterruptedException iex) {
120: iex.printStackTrace();
121: }
122: if (!stop) {
123: System.out.print("\r" + prompt + " \r" + prompt);
124: }
125: System.out.flush();
126: }
127: }
128:
129: public void stopMasking() {
130: this .stop = true;
131: }
132: }
133:
134: public class PasswordField {
135:
136: /**
137: *@param prompt The prompt to display to the user.
138: *@return The password as entered by the user.
139: */
140: String getPassword(String prompt) throws IOException {
141: // password holder
142: StringBuffer password = new StringBuffer();
143: MaskingThread maskingthread = new MaskingThread(prompt);
144: Thread thread = new Thread(maskingthread);
145: thread.start();
146: // block until enter is pressed
147: while (true) {
148: char c = (char) System.in.read();
149: // assume enter pressed, stop masking
150: maskingthread.stopMasking();
151: if (c == '\r') {
152: c = (char) System.in.read();
153: if (c == '\n') {
154: break;
155: } else {
156: continue;
157: }
158: } else if (c == '\n') {
159: break;
160: } else {
161: // store the password
162: password.append(c);
163: }
164: }
165: return password.toString();
166: }
167: }
168: }
|