01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
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: */package org.apache.openejb.client;
18:
19: import javax.security.auth.callback.CallbackHandler;
20: import javax.security.auth.callback.Callback;
21: import javax.security.auth.callback.UnsupportedCallbackException;
22: import javax.security.auth.callback.PasswordCallback;
23: import javax.security.auth.callback.NameCallback;
24: import java.io.IOException;
25:
26: /**
27: * A JASS username password CallbackHandler.
28: */
29: public class UsernamePasswordCallbackHandler implements CallbackHandler {
30:
31: private final String username;
32: private final String password;
33:
34: public UsernamePasswordCallbackHandler(String username,
35: String password) {
36: this .username = username;
37: this .password = password;
38: }
39:
40: public void handle(Callback[] callbacks) throws IOException,
41: UnsupportedCallbackException {
42: for (int i = 0; i < callbacks.length; i++) {
43: Callback callback = callbacks[i];
44: if (callback instanceof PasswordCallback) {
45: PasswordCallback passwordCallback = (PasswordCallback) callback;
46: if (password == null) {
47: passwordCallback.setPassword(null);
48: } else {
49: passwordCallback
50: .setPassword(password.toCharArray());
51: }
52: } else if (callback instanceof NameCallback) {
53: NameCallback nameCallback = (NameCallback) callback;
54: if (username == null) {
55: nameCallback.setName(null);
56: } else {
57: nameCallback.setName(username);
58: }
59: }
60: }
61: }
62: }
|