001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.cli;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.Writer;
023    import java.io.OutputStream;
024    import java.io.PrintWriter;
025    import java.io.OutputStreamWriter;
026    
027    import jline.ConsoleReader;
028    
029    /**
030     * Prompts a user for input; optionally masking.
031     *
032     * <p>
033     * Uses the <a href="http://jline.sf.net">JLine</a> library to provide a rich user experence.
034     *
035     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
036     */
037    public class InputPrompt
038    {
039        private ConsoleReader reader;
040    
041        public InputPrompt(final InputStream in, final Writer out) throws IOException {
042            this.reader = new ConsoleReader(in, out);
043        }
044    
045        public InputPrompt(final InputStream in, final OutputStream out) throws IOException {
046            this(in, new PrintWriter(new OutputStreamWriter(out), true));
047        }
048    
049        /**
050         * Displays the prompt, grabs the input.
051         */
052        public String getInput(final String prompt, final Character mask) throws IOException {
053            if (mask == null) {
054                return reader.readLine(prompt);
055            }
056            else {
057                return reader.readLine(prompt, mask);
058            }
059        }
060    
061        public String getInput(final String prompt, final char mask) throws IOException {
062            return getInput(prompt, new Character(mask));
063        }
064    
065        public String getInput(final String prompt) throws IOException {
066            return getInput(prompt, null);
067        }
068    
069        /**
070         * Displays the prompt, grabs the input masking with '*'.
071         */
072        public String getPassword(final String prompt) throws IOException {
073            return getInput(prompt, '*');
074        }
075    }