KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > plugins > ConsolePassword


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.plugins;
8
9 import java.io.IOException JavaDoc;
10 import java.io.CharArrayWriter JavaDoc;
11
12 /** Read a password from the System.in stream. This may be used as a
13  password accessor in conjunction with the JaasSecurityDomain
14  {CLASS}org.jboss.security.plugins.ConsolePassword
15  format of the KeyStorePass attribute.
16
17  @author Scott.Stark@jboss.org
18  @version $Revison:$
19  */

20 public class ConsolePassword
21 {
22    public ConsolePassword()
23    {
24    }
25
26    public char[] toCharArray()
27       throws IOException JavaDoc
28    {
29       System.err.print("Enter password: ");
30       CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
31       int b;
32       while( (b = System.in.read()) >= 0 )
33       {
34          if( b == '\r' || b == '\n' )
35             break;
36          writer.write(b);
37       }
38       char[] password = writer.toCharArray();
39       writer.reset();
40       for(int n = 0; n < password.length; n ++)
41          writer.write('\0');
42       return password;
43    }
44
45    public static void main(String JavaDoc[] args) throws IOException JavaDoc
46    {
47       ConsolePassword cp = new ConsolePassword();
48       System.out.println(new String JavaDoc(cp.toCharArray()));
49    }
50 }
51
Popular Tags