001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.vfs2.tasks;
018
019 import org.apache.commons.vfs2.Capability;
020 import org.apache.commons.vfs2.FileObject;
021 import org.apache.commons.vfs2.FileSystemException;
022 import org.apache.commons.vfs2.Selectors;
023
024 /**
025 * An Ant task that copies matching files.
026 *
027 * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
028 * @todo Copy folders that do not contain files
029 */
030 public class CopyTask
031 extends AbstractSyncTask
032 {
033 private boolean overwrite;
034 private boolean preserveLastModified = true;
035
036 /**
037 * Enable/disable overwriting of up-to-date files.
038 * @param overwrite true if the file should be overwritten.
039 */
040 public void setOverwrite(boolean overwrite)
041 {
042 this.overwrite = overwrite;
043 }
044
045 /**
046 * Enable/disable preserving last modified time of copied files.
047 * @param preserveLastModified true if the last modified time should be preserved.
048 */
049 public void setPreserveLastModified(boolean preserveLastModified)
050 {
051 this.preserveLastModified = preserveLastModified;
052 }
053
054 /**
055 * @return the current value of overwrite
056 */
057 public boolean isOverwrite()
058 {
059 return overwrite;
060 }
061
062 /**
063 * @return the current value of preserveLastModified
064 */
065 public boolean isPreserveLastModified()
066 {
067 return preserveLastModified;
068 }
069
070 /**
071 * Handles an out-of-date file.
072 * @param srcFile The source FileObject.
073 * @param destFile The destination FileObject.
074 */
075 @Override
076 protected void handleOutOfDateFile(final FileObject srcFile,
077 final FileObject destFile)
078 throws FileSystemException
079 {
080 log("Copying " + srcFile + " to " + destFile);
081 destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
082 if (preserveLastModified
083 && srcFile.getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)
084 && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE))
085 {
086 final long lastModTime = srcFile.getContent().getLastModifiedTime();
087 destFile.getContent().setLastModifiedTime(lastModTime);
088 }
089 }
090
091 /**
092 * Handles an up-to-date file.
093 * @param srcFile The source FileObject.
094 * @param destFile The destination FileObject.
095 */
096 @Override
097 protected void handleUpToDateFile(final FileObject srcFile,
098 final FileObject destFile)
099 throws FileSystemException
100 {
101 if (overwrite)
102 {
103 // Copy the file anyway
104 handleOutOfDateFile(srcFile, destFile);
105 }
106 }
107 }