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