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