]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/jsch/SimpleUserInfo.java
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 /** Basic implementation of user info. */
15 public class SimpleUserInfo
implements UserInfo
{
16 private Boolean permissive
= true;
17 private Boolean verbose
= false;
19 private final static Log log
= LogFactory
.getLog(SimpleUserInfo
.class);
21 protected String password
;
22 protected char[] passwordSafe
;
23 protected String passphrase
;
24 protected char[] passphraseSafe
;
27 if (passwordSafe
!= null)
28 Arrays
.fill(passwordSafe
, (char) 0);
30 if (passphraseSafe
!= null)
31 Arrays
.fill(passphraseSafe
, (char) 0);
32 passphraseSafe
= null;
35 public void setPassword(String password
) {
36 this.password
= password
;
39 public void setPassphrase(String passphrase
) {
40 this.passphrase
= passphrase
;
43 public String
getPassphrase() {
44 if (passphraseSafe
!= null)
45 return new String(passphraseSafe
);
49 public String
getPassword() {
50 if (passwordSafe
!= null)
51 return new String(passwordSafe
);
55 public boolean promptPassphrase(String message
) {
60 passwordSafe
= readPassword(System
.in
);
61 return passwordSafe
!= null;
65 public boolean promptPassword(String message
) {
70 passwordSafe
= readPassword(System
.in
);
71 return passwordSafe
!= null;
75 public boolean promptYesNo(String message
) {
76 String msg
= message
+ " (y/n): ";
85 c
= (char) System
.in
.read();
86 } catch (IOException e
) {
87 throw new SlcException("Cannot read stdin", e
);
96 public void showMessage(String message
) {
100 public void setPermissive(Boolean permissive
) {
101 this.permissive
= permissive
;
104 public void setVerbose(Boolean verbose
) {
105 this.verbose
= verbose
;
108 protected char[] readPassword(InputStream in
) {
115 buf
= lineBuffer
= new char[128];
117 int room
= buf
.length
;
122 switch (c
= in
.read()) {
129 if ((c2
!= '\n') && (c2
!= -1)) {
130 if (!(in
instanceof PushbackInputStream
)) {
131 in
= new PushbackInputStream(in
);
133 ((PushbackInputStream
) in
).unread(c2
);
139 buf
= new char[offset
+ 128];
140 room
= buf
.length
- offset
- 1;
141 System
.arraycopy(lineBuffer
, 0, buf
, 0, offset
);
142 Arrays
.fill(lineBuffer
, ' ');
145 buf
[offset
++] = (char) c
;
154 char[] ret
= new char[offset
];
155 System
.arraycopy(buf
, 0, ret
, 0, offset
);
156 Arrays
.fill(buf
, ' ');
159 } catch (IOException e
) {
160 throw new SlcException("Cannot read password.", e
);