/*
 * Copyright (C) 2006 NTT DATA Corporation
 * 
 */
package org.postgresforest.tool.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

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



public class InputShadowPassword implements CallbackHandler{

	public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
		for (int i = 0; i < callbacks.length; i++) {
			if(callbacks[i] instanceof PasswordCallback) {
				PasswordCallback pc = (PasswordCallback)callbacks[i];
				System.out.println(pc.getPrompt());
				System.err.flush();
				pc.setPassword(readPassword(System.in));
			}
		}
	}
	
	 // Reads user password from given input stream.
	 private char[] readPassword(InputStream in) throws IOException {
		 LineNumberReader line = new LineNumberReader(new InputStreamReader(in));
		 char[] password = line.readLine().toCharArray();		 
		 return password;
	 }
}

