KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > CopyJob


1 package org.oddjob.io;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11 import org.apache.commons.io.FileUtils;
12 import org.apache.commons.io.IOUtils;
13 import org.apache.log4j.Logger;
14 import org.oddjob.util.OddjobConfigException;
15
16
17 /**
18  * @oddjob.description A Copy job. Copy either:
19  * <ul>
20  * <li>A file from one name to another.</li>
21  * <li>A file or files or directories to a different directory.</li>
22  * <li>An input (from another job) to a file.</li>
23  * <li>A file to an output.</li>
24  * <li>An input to an output.</li>
25  * </ul>
26  *
27  * @oddjob.example
28  *
29  * &lt;copy from="fu/bar.txt" to="foo/ba.txt"/&gt;
30  *
31  */

32 public class CopyJob implements Runnable JavaDoc, Serializable JavaDoc {
33     private static final long serialVersionUID = 20050806;
34
35     private static final Logger logger = Logger.getLogger(CopyJob.class);
36     
37     /**
38      * @oddjob.property
39      * @oddjob.description A name, can be any text.
40      * @oddjob.required No.
41      */

42     private String JavaDoc name;
43     
44     /**
45      * @oddjob.property
46      * @oddjob.description The from file.
47      * @oddjob.required Yes unless input supplied.
48      */

49     private File JavaDoc[] from;
50     
51     /**
52      * @oddjob.property
53      * @oddjob.description The from file.
54      * @oddjob.required Yes unless output supplied.
55      */

56     private File JavaDoc to;
57     
58     /**
59      * @oddjob.property
60      * @oddjob.description An input.
61      * @oddjob.required Yes unless from supplied.
62      */

63     private transient InputStream JavaDoc input;
64     
65     /**
66      * @oddjob.property
67      * @oddjob.description The output.
68      * @oddjob.required Yes unless to supplied.
69      */

70     private transient OutputStream JavaDoc output;
71     
72     /**
73      * Get the name.
74      *
75      * @return The name.
76      */

77     public String JavaDoc getName() {
78         return name;
79     }
80     
81     /**
82      * Set the name
83      *
84      * @param name The name.
85      */

86     public void setName(String JavaDoc name) {
87         this.name = name;
88     }
89     
90     /**
91      * Get the from file.
92      *
93      * @return The from file.
94      */

95     synchronized public File JavaDoc[] getFrom() {
96         return from;
97     }
98     
99     /**
100      * Set the from file.
101      *
102      * @param The from file.
103      */

104     synchronized public void setFrom(File JavaDoc[] file) {
105         this.from = file;
106     }
107
108     /**
109      * Get the to file.
110      *
111      * @return The to file.
112      */

113     synchronized public File JavaDoc getTo() {
114         return to;
115     }
116     
117     /**
118      * Set the to file.
119      *
120      * @param The to file.
121      */

122     synchronized public void setTo(File JavaDoc file) {
123         this.to = file;
124     }
125
126     /**
127      * Set the InputStream.
128      *
129      * @param The InputStream.
130      */

131     synchronized public void setInput(InputStream JavaDoc in) {
132         this.input = in;
133     }
134
135     /**
136      * Set the OutputStream.
137      *
138      * @param The OutputStream.
139      */

140     synchronized public void setOutput(OutputStream JavaDoc out) {
141         this.output = out;
142     }
143
144     public void run() {
145         try {
146             CopyCommand command = command();
147             if (command == null) {
148                 return;
149             }
150             CopyStats stats = new CopyStats();
151             command.copy(stats);
152             logger.info("Copied " + stats.files + " files, "
153                     + stats.directories + " directories.");
154         } catch (IOException JavaDoc e) {
155             throw new RuntimeException JavaDoc(e);
156         }
157     }
158         
159     private CopyCommand command() throws IOException JavaDoc {
160             
161         if (input != null) {
162             if (output != null) {
163                 return new StreamCopy(input, output);
164             }
165             else {
166                 return new StreamCopy(input, to);
167             }
168         }
169         
170         if (from == null) {
171             throw new RuntimeException JavaDoc("Nothing to copy from!");
172         }
173
174         File JavaDoc[] possiblyMany = Files.expand(from);
175         Files.verifyReadable(possiblyMany);
176         File JavaDoc singleFrom = null;
177         
178         if (possiblyMany.length == 0) {
179             throw new RuntimeException JavaDoc("From does not specify any files.");
180         }
181         
182         if (possiblyMany.length == 1) {
183             singleFrom = possiblyMany[0];
184         }
185
186         if (singleFrom != null) {
187             if (output != null) {
188                 return new StreamCopy(singleFrom, output);
189             }
190             else if (singleFrom.isDirectory()) {
191                 return new DirectoryCopy(singleFrom, to);
192             } else {
193                 return new FileCopy(singleFrom, to);
194             }
195         }
196         
197         return new MultiFileCopy(possiblyMany, to);
198     }
199     
200     /**
201      * @return Returns the input.
202      */

203     public InputStream JavaDoc getInput() {
204         return input;
205     }
206     /**
207      * @return Returns the output.
208      */

209     public OutputStream JavaDoc getOutput() {
210         return output;
211     }
212     
213     public String JavaDoc toString() {
214         if (name == null) {
215             return "Copy Files or Directories";
216         }
217         return name;
218     }
219     
220     
221     /**
222      *
223      *
224      */

225     interface CopyCommand {
226     
227         public void copy(CopyStats stats) throws IOException JavaDoc;
228     }
229     
230     static class StreamCopy implements CopyCommand {
231         InputStream JavaDoc in;
232         OutputStream JavaDoc out;
233         
234         StreamCopy(InputStream JavaDoc in, OutputStream JavaDoc out) {
235             this.in = in;
236             this.out = out;
237         }
238         
239         StreamCopy(InputStream JavaDoc in, File JavaDoc to) throws IOException JavaDoc {
240             this.in = in;
241             if (to == null) {
242                 throw new RuntimeException JavaDoc("To file is not specified.");
243             }
244             if (to.isDirectory()) {
245                 throw new OddjobConfigException("Can copy stream to a directory.");
246             }
247             this.out = new FileOutputStream JavaDoc(to);
248         }
249         
250         StreamCopy(File JavaDoc from, OutputStream JavaDoc out) throws IOException JavaDoc {
251             this.in = new FileInputStream JavaDoc(from);
252             this.out = out;
253         }
254         
255         public void copy(CopyStats stats) throws IOException JavaDoc {
256             IOUtils.copy(in, out);
257             stats.files++;
258         }
259     }
260     
261     static class FileCopy implements CopyCommand {
262         File JavaDoc from;
263         File JavaDoc to;
264         
265         FileCopy(File JavaDoc from, File JavaDoc to) {
266             this.from = from;
267             this.to = to;
268         }
269         
270         public void copy(CopyStats stats) throws IOException JavaDoc {
271             if (to.isDirectory()) {
272                 FileUtils.copyFileToDirectory(from, to);
273             }
274             else {
275                 FileUtils.copyFile(from, to);
276             }
277             stats.files++;
278         }
279     }
280     
281     
282     static class DirectoryCopy implements CopyCommand {
283         File JavaDoc fromDir;
284         File JavaDoc toDir;
285         
286         DirectoryCopy(File JavaDoc from, File JavaDoc to) {
287             this.fromDir = from;
288             if (to == null) {
289                 throw new RuntimeException JavaDoc("To dir not specified.");
290             }
291             if (to.exists()) {
292                 if (!to.isDirectory()) {
293                     throw new OddjobConfigException("To must be a directory.");
294                 }
295                 this.toDir = new File JavaDoc(to, from.getName());
296             } else {
297                 this.toDir = to;
298             }
299         }
300         
301         public void copy(CopyStats stats) throws IOException JavaDoc {
302             FileUtils.copyDirectory(fromDir, toDir);
303             stats.directories++;
304         }
305     }
306
307     static class MultiFileCopy implements CopyCommand {
308         File JavaDoc[] files;
309         File JavaDoc toDir;
310         
311         MultiFileCopy(File JavaDoc[] files, File JavaDoc toDir) {
312             this.files = files;
313             if (toDir == null) {
314                 throw new RuntimeException JavaDoc("To dir is not specified.");
315             }
316             if (!toDir.isDirectory()) {
317                 throw new RuntimeException JavaDoc("To must be a directory.");
318             }
319             this.toDir = toDir;
320         }
321         
322         public void copy(CopyStats stats) throws IOException JavaDoc {
323             for (int i = 0; i < files.length; ++i) {
324                 CopyCommand command;
325                 if (files[i].isDirectory()) {
326                     command = new DirectoryCopy(files[i], toDir);
327                 } else {
328                     command = new FileCopy(files[i], toDir);
329                 }
330                 command.copy(stats);
331             }
332         }
333     }
334
335     class CopyStats {
336         int files;
337         int directories;
338     }
339 }
340
Popular Tags