KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > demo > sas > JaasTxtCalbackHandler


1 package org.jacorb.demo.sas;
2
3 import javax.security.auth.callback.Callback JavaDoc;
4 import javax.security.auth.callback.CallbackHandler JavaDoc;
5 import javax.security.auth.callback.NameCallback JavaDoc;
6 import javax.security.auth.callback.PasswordCallback JavaDoc;
7 import javax.security.auth.callback.TextOutputCallback JavaDoc;
8 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
9
10 public class JaasTxtCalbackHandler implements CallbackHandler JavaDoc {
11
12     private String JavaDoc myUsername = "";
13     private char[] myPassword = null;
14
15     /**
16      * <p>Creates a callback handler that prompts and reads from the
17      * command line for answers to authentication questions.
18      * This can be used by JAAS applications to instantiate a
19      * CallbackHandler.
20
21     */

22     public JaasTxtCalbackHandler() {
23     }
24
25     public void setMyUsername(String JavaDoc username) {
26         myUsername = username;
27     }
28
29     public void setMyPassword(char[] password) {
30         myPassword = password;
31     }
32
33     /**
34      * Handles the specified set of callbacks.
35      *
36      * @param callbacks the callbacks to handle
37      * @throws IOException if an input or output error occurs.
38      * @throws UnsupportedCallbackException if the callback is not an
39      * instance of NameCallback or PasswordCallback
40      */

41     public void handle(Callback JavaDoc[] callbacks) throws UnsupportedCallbackException JavaDoc {
42
43         for (int i = 0; i < callbacks.length; i++) {
44             if (callbacks[i] instanceof TextOutputCallback JavaDoc) {
45                 TextOutputCallback JavaDoc tc = (TextOutputCallback JavaDoc) callbacks[i];
46
47                 String JavaDoc text;
48                 switch (tc.getMessageType()) {
49                     case TextOutputCallback.INFORMATION :
50                     text = "";
51                     break;
52                     case TextOutputCallback.WARNING :
53                     text = "Warning: ";
54                     break;
55                     case TextOutputCallback.ERROR :
56                     text = "Error: ";
57                     break;
58                     default :
59                     throw new UnsupportedCallbackException JavaDoc(
60                         callbacks[i],
61                         "Unrecognized message type");
62                 }
63
64                 String JavaDoc message = tc.getMessage();
65                 if (message != null) {
66                     text += message;
67                 }
68                 if (text != null) {
69                     System.err.println(text);
70                 }
71
72             } else if (callbacks[i] instanceof NameCallback JavaDoc) {
73                 NameCallback JavaDoc nc = (NameCallback JavaDoc) callbacks[i];
74                 nc.setName(myUsername);
75
76             } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
77                 PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) callbacks[i];
78                 pc.setPassword(myPassword);
79
80             } else {
81                 throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
82             }
83         }
84     }
85
86 }
87
Popular Tags