View Javadoc
1   /*
2    * Copyright (C) 2014 Cristian Sulea ( http://cristian.sulea.net )
3    * 
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package jatoo.exec.remote;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import com.jcraft.jsch.UserInfo;
24  
25  /**
26   * Simple implementation for {@link UserInfo}.
27   * 
28   * @author <a href="http://cristian.sulea.net" rel="author">Cristian Sulea</a>
29   * @version 1.2, July 24, 2014
30   */
31  public final class RemoteCommandExecutorJCraftUserInfo implements UserInfo {
32  
33    /** The logger. */
34    private final Log logger = LogFactory.getLog(RemoteCommandExecutorJCraftUserInfo.class);
35  
36    private final String password;
37  
38    private boolean isAlreadyPrompted = false;
39  
40    public RemoteCommandExecutorJCraftUserInfo(final String password) {
41      this.password = password;
42    }
43  
44    @Override
45    public boolean promptPassword(final String message) {
46  
47      if (isAlreadyPrompted) {
48        logger.error("Something went wrong, we are prompted again for passwordat. Maybe wrong password?");
49        throw new RuntimeException("Wrong password?");
50      }
51  
52      isAlreadyPrompted = true;
53  
54      logger.info(message);
55      logger.info("No need to prompt for password, the password was passed through constructor.");
56      return true;
57    }
58  
59    @Override
60    public String getPassword() {
61      return password;
62    }
63  
64    @Override
65    public boolean promptYesNo(final String message) {
66      logger.info(message);
67      logger.info("Auto accepting the fingerprint, because I was told to do so.");
68      return true;
69    }
70  
71    @Override
72    public void showMessage(final String message) {
73      logger.info(message);
74    }
75  
76    @Override
77    public boolean promptPassphrase(final String message) {
78      logger.info(message);
79      logger.info("No need to prompt for passphrase, the method #getPassphrase() will always return null.");
80      return true;
81    }
82  
83    @Override
84    public String getPassphrase() {
85      return null;
86    }
87  
88  }