KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ssh > SSHUserInfo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.ssh;
20
21 import com.jcraft.jsch.UserInfo;
22 import com.jcraft.jsch.UIKeyboardInteractive;
23
24 /**
25  * Class containing information on an SSH user.
26  */

27 public class SSHUserInfo implements UserInfo, UIKeyboardInteractive {
28
29     private String JavaDoc name;
30     private String JavaDoc password = null;
31     private String JavaDoc keyfile;
32     private String JavaDoc passphrase = null;
33     private boolean trustAllCertificates;
34
35     /** Constructor for SSHUserInfo. */
36     public SSHUserInfo() {
37         super();
38         this.trustAllCertificates = false;
39     }
40
41     /**
42      * Constructor for SSHUserInfo.
43      * @param password the user's password
44      * @param trustAllCertificates if true trust hosts whose identity is unknown
45      */

46     public SSHUserInfo(String JavaDoc password, boolean trustAllCertificates) {
47         super();
48         this.password = password;
49         this.trustAllCertificates = trustAllCertificates;
50     }
51
52     /**
53      * Gets the user name.
54      * @return the user name
55      */

56     public String JavaDoc getName() {
57         return name;
58     }
59
60     /**
61      * Gets the pass phrase of the user.
62      * @param message a message
63      * @return the passphrase
64      */

65     public String JavaDoc getPassphrase(String JavaDoc message) {
66         return passphrase;
67     }
68
69     /**
70      * Gets the user's password.
71      * @return the user's password
72      */

73     public String JavaDoc getPassword() {
74         return password;
75     }
76
77     /**
78      * Prompts a string.
79      * @param str the string
80      * @return whether the string was prompted
81      */

82     public boolean prompt(String JavaDoc str) {
83         return false;
84     }
85
86     /**
87      * Indicates whether a retry was done.
88      * @return whether a retry was done
89      */

90     public boolean retry() {
91         return false;
92     }
93
94     /**
95      * Sets the name.
96      * @param name The name to set
97      */

98     public void setName(String JavaDoc name) {
99         this.name = name;
100     }
101
102     /**
103      * Sets the passphrase.
104      * @param passphrase The passphrase to set
105      */

106     public void setPassphrase(String JavaDoc passphrase) {
107         this.passphrase = passphrase;
108     }
109
110     /**
111      * Sets the password.
112      * @param password The password to set
113      */

114     public void setPassword(String JavaDoc password) {
115         this.password = password;
116     }
117
118     /**
119      * Sets the trust.
120      * @param trust whether to trust or not.
121      */

122     public void setTrust(boolean trust) {
123         this.trustAllCertificates = trust;
124     }
125
126     /**
127      * @return whether to trust or not.
128      */

129     public boolean getTrust() {
130         return this.trustAllCertificates;
131     }
132
133     /**
134      * Returns the passphrase.
135      * @return String
136      */

137     public String JavaDoc getPassphrase() {
138         return passphrase;
139     }
140
141     /**
142      * Returns the keyfile.
143      * @return String
144      */

145     public String JavaDoc getKeyfile() {
146         return keyfile;
147     }
148
149     /**
150      * Sets the keyfile.
151      * @param keyfile The keyfile to set
152      */

153     public void setKeyfile(String JavaDoc keyfile) {
154         this.keyfile = keyfile;
155     }
156
157     /**
158      * Implement the UserInfo interface.
159      * @param message ignored
160      * @return true always
161      */

162     public boolean promptPassphrase(String JavaDoc message) {
163         return true;
164     }
165
166     /**
167      * Implement the UserInfo interface.
168      * @param passwordPrompt ignored
169      * @return true the first time this is called, false otherwise
170      */

171     public boolean promptPassword(String JavaDoc passwordPrompt) {
172         return true;
173     }
174
175     /**
176      * Implement the UserInfo interface.
177      * @param message ignored
178      * @return the value of trustAllCertificates
179      */

180     public boolean promptYesNo(String JavaDoc message) {
181         return trustAllCertificates;
182     }
183
184 //why do we do nothing?
185
/**
186      * Implement the UserInfo interface (noop).
187      * @param message ignored
188      */

189     public void showMessage(String JavaDoc message) {
190         //log(message, Project.MSG_DEBUG);
191
}
192
193     /**
194      * Implementation of UIKeyboardInteractive#promptKeyboardInteractive.
195      * @param destination not used.
196      * @param name not used.
197      * @param instruction not used.
198      * @param prompt the method checks if this is one in length.
199      * @param echo the method checks if the first element is false.
200      * @return the password in an size one array if there is a password
201      * and if the prompt and echo checks pass.
202      */

203     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
204                                               String JavaDoc name,
205                                               String JavaDoc instruction,
206                                               String JavaDoc[] prompt,
207                                               boolean[] echo) {
208         if (prompt.length != 1 || echo[0] || this.password == null) {
209             return null;
210         }
211         String JavaDoc[] response = new String JavaDoc[1];
212         response[0] = this.password;
213         return response;
214     }
215
216 }
217
Popular Tags