KickJava   Java API By Example, From Geeks To Geeks.

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


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.JSchException;
22 import com.jcraft.jsch.Session;
23 import com.jcraft.jsch.JSch;
24
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Base class for Ant tasks using jsch.
31  *
32  * @since Ant 1.6
33  */

34 public abstract class SSHBase extends Task implements LogListener {
35
36     /** Default listen port for SSH daemon */
37     private static final int SSH_PORT = 22;
38
39     private String JavaDoc host;
40     private String JavaDoc knownHosts;
41     private int port = SSH_PORT;
42     private boolean failOnError = true;
43     private boolean verbose;
44     private SSHUserInfo userInfo;
45
46     /**
47      * Constructor for SSHBase.
48      */

49     public SSHBase() {
50         super();
51         userInfo = new SSHUserInfo();
52     }
53
54     /**
55      * Remote host, either DNS name or IP.
56      *
57      * @param host The new host value
58      */

59     public void setHost(String JavaDoc host) {
60         this.host = host;
61     }
62
63     /**
64      * Get the host.
65      * @return the host
66      */

67     public String JavaDoc getHost() {
68         return host;
69     }
70
71     /**
72      * Set the failonerror flag.
73      * Default is true
74      * @param failure if true throw a build exception when a failure occuries,
75      * otherwise just log the failure and continue
76      */

77     public void setFailonerror(boolean failure) {
78         failOnError = failure;
79     }
80
81     /**
82      * Get the failonerror flag.
83      * @return the failonerror flag
84      */

85     public boolean getFailonerror() {
86         return failOnError;
87     }
88
89     /**
90      * Set the verbose flag.
91      * @param verbose if true output more verbose logging
92      * @since Ant 1.6.2
93      */

94     public void setVerbose(boolean verbose) {
95         this.verbose = verbose;
96     }
97
98     /**
99      * Get the verbose flag.
100      * @return the verbose flag
101      * @since Ant 1.6.2
102      */

103     public boolean getVerbose() {
104         return verbose;
105     }
106
107     /**
108      * Username known to remote host.
109      *
110      * @param username The new username value
111      */

112     public void setUsername(String JavaDoc username) {
113         userInfo.setName(username);
114     }
115
116
117     /**
118      * Sets the password for the user.
119      *
120      * @param password The new password value
121      */

122     public void setPassword(String JavaDoc password) {
123         userInfo.setPassword(password);
124     }
125
126     /**
127      * Sets the keyfile for the user.
128      *
129      * @param keyfile The new keyfile value
130      */

131     public void setKeyfile(String JavaDoc keyfile) {
132         userInfo.setKeyfile(keyfile);
133     }
134
135     /**
136      * Sets the passphrase for the users key.
137      *
138      * @param passphrase The new passphrase value
139      */

140     public void setPassphrase(String JavaDoc passphrase) {
141         userInfo.setPassphrase(passphrase);
142     }
143
144     /**
145      * Sets the path to the file that has the identities of
146      * all known hosts. This is used by SSH protocol to validate
147      * the identity of the host. The default is
148      * <i>${user.home}/.ssh/known_hosts</i>.
149      *
150      * @param knownHosts a path to the known hosts file.
151      */

152     public void setKnownhosts(String JavaDoc knownHosts) {
153         this.knownHosts = knownHosts;
154     }
155
156     /**
157      * Setting this to true trusts hosts whose identity is unknown.
158      *
159      * @param yesOrNo if true trust the identity of unknown hosts.
160      */

161     public void setTrust(boolean yesOrNo) {
162         userInfo.setTrust(yesOrNo);
163     }
164
165     /**
166      * Changes the port used to connect to the remote host.
167      *
168      * @param port port number of remote host.
169      */

170     public void setPort(int port) {
171         this.port = port;
172     }
173
174     /**
175      * Get the port attribute.
176      * @return the port
177      */

178     public int getPort() {
179         return port;
180     }
181
182     /**
183      * Initialize the task.
184      * This initializizs the known hosts and sets the default port.
185      * @throws BuildException on error
186      */

187     public void init() throws BuildException {
188         super.init();
189         this.knownHosts = System.getProperty("user.home") + "/.ssh/known_hosts";
190         this.port = SSH_PORT;
191     }
192
193     /**
194      * Open an ssh seession.
195      * @return the opened session
196      * @throws JSchException on error
197      */

198     protected Session openSession() throws JSchException {
199         JSch jsch = new JSch();
200         if (null != userInfo.getKeyfile()) {
201             jsch.addIdentity(userInfo.getKeyfile());
202         }
203
204         if (!userInfo.getTrust() && knownHosts != null) {
205             log("Using known hosts: " + knownHosts, Project.MSG_DEBUG);
206             jsch.setKnownHosts(knownHosts);
207         }
208
209         Session session = jsch.getSession(userInfo.getName(), host, port);
210         session.setUserInfo(userInfo);
211         log("Connecting to " + host + ":" + port);
212         session.connect();
213         return session;
214     }
215
216     /**
217      * Get the user information.
218      * @return the user information
219      */

220     protected SSHUserInfo getUserInfo() {
221         return userInfo;
222     }
223 }
224
Popular Tags