KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > AppCallbackHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.util;
23
24 import java.io.IOException JavaDoc;
25 import javax.security.auth.callback.Callback JavaDoc;
26 import javax.security.auth.callback.CallbackHandler JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29 import javax.security.auth.callback.TextInputCallback JavaDoc;
30 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
31
32 /** An implemeentation of the JAAS CallbackHandler interface that handles
33  NameCallbacks, PasswordCallbac, TextInputCallback and the JBoss
34  ByteArrayCallback
35
36  @author Scott.Stark@jboss.org
37  @version $Revision: 43776 $
38  */

39 public class AppCallbackHandler implements CallbackHandler JavaDoc
40 {
41    private String JavaDoc username;
42    private char[] password;
43    private byte[] data;
44    private String JavaDoc text;
45
46    public AppCallbackHandler(String JavaDoc username, char[] password)
47    {
48       this.username = username;
49       this.password = password;
50    }
51    public AppCallbackHandler(String JavaDoc username, char[] password, byte[] data)
52    {
53       this.username = username;
54       this.password = password;
55       this.data = data;
56    }
57    public AppCallbackHandler(String JavaDoc username, char[] password, byte[] data, String JavaDoc text)
58    {
59       this.username = username;
60       this.password = password;
61       this.data = data;
62       this.text = text;
63    }
64
65    public void handle(Callback JavaDoc[] callbacks) throws
66          IOException JavaDoc, UnsupportedCallbackException JavaDoc
67    {
68       for (int i = 0; i < callbacks.length; i++)
69       {
70          Callback JavaDoc c = callbacks[i];
71          if( c instanceof NameCallback JavaDoc )
72          {
73             NameCallback JavaDoc nc = (NameCallback JavaDoc) c;
74             nc.setName(username);
75          }
76          else if( c instanceof PasswordCallback JavaDoc )
77          {
78             PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) c;
79             pc.setPassword(password);
80          }
81          else if( c instanceof TextInputCallback JavaDoc )
82          {
83             TextInputCallback JavaDoc tc = (TextInputCallback JavaDoc) c;
84             tc.setText(text);
85          }
86          else if( c instanceof ByteArrayCallback )
87          {
88             ByteArrayCallback bac = (ByteArrayCallback) c;
89             bac.setByteArray(data);
90          }
91          else
92          {
93             throw new UnsupportedCallbackException JavaDoc(c, "Unrecognized Callback");
94          }
95       }
96    }
97 }
98
99
Popular Tags