View Javadoc

1   /**
2    *  Copyright 2003-2006 The Apache Software Foundation
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  
17  package org.apache.geronimo.deployment.cli;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Writer;
22  import java.io.OutputStream;
23  import java.io.PrintWriter;
24  import java.io.OutputStreamWriter;
25  
26  import jline.ConsoleReader;
27  
28  /**
29   * Prompts a user for input; optionally masking.
30   *
31   * <p>
32   * Uses the <a href="http://jline.sf.net">JLine</a> library to provide a rich user experence.
33   *
34   * @version $Rev: 430136 $ $Date: 2006-08-09 13:21:52 -0700 (Wed, 09 Aug 2006) $
35   */
36  public class InputPrompt
37  {
38      private ConsoleReader reader;
39  
40      public InputPrompt(final InputStream in, final Writer out) throws IOException {
41          this.reader = new ConsoleReader(in, out);
42      }
43  
44      public InputPrompt(final InputStream in, final OutputStream out) throws IOException {
45          this(in, new PrintWriter(new OutputStreamWriter(out), true));
46      }
47  
48      /**
49       * Displays the prompt, grabs the input.
50       */
51      public String getInput(final String prompt, final Character mask) throws IOException {
52          if (mask == null) {
53              return reader.readLine(prompt);
54          }
55          else {
56              return reader.readLine(prompt, mask);
57          }
58      }
59  
60      public String getInput(final String prompt, final char mask) throws IOException {
61          return getInput(prompt, new Character(mask));
62      }
63  
64      public String getInput(final String prompt) throws IOException {
65          return getInput(prompt, null);
66      }
67  
68      /**
69       * Displays the prompt, grabs the input masking with '*'.
70       */
71      public String getPassword(final String prompt) throws IOException {
72          return getInput(prompt, '*');
73      }
74  }