]>
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) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.jsch
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
20 import java
.io
.PushbackInputStream
;
21 import java
.util
.Arrays
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.SlcException
;
27 import com
.jcraft
.jsch
.UserInfo
;
29 public class SimpleUserInfo
implements UserInfo
{
30 private Boolean permissive
= true;
31 private Boolean verbose
= false;
33 private final static Log log
= LogFactory
.getLog(SimpleUserInfo
.class);
35 protected String password
;
36 protected char[] passwordSafe
;
37 protected String passphrase
;
38 protected char[] passphraseSafe
;
41 if (passwordSafe
!= null)
42 Arrays
.fill(passwordSafe
, (char) 0);
44 if (passphraseSafe
!= null)
45 Arrays
.fill(passphraseSafe
, (char) 0);
46 passphraseSafe
= null;
49 public void setPassword(String password
) {
50 this.password
= password
;
53 public void setPassphrase(String passphrase
) {
54 this.passphrase
= passphrase
;
57 public String
getPassphrase() {
58 if (passphraseSafe
!= null)
59 return new String(passphraseSafe
);
63 public String
getPassword() {
64 if (passwordSafe
!= null)
65 return new String(passwordSafe
);
69 public boolean promptPassphrase(String message
) {
74 passwordSafe
= readPassword(System
.in
);
75 return passwordSafe
!= null;
79 public boolean promptPassword(String message
) {
84 passwordSafe
= readPassword(System
.in
);
85 return passwordSafe
!= null;
89 public boolean promptYesNo(String message
) {
90 String msg
= message
+ " (y/n): ";
99 c
= (char) System
.in
.read();
100 } catch (IOException e
) {
101 throw new SlcException("Cannot read stdin", e
);
110 public void showMessage(String message
) {
114 public void setPermissive(Boolean permissive
) {
115 this.permissive
= permissive
;
118 public void setVerbose(Boolean verbose
) {
119 this.verbose
= verbose
;
122 protected char[] readPassword(InputStream in
) {
129 buf
= lineBuffer
= new char[128];
131 int room
= buf
.length
;
136 switch (c
= in
.read()) {
143 if ((c2
!= '\n') && (c2
!= -1)) {
144 if (!(in
instanceof PushbackInputStream
)) {
145 in
= new PushbackInputStream(in
);
147 ((PushbackInputStream
) in
).unread(c2
);
153 buf
= new char[offset
+ 128];
154 room
= buf
.length
- offset
- 1;
155 System
.arraycopy(lineBuffer
, 0, buf
, 0, offset
);
156 Arrays
.fill(lineBuffer
, ' ');
159 buf
[offset
++] = (char) c
;
168 char[] ret
= new char[offset
];
169 System
.arraycopy(buf
, 0, ret
, 0, offset
);
170 Arrays
.fill(buf
, ' ');
173 } catch (IOException e
) {
174 throw new SlcException("Cannot read password.", e
);