KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > auth > LoginCallbackHandler


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.auth;
17
18 import javax.security.auth.callback.*;
19 import java.io.IOException JavaDoc;
20
21 /**
22  * Date : Jun 27, 2004 1:34:02 PM
23  * @author Shashank
24  */

25 public class LoginCallbackHandler implements CallbackHandler {
26     private String JavaDoc username;
27     private String JavaDoc password;
28
29     public LoginCallbackHandler(String JavaDoc username, String JavaDoc password) {
30         this.username = username;
31         this.password = password;
32     }
33
34     public String JavaDoc getUsername() {
35         return username;
36     }
37
38     public String JavaDoc getPassword() {
39         return password;
40     }
41
42     /**
43      *
44      * @param callbacks
45      * @throws IOException
46      * @throws UnsupportedCallbackException
47      */

48     public void handle(Callback[] callbacks) throws IOException JavaDoc,
49             UnsupportedCallbackException {
50
51         for (int i = 0; i < callbacks.length; i++) {
52             if (callbacks[i] instanceof NameCallback) {
53                 NameCallback nc = (NameCallback)callbacks[i];
54                 nc.setName(username);
55             }else if(callbacks[i] instanceof PasswordCallback){
56                 PasswordCallback pc = (PasswordCallback)callbacks[i];
57                 pc.setPassword(password.toCharArray());
58             } else {
59                 throw(new UnsupportedCallbackException(callbacks[i],
60                         "Callback handler not supported"));
61             }
62         }
63     }
64 }
Popular Tags