You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2007/11/20 14:09:09 UTC
svn commit: r596648 -
/maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
Author: vsiveton
Date: Tue Nov 20 05:09:09 2007
New Revision: 596648
URL: http://svn.apache.org/viewvc?rev=596648&view=rev
Log:
MNG-3294: [file-management] Add a parameter to throw or not IOException in delete( FileSet fileSet )
o added new method delete( FileSet fileSet, boolean failOnError )
Modified:
maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
Modified: maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java?rev=596648&r1=596647&r2=596648&view=diff
==============================================================================
--- maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java (original)
+++ maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java Tue Nov 20 05:09:09 2007
@@ -40,6 +40,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -254,14 +255,26 @@
/**
* Delete the matching files and directories for the given file-set definition.
*
- * @param fileSet
- * The file-set matching rules, along with search base directory
- * @throws IOException
- * If a matching file cannot be deleted
+ * @param fileSet The file-set matching rules, along with search base directory
+ * @throws IOException If a matching file cannot be deleted
*/
public void delete( FileSet fileSet )
throws IOException
{
+ delete( fileSet, true );
+ }
+
+ /**
+ * Delete the matching files and directories for the given file-set definition.
+ *
+ * @param fileSet The file-set matching rules, along with search base directory.
+ * @param throwsError Throw IOException when errors have occurred by deleting files or directories.
+ * @throws IOException If a matching file cannot be deleted and <code>throwsError=true</code>, otherwise
+ * print warning messages.
+ */
+ public void delete( FileSet fileSet, boolean throwsError )
+ throws IOException
+ {
Set deletablePaths = findDeletablePaths( fileSet );
if ( messages != null && messages.isDebugEnabled() )
@@ -270,6 +283,8 @@
.addDebugMessage( "Found deletable paths: " + String.valueOf( deletablePaths ).replace( ',', '\n' ) );
}
+ List warnMessages = new LinkedList();
+
for ( Iterator it = deletablePaths.iterator(); it.hasNext(); )
{
String path = (String) it.next();
@@ -285,7 +300,7 @@
messages.addInfoMessage( "Deleting directory: " + file );
}
- removeDir( file, fileSet.isFollowSymlinks() );
+ removeDir( file, fileSet.isFollowSymlinks(), throwsError, warnMessages );
}
else
{
@@ -296,11 +311,27 @@
if ( !delete( file ) )
{
- throw new IOException( "Failed to delete file: " + file + ". Reason is unknown." );
+ String message = "Failed to delete file " + file.getAbsolutePath() + ". Reason is unknown.";
+ if ( throwsError )
+ {
+ throw new IOException( message );
+ }
+
+ warnMessages.add( message );
}
}
}
}
+
+ if ( messages != null && messages.isWarningEnabled() && !throwsError && ( warnMessages.size() > 0 ) )
+ {
+ for ( Iterator it = warnMessages.iterator(); it.hasNext(); )
+ {
+ String msg = (String) it.next();
+
+ messages.addWarningMessage( msg ).flush();
+ }
+ }
}
private boolean isSymlink( File file )
@@ -509,12 +540,13 @@
/**
* Delete a directory
*
- * @param dir
- * the directory to delete
- * @param followSymlinks
- * whether to follow symbolic links, or simply delete the link
+ * @param dir the directory to delete
+ * @param followSymlinks whether to follow symbolic links, or simply delete the link
+ * @param throwsError Throw IOException when errors have occurred by deleting files or directories.
+ * @param warnMessages A list of warning messages used when <code>throwsError=false</code>.
+ * @throws IOException If a matching file cannot be deleted and <code>throwsError=true</code>.
*/
- private void removeDir( File dir, boolean followSymlinks )
+ private void removeDir( File dir, boolean followSymlinks, boolean throwsError, List warnMessages )
throws IOException
{
String[] list = dir.list();
@@ -522,28 +554,29 @@
{
list = new String[0];
}
+
for ( int i = 0; i < list.length; i++ )
{
String s = list[i];
File f = new File( dir, s );
if ( f.isDirectory() && ( followSymlinks || !isSymlink( f ) ) )
{
- removeDir( f, followSymlinks );
+ removeDir( f, followSymlinks, throwsError, warnMessages );
}
else
{
if ( !delete( f ) )
{
String message = "Unable to delete file " + f.getAbsolutePath();
- // TODO:...
- // if ( failOnError )
- // {
- throw new IOException( message );
- // }
- // else
- // {
- // getLog().info( message );
- // }
+ if ( throwsError )
+ {
+ throw new IOException( message );
+ }
+
+ if ( !warnMessages.contains( message ) )
+ {
+ warnMessages.add( message );
+ }
}
}
}
@@ -551,15 +584,15 @@
if ( !delete( dir ) )
{
String message = "Unable to delete directory " + dir.getAbsolutePath();
- // TODO:...
- // if ( failOnError )
- // {
- throw new IOException( message );
- // }
- // else
- // {
- // getLog().info( message );
- // }
+ if ( throwsError )
+ {
+ throw new IOException( message );
+ }
+
+ if ( !warnMessages.contains( message ) )
+ {
+ warnMessages.add( message );
+ }
}
}