001package org.apache.maven.wagon.providers.ssh.jsch.interactive;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import com.jcraft.jsch.UIKeyboardInteractive;
023import org.codehaus.plexus.components.interactivity.Prompter;
024import org.codehaus.plexus.components.interactivity.PrompterException;
025
026/**
027 * UIKeyboardInteractive that use plexus-prompter.
028 * 
029 * <code>UIKeyboardInteractive</code> are usefull when you don't use user with
030 * password authentication with a server that use keyboard-interactive and
031 * doesn't allow password method <code>PasswordAuthentication no</code>.
032 *
033 * @author <a href="mailto:juam at users.sourceforge.net">Juan F. Codagnone</a>
034 * @since Sep 22, 2005
035 * 
036 * @plexus.component role="com.jcraft.jsch.UIKeyboardInteractive" 
037 */
038public class PrompterUIKeyboardInteractive
039    implements UIKeyboardInteractive
040{
041    /**
042     * @plexus.requirement role-hint="default"
043     */
044    private volatile Prompter prompter;
045
046    public PrompterUIKeyboardInteractive()
047    {
048    }
049
050    public PrompterUIKeyboardInteractive( Prompter promper )
051    {
052        this.prompter = promper;
053    }
054
055    /**
056     * @see UIKeyboardInteractive#promptKeyboardInteractive(String,String,
057     *String,String[],boolean[])
058     */
059    public String[] promptKeyboardInteractive( String destination, String name, String instruction, String[] prompt,
060                                               boolean[] echo )
061    {
062
063        if ( prompt.length != echo.length )
064        {
065            // jcsh is buggy?
066            throw new IllegalArgumentException( "prompt and echo size arrays are different!" );
067        }
068        String[] ret = new String[prompt.length];
069
070        try
071        {
072
073            for ( int i = 0; i < ret.length; i++ )
074            {
075                if ( echo[i] )
076                {
077                    ret[i] = prompter.prompt( prompt[i] );
078                }
079                else
080                {
081                    ret[i] = prompter.promptForPassword( prompt[i] );
082                }
083            }
084        }
085        catch ( PrompterException e )
086        {
087            // TODO: log
088            // the user canceled?
089            ret = null;
090        }
091
092        return ret;
093    }
094}