KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jaas > JassCredentialCallbackHandler


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

18 package org.apache.activemq.jaas;
19
20 import javax.security.auth.callback.Callback JavaDoc;
21 import javax.security.auth.callback.CallbackHandler JavaDoc;
22 import javax.security.auth.callback.NameCallback JavaDoc;
23 import javax.security.auth.callback.PasswordCallback JavaDoc;
24 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
25 import java.io.IOException JavaDoc;
26
27
28 /**
29  * A JASS username password CallbackHandler.
30  */

31 public class JassCredentialCallbackHandler implements CallbackHandler JavaDoc {
32
33     private final String JavaDoc username;
34     private final String JavaDoc password;
35
36     public JassCredentialCallbackHandler(String JavaDoc username, String JavaDoc password) {
37         this.username = username;
38         this.password = password;
39     }
40
41     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
42         for (int i = 0; i < callbacks.length; i++) {
43             Callback JavaDoc callback = callbacks[i];
44             if (callback instanceof PasswordCallback JavaDoc) {
45                 PasswordCallback JavaDoc passwordCallback = (PasswordCallback JavaDoc) callback;
46                 if (password == null) {
47                     passwordCallback.setPassword(null);
48                 }
49                 else {
50                     passwordCallback.setPassword(password.toCharArray());
51                 }
52             } else if (callback instanceof NameCallback JavaDoc) {
53                 NameCallback JavaDoc nameCallback = (NameCallback JavaDoc) callback;
54                 if (username == null) {
55                     nameCallback.setName(null);
56                 }
57                 else {
58                     nameCallback.setName(username);
59                 }
60             }
61         }
62     }
63 }
64
Popular Tags