KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > vwms > SCPcopy


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders.vwms;
11
12 import org.mmbase.util.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * Performs a transfer of a file from the current server to a host.
17  * Uses invocation of ssh on the commandline to achieve its ends.
18  * Since no password is used, the client machine (this server) has to be
19  * configured to gain access (i.e. through the use of public keys).
20  * Also note that the client needs execute rights on ssh.<br />
21  * A system that directlky acecsses ssh, and does not make use of {@link Execute}
22  * is being developed.
23  *
24  * @author Rico Jansen
25  * @author Pierre van Rooden (javadocs)
26  * @version $Id: SCPcopy.java,v 1.6 2003/03/10 11:50:24 pierre Exp $
27  */

28 public class SCPcopy {
29
30     /**
31      * Program runner for executing the 'ssh' command.
32      */

33     Execute exec=new Execute();
34     /**
35      * Name of the current server
36      */

37     String JavaDoc thisserver;
38     /**
39      * Root-path of the file at the destination host
40      */

41     String JavaDoc dstpath;
42     /**
43      * Name of the destination user.
44      * Needed to log in to the host (using ssh).
45      */

46     String JavaDoc dstuser;
47     /**
48      * Name of the destination host
49      */

50     String JavaDoc dsthost;
51     /**
52      * Path to ssh command
53      */

54     String JavaDoc sshpath;
55
56     // Logger class
57
private static Logger log = Logging.getLoggerInstance(SCPcopy.class.getName());
58
59     /**
60      * SCPCopy Constructor.
61      * @deprecated vpro-specific
62      */

63     public SCPcopy() {
64         setSSHpath("/usr/local/bin");
65         setUser("vpro");
66         setHost("vpro.omroep.nl");
67         setPath("/bigdisk/htdocs");
68     }
69
70     /**
71      * SCPCopy Constructor.
72      * @param sshpath Path to ssh command
73      * @deprecated vpro-specific
74      */

75     public SCPcopy(String JavaDoc sshpath) {
76         setSSHpath(sshpath);
77         setUser("vpro");
78         setHost("vpro.omroep.nl");
79         setPath("/bigdisk/htdocs");
80     }
81
82     /**
83      * SCPCopy Constructor.
84      * @param sshpath Path to ssh command
85      * @param user User name needed to log on at the destination host
86      * @deprecated vpro-specific
87      */

88     public SCPcopy(String JavaDoc sshpath,String JavaDoc user) {
89         setSSHpath(sshpath);
90         setUser(user);
91         setHost("vpro.omroep.nl");
92         setPath("/bigdisk/htdocs");
93     }
94
95     /**
96      * SCPCopy Constructor.
97      * @param sshpath Path to ssh command
98      * @param user User name needed to log on at the destination host
99      * @param host Name of the destination host
100      * @deprecated vpro-specific
101      */

102     public SCPcopy(String JavaDoc sshpath,String JavaDoc user,String JavaDoc host) {
103         setSSHpath(sshpath);
104         setUser(user);
105         setHost(host);
106         setPath("/bigdisk/htdocs");
107     }
108
109     /**
110      * SCPCopy Constructor.
111      * @param sshpath Path to ssh command
112      * @param user User name needed to log on at the destination host
113      * @param host Name of the destination host
114      * @param path Root path of the file at the destination host
115      */

116     public SCPcopy(String JavaDoc sshpath,String JavaDoc user,String JavaDoc host,String JavaDoc path) {
117         setSSHpath(sshpath);
118         setUser(user);
119         setHost(host);
120         setPath(path);
121     }
122
123     /**
124      * Transfers a file from this server to a host.
125      * Creates directories if needed.
126      * @param base Root path of the file on this server
127      * @param src The actual file to transfer (includes path info)
128      * @param dst Name of the destination host
129      */

130     public void copy(String JavaDoc base,String JavaDoc src,String JavaDoc dst) {
131         dsthost=dst;
132         copy(base,src);
133     }
134
135     /**
136      * Transfers a file from this server to the host.
137      * Creates directories if needed.
138      * @param base Root path of the file on this server
139      * @param src The actual file to transfer (includes path info)
140      */

141     public void copy(String JavaDoc base,String JavaDoc src) {
142         int last;
143         String JavaDoc path,res;
144
145         last=src.lastIndexOf('/');
146
147         // creates the directories
148
if (last!=-1) {
149             path=dstpath+src.substring(0,last);
150             // note: mkdirs returns success or failure, but this is not checked
151
mkdirs(path);
152         }
153         // determine full file path
154
path=dstpath+src;
155         realcopy(base+src,path+".tmp");
156         rename(path+".tmp",path);
157     }
158
159     /**
160      * Creates a directory at the host.
161      * @param path directory to create
162      * @return <code>true</code> if successful
163      */

164     public boolean mkdir(String JavaDoc path) {
165         String JavaDoc res;
166         res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" mkdir "+path+"");
167         log.debug("SCPcopy -> mkdir "+path+" : "+res);
168         return res.length()<=0;
169     }
170
171     /**
172      * Creates a full directory path at the host.
173      * @param path path to create
174      * @return <code>true</code> if successful
175      */

