adammurdoch 2002/10/23 19:11:03
Modified: vfs/src/java/org/apache/commons/vfs AllFileSelector.java
FileDepthSelector.java Resources.properties
vfs/src/java/org/apache/commons/vfs/tasks
AbstractSyncTask.java CopyTask.java DeleteTask.java
MoveTask.java VfsTask.java tasks.properties
Added: vfs/src/java/org/apache/commons/vfs/tasks MkdirTask.java
SyncTask.java
Log:
- AbstractSyncTask:
- Compares last-modified time of source and destination files.
- Warns when more than one source file maps to the same destination file.
- Added support for handling destination files with no corresponding source file.
- Warns if a source file does not exist, rather than throwing an exception.
- Added Sync task, which synchronises the destination directory to make it
look like the source directories.
- Added 'overwrite' and 'preservelastmodified' to copy task (and move and sync).
- Delete task now actually deletes stuff.
- Added mkdir task.
Revision Changes Path
1.4 +1 -1
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/AllFileSelector.java
Index: AllFileSelector.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/AllFileSelector.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AllFileSelector.java 23 Oct 2002 11:59:39 -0000 1.3
+++ AllFileSelector.java 24 Oct 2002 02:11:03 -0000 1.4
@@ -56,7 +56,7 @@
package org.apache.commons.vfs;
/**
- * A {@link FileSelector} which selects everything.
+ * A {@link FileSelector} that selects everything.
*
* @author <a href="mailto:adammurdoch@;apache.org">Adam Murdoch</a>
* @version $Revision$ $Date$
1.5 +1 -1
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileDepthSelector.java
Index: FileDepthSelector.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileDepthSelector.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FileDepthSelector.java 23 Oct 2002 11:59:39 -0000 1.4
+++ FileDepthSelector.java 24 Oct 2002 02:11:03 -0000 1.5
@@ -56,7 +56,7 @@
package org.apache.commons.vfs;
/**
- * A {@link FileSelector} which selects all files in a particular depth range.
+ * A {@link FileSelector} that selects all files in a particular depth range.
*
* @author <a href="mailto:adammurdoch@;apache.org">Adam Murdoch</a>
* @version $Revision$ $Date$
1.6 +2 -1
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties
Index: Resources.properties
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Resources.properties 23 Oct 2002 13:12:14 -0000 1.5
+++ Resources.properties 24 Oct 2002 02:11:03 -0000 1.6
@@ -114,5 +114,6 @@
vfs.tasks/sync.no-source-file.error=No source file specified.
vfs.tasks/sync.too-many-source-files.error=Too many source files specified.
vfs.tasks/sync.source-not-file.error=Source file "{0}" is not a file, or does not
exist.
-vfs.tasks/sync.src-file-no-exist.error=Source file "{0}" does not exist.
+vfs.tasks/sync.src-file-no-exist.warn=Source file "{0}" does not exist.
+vfs.tasks/sync.duplicate-source-files.warn=Multiple source files for destination
file "{0}".
vfs.tasks/delete.no-source-files.error=No files to delete specified.
1.3 +107 -19
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/AbstractSyncTask.java
Index: AbstractSyncTask.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/AbstractSyncTask.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractSyncTask.java 23 Oct 2002 11:59:42 -0000 1.2
+++ AbstractSyncTask.java 24 Oct 2002 02:11:03 -0000 1.3
@@ -56,13 +56,14 @@
package org.apache.commons.vfs.tasks;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
-import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.NameScope;
import org.apache.commons.vfs.Selectors;
-import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.util.Messages;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
@@ -80,14 +81,13 @@
* @author <a href="mailto:adammurdoch@;apache.org">Adam Murdoch</a>
* @version $Revision$ $Date$
*
- * @todo Detect collisions when more than one source files map to the same dest file
* @todo Deal with case where dest file maps to a child of one of the source files
- * @todo Scan destination directory
- * @todo Use visitors
- * @todo Check last modified time
* @todo Deal with case where dest file already exists and is incorrect type (not
file, not a folder)
+ * @todo Use visitors
* @todo Add default excludes
* @todo Allow selector, mapper, filters, etc to be specified.
+ * @todo Handle source/dest directories as well
+ * @todo Allow selector to be specified for choosing which dest files to sync
*/
public abstract class AbstractSyncTask
extends VfsTask
@@ -203,15 +203,17 @@
if ( !srcFile.exists() )
{
final String message =
- Messages.getString( "vfs.tasks/sync.src-file-no-exist.error",
srcFile );
- throw new BuildException( message );
+ Messages.getString( "vfs.tasks/sync.src-file-no-exist.warn",
srcFile );
+ log( message, Project.MSG_WARN );
+ }
+ else
+ {
+ srcs.add( srcFile );
}
- srcs.add( srcFile );
}
- // Scan the destination files
-
// Scan the source files
+ final Set destFiles = new HashSet();
for ( int i = 0; i < srcs.size(); i++ )
{
final FileObject rootFile = (FileObject)srcs.get( i );
@@ -223,7 +225,7 @@
final FileObject destFile = destFolder.resolveFile(
rootName.getBaseName(), NameScope.DESCENDENT );
// Do the copy
- handleFile( rootFile, destFile );
+ handleFile( destFiles, rootFile, destFile );
}
else
{
@@ -241,10 +243,47 @@
destFolder.resolveFile( relName, NameScope.DESCENDENT );
// Do the copy
- handleFile( srcFile, destFile );
+ handleFile( destFiles, srcFile, destFile );
}
}
}
+
+ // Scan the destination files for files with no source file
+ if ( detectMissingSourceFiles() )
+ {
+ final List extraFiles = destFolder.findFiles( Selectors.SELECT_FILES );
+ extraFiles.removeAll( destFiles );
+ final int count = extraFiles.size();
+ for ( int i = 0; i < count; i++ )
+ {
+ final FileObject destFile = (FileObject)extraFiles.get( i );
+ handleMissingSourceFile( destFile );
+ }
+ }
+ }
+
+ /**
+ * Handles a single file, checking for collisions where more than one
+ * source file maps to the same destination file.
+ */
+ private void handleFile( final Set destFiles,
+ final FileObject srcFile,
+ final FileObject destFile ) throws Exception
+
+ {
+ // Check for duplicate source files
+ if ( destFiles.contains( destFile ) )
+ {
+ final String message = Messages.getString(
"vfs.tasks/sync.duplicate-source-files.warn", destFile );
+ log( message, Project.MSG_WARN );
+ }
+ else
+ {
+ destFiles.add( destFile );
+ }
+
+ // Handle the file
+ handleFile( srcFile, destFile );
}
/**
@@ -278,13 +317,62 @@
/**
* Handles a single source file.
- * @param srcFile
- * @param destFile
- * @throws FileSystemException
*/
- protected abstract void handleFile( final FileObject srcFile,
+ private void handleFile( final FileObject srcFile,
+ final FileObject destFile )
+ throws Exception
+ {
+ if ( !destFile.exists()
+ || srcFile.getContent().getLastModifiedTime() >
destFile.getContent().getLastModifiedTime() )
+ {
+ // Destination file is out-of-date
+ handleOutOfDateFile( srcFile, destFile );
+ }
+ else
+ {
+ // Destination file is up-to-date
+ handleUpToDateFile( srcFile, destFile );
+ }
+ }
+
+ /**
+ * Handles an out-of-date file (a file where the destination file
+ * either doesn't exist, or is older than the source file).
+ * This implementation does nothing.
+ */
+ protected void handleOutOfDateFile( final FileObject srcFile,
final FileObject destFile )
- throws FileSystemException;
+ throws Exception
+ {
+ }
+
+ /**
+ * Handles an up-to-date file (where the destination file exists and is
+ * newer than the source file). This implementation does nothing.
+ */
+ protected void handleUpToDateFile( final FileObject srcFile,
+ final FileObject destFile )
+ throws Exception
+ {
+ }
+
+ /**
+ * Handles a destination for which there is no corresponding source file.
+ * This implementation does nothing.
+ */
+ protected void handleMissingSourceFile( final FileObject destFile )
+ throws Exception
+ {
+ }
+
+ /**
+ * Check if this task cares about destination files with a missing source
+ * file. This implementation returns false.
+ */
+ protected boolean detectMissingSourceFiles()
+ {
+ return false;
+ }
/**
* Information about a source file.
1.4 +44 -3
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/CopyTask.java
Index: CopyTask.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/CopyTask.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CopyTask.java 23 Oct 2002 13:09:45 -0000 1.3
+++ CopyTask.java 24 Oct 2002 02:11:03 -0000 1.4
@@ -70,11 +70,52 @@
public class CopyTask
extends AbstractSyncTask
{
- protected void handleFile( final FileObject srcFile,
- final FileObject destFile )
+ private boolean overwrite = false;
+ private boolean preserveLastModified = true;
+
+ /**
+ * Enable/disable overwriting of up-to-date files.
+ */
+ public void setOverwrite( boolean overwrite )
+ {
+ this.overwrite = overwrite;
+ }
+
+ /**
+ * Enable/disable preserving last modified time of copied files.
+ */
+ public void setPreserveLastModified( boolean preserveLastModified )
+ {
+ this.preserveLastModified = preserveLastModified;
+ }
+
+ /**
+ * Handles an out-of-date file.
+ */
+ protected void handleOutOfDateFile( final FileObject srcFile,
+ final FileObject destFile )
throws FileSystemException
{
log( "Copying " + srcFile + " to " + destFile );
destFile.copyFrom( srcFile, Selectors.SELECT_SELF );
+ if ( preserveLastModified )
+ {
+ final long lastModTime = srcFile.getContent().getLastModifiedTime();
+ destFile.getContent().setLastModifiedTime( lastModTime );
+ }
+ }
+
+ /**
+ * Handles an up-to-date file.
+ */
+ protected void handleUpToDateFile( final FileObject srcFile,
+ final FileObject destFile )
+ throws FileSystemException
+ {
+ if ( overwrite )
+ {
+ // Copy the file anyway
+ handleOutOfDateFile( srcFile, destFile );
+ }
}
}
1.3 +2 -2
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/DeleteTask.java
Index: DeleteTask.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/DeleteTask.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DeleteTask.java 23 Oct 2002 11:59:42 -0000 1.2
+++ DeleteTask.java 24 Oct 2002 02:11:03 -0000 1.3
@@ -97,7 +97,7 @@
{
final FileObject srcFile = resolveFile( file );
log( "Deleting " + srcFile );
- //srcFile.delete( Selectors.SELECT_ALL );
+ srcFile.delete( Selectors.SELECT_ALL );
}
catch ( final Exception e )
{
1.4 +4 -4
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/MoveTask.java
Index: MoveTask.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/MoveTask.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MoveTask.java 23 Oct 2002 13:09:45 -0000 1.3
+++ MoveTask.java 24 Oct 2002 02:11:03 -0000 1.4
@@ -73,11 +73,11 @@
/**
* Handles a single source file.
*/
- protected void handleFile( final FileObject srcFile,
- final FileObject destFile )
+ protected void handleOutOfDateFile( final FileObject srcFile,
+ final FileObject destFile )
throws FileSystemException
{
- super.handleFile( srcFile, destFile );
+ super.handleOutOfDateFile( srcFile, destFile );
log( "Deleting " + srcFile );
srcFile.delete( Selectors.SELECT_SELF );
}
1.4 +2 -3
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/VfsTask.java
Index: VfsTask.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/VfsTask.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- VfsTask.java 23 Oct 2002 13:12:14 -0000 1.3
+++ VfsTask.java 24 Oct 2002 02:11:03 -0000 1.4
@@ -73,8 +73,7 @@
private FileSystemManager manager;
/**
- * Resolves a URI to a file. Relative URI are resolved relative to the
- * project directory.
+ * Resolves a URI to a file, relative to the project's base directory.
*
* @param uri The URI to resolve.
*/
1.2 +2 -0
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/tasks.properties
Index: tasks.properties
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/tasks.properties,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- tasks.properties 23 Oct 2002 10:58:52 -0000 1.1
+++ tasks.properties 24 Oct 2002 02:11:03 -0000 1.2
@@ -1,4 +1,6 @@
v-copy=org.apache.commons.vfs.tasks.CopyTask
v-move=org.apache.commons.vfs.tasks.MoveTask
+v-sync=org.apache.commons.vfs.tasks.SyncTask
v-delete=org.apache.commons.vfs.tasks.DeleteTask
+v-mkdir=org.apache.commons.vfs.tasks.MkdirTask
v-show-file=org.apache.commons.vfs.tasks.ShowFileTask
1.1
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/MkdirTask.java
Index: MkdirTask.java
===================================================================
/* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.vfs.tasks;
import org.apache.tools.ant.BuildException;
import org.apache.commons.vfs.util.Messages;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.FileSystemException;
/**
* An Ant task that creates a directory.
*
* @author <a href="mailto:adammurdoch@;apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/10/24 02:11:03 $
*/
public class MkdirTask
extends VfsTask
{
private String dirName;
/**
* Sets the directory to create.
* @param dir
*/
public void setDir( final String dir )
{
dirName = dir;
}
/**
* Executes the task.
*/
public void execute() throws BuildException
{
if ( dirName == null )
{
final String message = Messages.getString(
"vfs.tasks/no-directory-specified.error" );
throw new BuildException( message );
}
try
{
final FileObject dir = resolveFile( dirName );
dir.create( FileType.FOLDER );
}
catch ( final FileSystemException e )
{
throw new BuildException( e );
}
}
}
1.1
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/tasks/SyncTask.java
Index: SyncTask.java
===================================================================
/* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.vfs.tasks;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.Selectors;
/**
* A task that synchronises the destination folder to look exactly like
* the source folder (or folders).
*
* @author <a href="mailto:adammurdoch@;apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/10/24 02:11:03 $
*/
public class SyncTask
extends CopyTask
{
/**
* Handles a destination for which there is no corresponding source file.
*/
protected void handleMissingSourceFile( final FileObject destFile )
throws Exception
{
log( "deleting " + destFile );
//destFile.delete( Selectors.SELECT_SELF );
}
/**
* Check if this task cares about destination files with a missing source
* file.
*/
protected boolean detectMissingSourceFiles()
{
return true;
}
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>