KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > audit > interfaces > ApplicationCallbackHandler


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.cmp2.audit.interfaces;
23
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29 import javax.security.auth.login.LoginContext JavaDoc;
30 import javax.security.auth.login.LoginException JavaDoc;
31
32 /**
33  * A callback handler for login with user and password.
34  *
35  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
36  * @version $Revision: 42492 $
37  */

38 public class ApplicationCallbackHandler
39    implements CallbackHandler JavaDoc
40 {
41    // Attributes ----------------------------------------------------
42

43    private String JavaDoc user;
44    private char[] password;
45
46    // Constructor ---------------------------------------------------
47

48    private ApplicationCallbackHandler(String JavaDoc user, String JavaDoc password)
49    {
50       this.user = user;
51       this.password = password.toCharArray();
52    }
53
54    // Public --------------------------------------------------------
55

56    public void handle(Callback JavaDoc[] callbacks)
57       throws UnsupportedCallbackException JavaDoc
58    {
59       for (int i = 0; i < callbacks.length; i++)
60       {
61          if (callbacks[i] instanceof NameCallback JavaDoc)
62          {
63             NameCallback JavaDoc nameCallback = (NameCallback JavaDoc) callbacks[i];
64             nameCallback.setName(user);
65          }
66          else if (callbacks[i] instanceof PasswordCallback JavaDoc)
67          {
68             PasswordCallback JavaDoc passwordCallback = (PasswordCallback JavaDoc) callbacks[i];
69             passwordCallback.setPassword(password);
70          }
71          else
72          {
73             throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unsupported callback");
74          }
75       }
76    }
77
78    // Static --------------------------------------------------------
79

80    /**
81     * Login using the "client-login" config
82     */

83    public static LoginContext JavaDoc login(String JavaDoc user, String JavaDoc password)
84       throws LoginException JavaDoc
85    {
86       return login("client-login", user, password);
87    }
88
89    /**
90     * Login using specific login configuration
91     * @param config - name of login config to use
92     * @param user
93     * @param password
94     * @return
95     * @throws LoginException
96     */

97    public static LoginContext JavaDoc login(String JavaDoc config, String JavaDoc user, String JavaDoc password)
98       throws LoginException JavaDoc
99    {
100       ApplicationCallbackHandler handler = new ApplicationCallbackHandler(user, password);
101       LoginContext JavaDoc result = new LoginContext JavaDoc(config, handler);
102       result.login();
103       return result;
104    }
105 }
106
Popular Tags