]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
Start working on serialized JMS
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / SimpleUserInfo.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.jsch;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.PushbackInputStream;
22 import java.util.Arrays;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.slc.SlcException;
27
28 import com.jcraft.jsch.UserInfo;
29
30 public class SimpleUserInfo implements UserInfo {
31 private Boolean permissive = true;
32 private Boolean verbose = false;
33
34 private final static Log log = LogFactory.getLog(SimpleUserInfo.class);
35
36 protected String password;
37 protected char[] passwordSafe;
38 protected String passphrase;
39 protected char[] passphraseSafe;
40
41 public void reset() {
42 if (passwordSafe != null)
43 Arrays.fill(passwordSafe, (char) 0);
44 passwordSafe = null;
45 if (passphraseSafe != null)
46 Arrays.fill(passphraseSafe, (char) 0);
47 passphraseSafe = null;
48 }
49
50 public void setPassword(String password) {
51 this.password = password;
52 }
53
54 public void setPassphrase(String passphrase) {
55 this.passphrase = passphrase;
56 }
57
58 public String getPassphrase() {
59 if (passphraseSafe != null)
60 return new String(passphraseSafe);
61 return passphrase;
62 }
63
64 public String getPassword() {
65 if (passwordSafe != null)
66 return new String(passwordSafe);
67 return password;
68 }
69
70 public boolean promptPassphrase(String message) {
71 if (permissive)
72 return true;
73 else {
74 log.info(message);
75 passwordSafe = readPassword(System.in);
76 return passwordSafe != null;
77 }
78 }
79
80 public boolean promptPassword(String message) {
81 if (permissive)
82 return true;
83 else {
84 log.info(message);
85 passwordSafe = readPassword(System.in);
86 return passwordSafe != null;
87 }
88 }
89
90 public boolean promptYesNo(String message) {
91 String msg = message + " (y/n): ";
92 if (permissive) {
93 if (verbose)
94 log.info(msg + "y");
95 return true;
96 } else {
97 log.info(msg);
98 char c;
99 try {
100 c = (char) System.in.read();
101 } catch (IOException e) {
102 throw new SlcException("Cannot read stdin", e);
103 }
104 if (c == 'y')
105 return true;
106 else
107 return false;
108 }
109 }
110
111 public void showMessage(String message) {
112 log.info(message);
113 }
114
115 public void setPermissive(Boolean permissive) {
116 this.permissive = permissive;
117 }
118
119 public void setVerbose(Boolean verbose) {
120 this.verbose = verbose;
121 }
122
123 protected char[] readPassword(InputStream in) {
124
125 try {
126 char[] lineBuffer;
127 char[] buf;
128 // int i;
129
130 buf = lineBuffer = new char[128];
131
132 int room = buf.length;
133 int offset = 0;
134 int c;
135
136 loop: while (true) {
137 switch (c = in.read()) {
138 case -1:
139 case '\n':
140 break loop;
141
142 case '\r':
143 int c2 = in.read();
144 if ((c2 != '\n') && (c2 != -1)) {
145 if (!(in instanceof PushbackInputStream)) {
146 in = new PushbackInputStream(in);
147 }
148 ((PushbackInputStream) in).unread(c2);
149 } else
150 break loop;
151
152 default:
153 if (--room < 0) {
154 buf = new char[offset + 128];
155 room = buf.length - offset - 1;
156 System.arraycopy(lineBuffer, 0, buf, 0, offset);
157 Arrays.fill(lineBuffer, ' ');
158 lineBuffer = buf;
159 }
160 buf[offset++] = (char) c;
161 break;
162 }
163 }
164
165 if (offset == 0) {
166 return null;
167 }
168
169 char[] ret = new char[offset];
170 System.arraycopy(buf, 0, ret, 0, offset);
171 Arrays.fill(buf, ' ');
172
173 return ret;
174 } catch (IOException e) {
175 throw new SlcException("Cannot read password.", e);
176 }
177 }
178
179 }