KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > tasks > CopyTask


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.vfs.tasks;
17
18 import org.apache.commons.vfs.FileObject;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.Selectors;
21
22 /**
23  * An Ant task that copies matching files.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  * @todo Copy folders that do not contain files
27  */

28 public class CopyTask
29     extends AbstractSyncTask
30 {
31     private boolean overwrite = false;
32     private boolean preserveLastModified = true;
33
34     /**
35      * Enable/disable overwriting of up-to-date files.
36      */

37     public void setOverwrite(boolean overwrite)
38     {
39         this.overwrite = overwrite;
40     }
41
42     /**
43      * Enable/disable preserving last modified time of copied files.
44      */

45     public void setPreserveLastModified(boolean preserveLastModified)
46     {
47         this.preserveLastModified = preserveLastModified;
48     }
49
50     /**
51      * @return the curent value of overwrite
52      */

53     public boolean isOverwrite()
54     {
55         return overwrite;
56     }
57
58     /**
59      * @return the curent value of preserveLastModified
60      */

61     public boolean isPreserveLastModified()
62     {
63         return preserveLastModified;
64     }
65
66     /**
67      * Handles an out-of-date file.
68      */

69     protected void handleOutOfDateFile(final FileObject srcFile,
70                                        final FileObject destFile)
71         throws FileSystemException
72     {
73         log("Copying " + srcFile + " to " + destFile);
74         destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
75         if (preserveLastModified)
76         {
77             final long lastModTime = srcFile.getContent().getLastModifiedTime();
78             destFile.getContent().setLastModifiedTime(lastModTime);
79         }
80     }
81
82     /**
83      * Handles an up-to-date file.
84      */

85     protected void handleUpToDateFile(final FileObject srcFile,
86                                       final FileObject destFile)
87         throws FileSystemException
88     {
89         if (overwrite)
90         {
91             // Copy the file anyway
92
handleOutOfDateFile(srcFile, destFile);
93         }
94     }
95 }
96
Popular Tags