001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import java.io.Serializable;
018    
019    import javax.security.auth.callback.Callback;
020    import javax.security.auth.callback.CallbackHandler;
021    import javax.security.auth.callback.NameCallback;
022    import javax.security.auth.callback.PasswordCallback;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PortalCallbackHandler implements CallbackHandler, Serializable {
028    
029            public PortalCallbackHandler(String name, String password) {
030                    _name = name;
031                    _password = password;
032            }
033    
034            public void handle(Callback[] callbacks) {
035                    for (int i = 0; i < callbacks.length; i++) {
036                            if (callbacks[i] instanceof NameCallback) {
037                                    NameCallback nameCallback = (NameCallback)callbacks[i];
038                                    nameCallback.setName(_name);
039                            }
040                            else if (callbacks[i] instanceof PasswordCallback) {
041                                    PasswordCallback passCallback = (PasswordCallback)callbacks[i];
042                                    passCallback.setPassword(_password.toCharArray());
043                            }
044                    }
045            }
046    
047            private String _name;
048            private String _password;
049    
050    }