KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > security > 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.ejb3.test.security;
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 import org.jboss.logging.Logger;
33
34 /** An implemeentation of the JAAS CallbackHandler interface that handles
35  NameCallbacks, PasswordCallbac, TextInputCallback and the JBoss
36  ByteArrayCallback
37
38  @author Scott.Stark@jboss.org
39  @version $Revision: 58110 $
40  */

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