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 */
017package org.apache.commons.vfs2.tasks;
018
019import org.apache.commons.vfs2.Capability;
020import org.apache.commons.vfs2.FileObject;
021import org.apache.commons.vfs2.FileSystemException;
022import org.apache.commons.vfs2.Selectors;
023
024/**
025 * An Ant task that copies matching files.
026 *
027 * @todo Copy folders that do not contain files
028 */
029public class CopyTask
030    extends AbstractSyncTask
031{
032    private boolean overwrite;
033    private boolean preserveLastModified = true;
034
035    /**
036     * Enable/disable overwriting of up-to-date files.
037     * @param overwrite true if the file should be overwritten.
038     */
039    public void setOverwrite(final boolean overwrite)
040    {
041        this.overwrite = overwrite;
042    }
043
044    /**
045     * Enable/disable preserving last modified time of copied files.
046     * @param preserveLastModified true if the last modified time should be preserved.
047     */
048    public void setPreserveLastModified(final boolean preserveLastModified)
049    {
050        this.preserveLastModified = preserveLastModified;
051    }
052
053    /**
054     * @return the current value of overwrite
055     */
056    public boolean isOverwrite()
057    {
058        return overwrite;
059    }
060
061    /**
062     * @return the current value of preserveLastModified
063     */
064    public boolean isPreserveLastModified()
065    {
066        return preserveLastModified;
067    }
068
069    /**
070     * Handles an out-of-date file.
071     * @param srcFile The source FileObject.
072     * @param destFile The destination FileObject.
073     */
074    @Override
075    protected void handleOutOfDateFile(final FileObject srcFile,
076                                       final FileObject destFile)
077        throws FileSystemException
078    {
079        log("Copying " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
080        destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
081        if (preserveLastModified
082            && srcFile.getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)
083            && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE))
084        {
085            final long lastModTime = srcFile.getContent().getLastModifiedTime();
086            destFile.getContent().setLastModifiedTime(lastModTime);
087        }
088    }
089
090    /**
091     * Handles an up-to-date file.
092     * @param srcFile The source FileObject.
093     * @param destFile The destination FileObject.
094     */
095    @Override
096    protected void handleUpToDateFile(final FileObject srcFile,
097                                      final FileObject destFile)
098        throws FileSystemException
099    {
100        if (overwrite)
101        {
102            // Copy the file anyway
103            handleOutOfDateFile(srcFile, destFile);
104        }
105    }
106}