View Javadoc
1   package org.apache.maven.wagon.providers.ssh.jsch.interactive;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.jcraft.jsch.UIKeyboardInteractive;
23  import org.codehaus.plexus.components.interactivity.Prompter;
24  import org.codehaus.plexus.components.interactivity.PrompterException;
25  
26  /**
27   * UIKeyboardInteractive that use plexus-prompter.
28   * 
29   * <code>UIKeyboardInteractive</code> are usefull when you don't use user with
30   * password authentication with a server that use keyboard-interactive and
31   * doesn't allow password method <code>PasswordAuthentication no</code>.
32   *
33   * @author <a href="mailto:juam at users.sourceforge.net">Juan F. Codagnone</a>
34   * @since Sep 22, 2005
35   * 
36   * @plexus.component role="com.jcraft.jsch.UIKeyboardInteractive" 
37   */
38  public class PrompterUIKeyboardInteractive
39      implements UIKeyboardInteractive
40  {
41      /**
42       * @plexus.requirement role-hint="default"
43       */
44      private volatile Prompter prompter;
45  
46      public PrompterUIKeyboardInteractive()
47      {
48      }
49  
50      public PrompterUIKeyboardInteractive( Prompter promper )
51      {
52          this.prompter = promper;
53      }
54  
55      /**
56       * @see UIKeyboardInteractive#promptKeyboardInteractive(String,String,
57       *String,String[],boolean[])
58       */
59      public String[] promptKeyboardInteractive( String destination, String name, String instruction, String[] prompt,
60                                                 boolean[] echo )
61      {
62  
63          if ( prompt.length != echo.length )
64          {
65              // jcsh is buggy?
66              throw new IllegalArgumentException( "prompt and echo size arrays are different!" );
67          }
68          String[] ret = new String[prompt.length];
69  
70          try
71          {
72  
73              for ( int i = 0; i < ret.length; i++ )
74              {
75                  if ( echo[i] )
76                  {
77                      ret[i] = prompter.prompt( prompt[i] );
78                  }
79                  else
80                  {
81                      ret[i] = prompter.promptForPassword( prompt[i] );
82                  }
83              }
84          }
85          catch ( PrompterException e )
86          {
87              // TODO: log
88              // the user canceled?
89              ret = null;
90          }
91  
92          return ret;
93      }
94  }