View Javadoc
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       * Enable/disable overwriting of up-to-date files.
37       *
38       * @param overwrite true if the file should be overwritten.
39       */
40      public void setOverwrite(final boolean overwrite) {
41          this.overwrite = overwrite;
42      }
43  
44      /**
45       * Enable/disable preserving last modified time of copied files.
46       *
47       * @param preserveLastModified true if the last modified time should be preserved.
48       */
49      public void setPreserveLastModified(final boolean preserveLastModified) {
50          this.preserveLastModified = preserveLastModified;
51      }
52  
53      /**
54       * @return the current value of overwrite
55       */
56      public boolean isOverwrite() {
57          return overwrite;
58      }
59  
60      /**
61       * @return the current value of preserveLastModified
62       */
63      public boolean isPreserveLastModified() {
64          return preserveLastModified;
65      }
66  
67      /**
68       * Handles an out-of-date file.
69       *
70       * @param srcFile The source FileObject.
71       * @param destFile The destination FileObject.
72       */
73      @Override
74      protected void handleOutOfDateFile(final FileObjectect.html#FileObject">FileObject srcFile, final FileObject destFile) throws FileSystemException {
75          log("Copying " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
76          destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
77          if (preserveLastModified && srcFile.getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)
78                  && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
79              final long lastModTime = srcFile.getContent().getLastModifiedTime();
80              destFile.getContent().setLastModifiedTime(lastModTime);
81          }
82      }
83  
84      /**
85       * Handles an up-to-date file.
86       *
87       * @param srcFile The source FileObject.
88       * @param destFile The destination FileObject.
89       */
90      @Override
91      protected void handleUpToDateFile(final FileObjectect.html#FileObject">FileObject srcFile, final FileObject destFile) throws FileSystemException {
92          if (overwrite) {
93              // Copy the file anyway
94              handleOutOfDateFile(srcFile, destFile);
95          }
96      }
97  }