View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 30-mar-2004
21   *
22   * Copyright (c)2004 Grid Systems
23   */
24  package com.gridsystems.config.tools.console;
25  
26  import java.io.IOException;
27  import java.text.MessageFormat;
28  import java.util.ResourceBundle;
29  
30  /**
31   * A password entry field.
32   *
33   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
34   * @version 1.0
35   */
36  public class PasswordField extends ValueField {
37  
38    /**
39     * KernelConfigurator Bundle.
40     */
41    private static ResourceBundle bundle = ResourceBundle.getBundle("config");
42  
43    /**
44     * Creates a field of password type.
45     *
46     * @param bundle The resource bundle
47     * @param linePattern The line pattern for contents construction
48     */
49    public PasswordField(ResourceBundle bundle, String linePattern) {
50      super(bundle, linePattern, null);
51    }
52  
53    /**
54     * {@inheritDoc}
55     */
56    public String[] getContents() {
57      String[] contents = new String[1];
58  
59      // Constructs the actual line pattern
60      StringBuffer pattern = new StringBuffer();
61      String key = getKeyMapping();
62      if (key != null) {
63        pattern.append(" ").append(key).append(". ");
64      }
65  
66      pattern.append(getString(linePattern));
67  
68      String s = getStringValue();
69      int count = s.length();
70      s = "*********************************************".substring(0, count);
71  
72      contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
73      return contents;
74    }
75  
76    /**
77     * Reads a line from the console, and prevents casual viewers from reading it by
78     * masking the echoed characters.
79     *
80     * {@inheritDoc}
81     */
82    protected String readLine() throws IOException {
83      String prompt;
84  
85      do {
86        prompt = bundle.getString("console.typepassword");
87        String pass1 = ConsoleTools.readPassword(prompt, '*');
88        prompt = bundle.getString("console.retypepassword");
89        String pass2 = ConsoleTools.readPassword(prompt, '*');
90  
91        if (pass1.equals(pass2)) {
92          return pass1;
93        } else {
94          System.out.println("ERROR: Passwords do not match");
95        }
96      } while (true);
97    }
98  
99  }