001package org.apache.maven.wagon.providers.ssh.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 org.codehaus.plexus.components.interactivity.Prompter;
023import org.codehaus.plexus.components.interactivity.PrompterException;
024
025import java.util.Arrays;
026
027/**
028 * Shows messages to System.out, and ask replies using an InputHandler
029 *
030 * @author Juan F. Codagnone
031 * @since Sep 12, 2005
032 * 
033 * @plexus.component role="org.apache.maven.wagon.providers.ssh.interactive.InteractiveUserInfo"
034 *    instantiation-strategy="per-lookup"
035 */
036public class ConsoleInteractiveUserInfo
037    implements InteractiveUserInfo
038{
039    /**
040     * @plexus.requirement role-hint="default"
041     */
042    private volatile Prompter prompter;
043
044    public ConsoleInteractiveUserInfo()
045    {
046    }
047
048    public ConsoleInteractiveUserInfo( Prompter prompter )
049    {
050        this.prompter = prompter;
051    }
052
053    /**
054     * @see InteractiveUserInfo#promptYesNo(String)
055     */
056    public boolean promptYesNo( String message )
057    {
058        String ret;
059        try
060        {
061            ret = prompter.prompt( message, Arrays.asList( new String[]{"yes", "no"} ) );
062        }
063        catch ( PrompterException e )
064        {
065            // no op
066            ret = null;
067        }
068        return "yes".equalsIgnoreCase( ret );
069    }
070
071    /**
072     * @see InteractiveUserInfo#showMessage(String)
073     */
074    public void showMessage( String message )
075    {
076        try
077        {
078            prompter.showMessage( message );
079        }
080        catch ( PrompterException e )
081        {
082            // no op
083        }
084    }
085
086    public String promptPassword( String message )
087    {
088        try
089        {
090            return prompter.promptForPassword( message );
091        }
092        catch ( PrompterException e )
093        {
094            return null;
095        }
096    }
097
098    public String promptPassphrase( String message )
099    {
100        return promptPassword( message );
101    }
102}