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