176     public boolean mkdirs(String JavaDoc path) {
177         String JavaDoc res;
178         res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" mkdir -p "+path+"");
179         log.debug("SCPcopy -> mkdirs "+path+" : "+res);
180         return res.length()<=0;
181     }
182
183     /**
184      * Renames a file at the host.
185      * @param src Original file name
186      * @param dst New file name
187      * @return <code>true</code> if successful
188      */

189     public boolean rename(String JavaDoc src,String JavaDoc dst) {
190         String JavaDoc res;
191         int pos=src.indexOf("("); // strange check. and how about spaces?
192
if (pos==-1) {
193             res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" mv "+src+" "+dst+"");
194         } else {
195             res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" mv \""+src+"\" \""+dst+"\"");
196         }
197
198         log.debug("SCPcopy -> rename "+src+"->"+dst+" : "+res);
199         return res.length()<=0;
200     }
201
202     /**
203      * Transfers a file at this server to the host.
204      * Directories should already exist.
205      * @param src file path at this server
206      * @param dst new file path at the host
207      * @return <code>true</code> if successful
208      */

209     public boolean realcopy(String JavaDoc src,String JavaDoc dst) {
210         String JavaDoc res;
211         int pos=src.indexOf("("); // strange check. and how about spaces?
212
if (pos==-1) {
213             res=exec.execute(sshpath+"/scp -B -A -q "+src+" "+dstuser+"@"+dsthost+":"+dst);
214         } else {
215             res=exec.execute(sshpath+"/scp -B -A -q "+src+" "+dstuser+"@"+dsthost+":\""+dst+"\"");
216         }
217
218         log.service("SCPcopy -> copy "+src+"->"+dst+" : "+res);
219         return res.length()<=0;
220     }
221
222     /**
223      * Removes a file at the host.
224      * @param path path of the file to remove
225      * @return <code>true</code> if successful
226      */

227     public boolean remove(String JavaDoc path) {
228         String JavaDoc res;
229         res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" rm -f "+path);
230         log.debug("SCPcopy -> remove "+path+" : "+res);
231         return res.length()<=0;
232     }
233
234     /**
235      * Removes a directory at the host.
236      * @param path path of the directory to remove
237      * @return <code>true</code> if successful
238      */

239     public boolean removedir(String JavaDoc path) {
240         String JavaDoc res;
241         res=exec.execute(sshpath+"/ssh -q -l "+dstuser+" "+dsthost+" rmdir "+path);
242         log.debug("SCPcopy -> removedir "+path+" : "+res);
243         return res.length()<=0;
244     }
245
246     /**
247      * Get path to ssh command
248      */

249     public String JavaDoc getSSHpath() {
250         return(sshpath);
251     }
252
253     /**
254      * Get root-path of the file at the destination host
255      */

256     public String JavaDoc getPath() {
257         return(dstpath);
258     }
259
260     /**
261      * Get name of the destination host
262      */

263     public String JavaDoc getHost() {
264         return(dsthost);
265     }
266
267     /**
268      * Get name of the destination user.
269      */

270     public String JavaDoc getUser() {
271         return(dstuser);
272     }
273
274
275
276     /**
277      * Set path to ssh command
278      */

279     public void setSSHpath(String JavaDoc path) {
280         sshpath=path;
281     }
282
283     /**
284      * Set root-path of the file at the destination host
285      */

286     public void setPath(String JavaDoc path) {
287         dstpath=path;
288     }
289
290     /**
291      * Set name of the destination host
292      */

293     public void setHost(String JavaDoc host) {
294         dsthost=host;
295     }
296
297     /**
298      * Set name of the destination user.
299      */

300     public void setUser(String JavaDoc user) {
301         dstuser=user;
302     }
303
304     /**
305      * Entry for direct invocation from the commandline.
306      * Usage:<br />
307      * java SCPCopy [basedir],[filepath],[destinationhost]<br />
308      * @param args The commandline arguments
309      * @deprecated VPRO-specific
310      */

311     public static void main(String JavaDoc args[]) {
312         SCPcopy scp=new SCPcopy();
313         scp.copy(args[0],args[1],args[2]);
314         System.exit(0);
315     }
316 }
317
Popular Tags