]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
872f17b290716561749837bf5f84206abc7227fa
1 package org
.argeo
.slc
.jsch
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.PushbackInputStream
;
6 import java
.util
.Arrays
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.SlcException
;
12 import com
.jcraft
.jsch
.UserInfo
;
14 public class SimpleUserInfo
implements UserInfo
{
15 private Boolean permissive
= true;
16 private Boolean verbose
= false;
18 private final static Log log
= LogFactory
.getLog(SimpleUserInfo
.class);
20 private String password
;
21 private char[] passwordSafe
;
22 private String passphrase
;
23 private char[] passphraseSafe
;
25 public void setPassword(String password
) {
26 this.password
= password
;
29 public void setPassphrase(String passphrase
) {
30 this.passphrase
= passphrase
;
33 public String
getPassphrase() {
34 if (passphraseSafe
!= null)
35 return new String(passphraseSafe
);
39 public String
getPassword() {
40 if (passwordSafe
!= null)
41 return new String(passwordSafe
);
45 public boolean promptPassphrase(String message
) {
50 passwordSafe
= readPassword(System
.in
);
51 return passwordSafe
!= null;
55 public boolean promptPassword(String message
) {
60 passwordSafe
= readPassword(System
.in
);
61 return passwordSafe
!= null;
65 public boolean promptYesNo(String message
) {
66 String msg
= message
+ " (y/n): ";
75 c
= (char) System
.in
.read();
76 } catch (IOException e
) {
77 throw new SlcException("Cannot read stdin", e
);
86 public void showMessage(String message
) {
90 public void setPermissive(Boolean permissive
) {
91 this.permissive
= permissive
;
94 public void setVerbose(Boolean verbose
) {
95 this.verbose
= verbose
;
98 protected char[] readPassword(InputStream in
) {
105 buf
= lineBuffer
= new char[128];
107 int room
= buf
.length
;
112 switch (c
= in
.read()) {
119 if ((c2
!= '\n') && (c2
!= -1)) {
120 if (!(in
instanceof PushbackInputStream
)) {
121 in
= new PushbackInputStream(in
);
123 ((PushbackInputStream
) in
).unread(c2
);
129 buf
= new char[offset
+ 128];
130 room
= buf
.length
- offset
- 1;
131 System
.arraycopy(lineBuffer
, 0, buf
, 0, offset
);
132 Arrays
.fill(lineBuffer
, ' ');
135 buf
[offset
++] = (char) c
;
144 char[] ret
= new char[offset
];
145 System
.arraycopy(buf
, 0, ret
, 0, offset
);
146 Arrays
.fill(buf
, ' ');
149 } catch (IOException e
) {
150 throw new SlcException("Cannot read password.", e
);