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 moves matching files.
026 * <p>
027 * TODO - Delete matching folders.
028 * </p>
029 */
030public class MoveTask extends CopyTask {
031
032    private boolean tryRename;
033
034    /**
035     * Constructs a new instance.
036     */
037    public MoveTask() {
038        // empty
039    }
040
041    /**
042     * Handles a single source file.
043     */
044    @Override
045    protected void handleOutOfDateFile(final FileObject srcFile, final FileObject destFile) throws FileSystemException {
046        if (!tryRename || !srcFile.canRenameTo(destFile)) {
047            super.handleOutOfDateFile(srcFile, destFile);
048
049            log("Deleting " + srcFile.getPublicURIString());
050            srcFile.delete(Selectors.SELECT_SELF);
051        } else {
052            log("Rename " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
053            srcFile.moveTo(destFile);
054            if (!isPreserveLastModified()
055                    && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
056                destFile.getContent().setLastModifiedTime(System.currentTimeMillis());
057            }
058        }
059    }
060
061    /**
062     * Enable/disable move/rename of file (if possible).
063     *
064     * @param tryRename true if the file should be renamed.
065     */
066    public void setTryRename(final boolean tryRename) {
067        this.tryRename = tryRename;
068    }
069}