]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.jsch
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.io
.PushbackInputStream
;
22 import java
.util
.Arrays
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.slc
.SlcException
;
28 import com
.jcraft
.jsch
.UserInfo
;
30 public class SimpleUserInfo
implements UserInfo
{
31 private Boolean permissive
= true;
32 private Boolean verbose
= false;
34 private final static Log log
= LogFactory
.getLog(SimpleUserInfo
.class);
36 protected String password
;
37 protected char[] passwordSafe
;
38 protected String passphrase
;
39 protected char[] passphraseSafe
;
42 if (passwordSafe
!= null)
43 Arrays
.fill(passwordSafe
, (char) 0);
45 if (passphraseSafe
!= null)
46 Arrays
.fill(passphraseSafe
, (char) 0);
47 passphraseSafe
= null;
50 public void setPassword(String password
) {
51 this.password
= password
;
54 public void setPassphrase(String passphrase
) {
55 this.passphrase
= passphrase
;
58 public String
getPassphrase() {
59 if (passphraseSafe
!= null)
60 return new String(passphraseSafe
);
64 public String
getPassword() {
65 if (passwordSafe
!= null)
66 return new String(passwordSafe
);
70 public boolean promptPassphrase(String message
) {
75 passwordSafe
= readPassword(System
.in
);
76 return passwordSafe
!= null;
80 public boolean promptPassword(String message
) {
85 passwordSafe
= readPassword(System
.in
);
86 return passwordSafe
!= null;
90 public boolean promptYesNo(String message
) {
91 String msg
= message
+ " (y/n): ";
100 c
= (char) System
.in
.read();
101 } catch (IOException e
) {
102 throw new SlcException("Cannot read stdin", e
);
111 public void showMessage(String message
) {
115 public void setPermissive(Boolean permissive
) {
116 this.permissive
= permissive
;
119 public void setVerbose(Boolean verbose
) {
120 this.verbose
= verbose
;
123 protected char[] readPassword(InputStream in
) {
130 buf
= lineBuffer
= new char[128];
132 int room
= buf
.length
;
137 switch (c
= in
.read()) {
144 if ((c2
!= '\n') && (c2
!= -1)) {
145 if (!(in
instanceof PushbackInputStream
)) {
146 in
= new PushbackInputStream(in
);
148 ((PushbackInputStream
) in
).unread(c2
);
154 buf
= new char[offset
+ 128];
155 room
= buf
.length
- offset
- 1;
156 System
.arraycopy(lineBuffer
, 0, buf
, 0, offset
);
157 Arrays
.fill(lineBuffer
, ' ');
160 buf
[offset
++] = (char) c
;
169 char[] ret
= new char[offset
];
170 System
.arraycopy(buf
, 0, ret
, 0, offset
);
171 Arrays
.fill(buf
, ' ');
174 } catch (IOException e
) {
175 throw new SlcException("Cannot read password.", e
);