KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > callback > ConsoleInputHandler


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

7 package org.jboss.security.auth.callback;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import javax.security.auth.callback.Callback JavaDoc;
13 import javax.security.auth.callback.CallbackHandler JavaDoc;
14 import javax.security.auth.callback.NameCallback JavaDoc;
15 import javax.security.auth.callback.PasswordCallback JavaDoc;
16 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
17
18 /** An implementation of CallbackHandler that obtains the values for
19  NameCallback and PasswordCallback from the console.
20
21  @see javax.security.auth.callback.CallbackHandler
22  @see #handle(Callback[])
23
24  @author Scott.Stark@jboss.org
25  @version $Revision: 1.3 $
26  */

27 public class ConsoleInputHandler implements CallbackHandler JavaDoc
28 {
29    /** Sets any NameCallback name property to the instance username,
30     sets any PasswordCallback password property to the instance, and any
31     password.
32     @exception UnsupportedCallbackException, thrown if any callback of
33     type other than NameCallback or PasswordCallback are seen.
34     */

35    public void handle(Callback JavaDoc[] callbacks) throws
36          UnsupportedCallbackException JavaDoc
37    {
38       for (int i = 0; i < callbacks.length; i++)
39       {
40          Callback JavaDoc c = callbacks[i];
41          if (c instanceof NameCallback JavaDoc)
42          {
43             NameCallback JavaDoc nc = (NameCallback JavaDoc) c;
44             String JavaDoc prompt = nc.getPrompt();
45             if( prompt == null )
46                prompt = "Enter Username: ";
47             // Prompt the user for the username
48
System.out.print(prompt);
49             InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(System.in);
50             BufferedReader JavaDoc br = new BufferedReader JavaDoc(isr);
51             try
52             {
53                String JavaDoc username = br.readLine();
54                nc.setName(username);
55             }
56             catch(IOException JavaDoc e)
57             {
58                throw new SecurityException JavaDoc("Failed to obtain username, ioe="+e.getMessage());
59             }
60          }
61          else if (c instanceof PasswordCallback JavaDoc)
62          {
63             PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) c;
64             String JavaDoc prompt = pc.getPrompt();
65             if( prompt == null )
66                prompt = "Enter Password: ";
67             // Prompt the user for the username
68
System.out.print(prompt);
69             InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(System.in);
70             BufferedReader JavaDoc br = new BufferedReader JavaDoc(isr);
71             try
72             {
73                String JavaDoc password = br.readLine();
74                pc.setPassword(password.toCharArray());
75             }
76             catch(IOException JavaDoc e)
77             {
78                throw new SecurityException JavaDoc("Failed to obtain password, ioe="+e.getMessage());
79             }
80          }
81          else
82          {
83             throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
84          }
85       }
86    }
87 }
88
Popular Tags