/**
 * @version $Id: JaasCallbackHandler.java 4496 2006-02-08 20:27:04Z wehrens $
 */
package org.gridlab.gridsphere.services.core.security.auth.modules.impl;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.gridlab.gridsphere.portlet.PortletLog;
import org.gridlab.gridsphere.portlet.impl.SportletLog;

public class JaasCallbackHandler implements CallbackHandler {

    protected String username = null;
    protected String password = null;

    private PortletLog log = SportletLog.getInstance(JaasCallbackHandler.class);

    public JaasCallbackHandler() {}
    public JaasCallbackHandler(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void handle(Callback callbacks[])
        throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                if (log.isDebugEnabled()) log.debug("responding to NameCallback");
                ((NameCallback) callbacks[i]).setName(username);
            } else if (callbacks[i] instanceof PasswordCallback) {
                if (log.isDebugEnabled()) log.debug("responding to PasswordCallback");
                ((PasswordCallback) callbacks[i]).setPassword(password != null ? password.toCharArray() : new char[0]);
            } else {
                if (log.isDebugEnabled()) log.debug("unsupported callback: " + callbacks[i].getClass());
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
    }

}
