001    /**
002     *  Copyright 2003-2006 The Apache Software Foundation
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    
017    package org.apache.geronimo.deployment.cli;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.io.Writer;
022    import java.io.OutputStream;
023    import java.io.PrintWriter;
024    import java.io.OutputStreamWriter;
025    
026    import jline.ConsoleReader;
027    
028    /**
029     * Prompts a user for input; optionally masking.
030     *
031     * <p>
032     * Uses the <a href="http://jline.sf.net">JLine</a> library to provide a rich user experence.
033     *
034     * @version $Rev: 430136 $ $Date: 2006-08-09 13:21:52 -0700 (Wed, 09 Aug 2006) $
035     */
036    public class InputPrompt
037    {
038        private ConsoleReader reader;
039    
040        public InputPrompt(final InputStream in, final Writer out) throws IOException {
041            this.reader = new ConsoleReader(in, out);
042        }
043    
044        public InputPrompt(final InputStream in, final OutputStream out) throws IOException {
045            this(in, new PrintWriter(new OutputStreamWriter(out), true));
046        }
047    
048        /**
049         * Displays the prompt, grabs the input.
050         */
051        public String getInput(final String prompt, final Character mask) throws IOException {
052            if (mask == null) {
053                return reader.readLine(prompt);
054            }
055            else {
056                return reader.readLine(prompt, mask);
057            }
058        }
059    
060        public String getInput(final String prompt, final char mask) throws IOException {
061            return getInput(prompt, new Character(mask));
062        }
063    
064        public String getInput(final String prompt) throws IOException {
065            return getInput(prompt, null);
066        }
067    
068        /**
069         * Displays the prompt, grabs the input masking with '*'.
070         */
071        public String getPassword(final String prompt) throws IOException {
072            return getInput(prompt, '*');
073        }
074    }