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 package org.apache.commons.vfs2.tasks;
18
19 import org.apache.commons.vfs2.Capability;
20 import org.apache.commons.vfs2.FileObject;
21 import org.apache.commons.vfs2.FileSystemException;
22 import org.apache.commons.vfs2.Selectors;
23
24 /**
25 * An Ant task that copies matching files.
26 * <p>
27 * TODO - Copy folders that do not contain files.
28 * </p>
29 */
30 public class CopyTask extends AbstractSyncTask {
31
32 private boolean overwrite;
33 private boolean preserveLastModified = true;
34
35 /**
36 * Constructs a new instance.
37 */
38 public CopyTask() {
39 // empty
40 }
41
42 /**
43 * Handles an out-of-date file.
44 *
45 * @param srcFile The source FileObject.
46 * @param destFile The destination FileObject.
47 */
48 @Override
49 protected void handleOutOfDateFile(final FileObject srcFile, final FileObject destFile) throws FileSystemException {
50 log("Copying " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
51 destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
52 if (preserveLastModified && srcFile.getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)
53 && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
54 final long lastModTime = srcFile.getContent().getLastModifiedTime();
55 destFile.getContent().setLastModifiedTime(lastModTime);
56 }
57 }
58
59 /**
60 * Handles an up-to-date file.
61 *
62 * @param srcFile The source FileObject.
63 * @param destFile The destination FileObject.
64 */
65 @Override
66 protected void handleUpToDateFile(final FileObject srcFile, final FileObject destFile) throws FileSystemException {
67 if (overwrite) {
68 // Copy the file anyway
69 handleOutOfDateFile(srcFile, destFile);
70 }
71 }
72
73 /**
74 * Tests whether overwrite is enabled.
75 *
76 * @return the current value of overwrite
77 */
78 public boolean isOverwrite() {
79 return overwrite;
80 }
81
82 /**
83 * Tests whether preserve last modified is enabled.
84 *
85 * @return the current value of preserveLastModified
86 */
87 public boolean isPreserveLastModified() {
88 return preserveLastModified;
89 }
90
91 /**
92 * Enable/disable overwriting of up-to-date files.
93 *
94 * @param overwrite true if the file should be overwritten.
95 */
96 public void setOverwrite(final boolean overwrite) {
97 this.overwrite = overwrite;
98 }
99
100 /**
101 * Sets preserving last modified time of copied files.
102 *
103 * @param preserveLastModified true if the last modified time should be preserved.
104 */
105 public void setPreserveLastModified(final boolean preserveLastModified) {
106 this.preserveLastModified = preserveLastModified;
107 }
108 }