You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2006/02/15 00:51:17 UTC

svn commit: r377892 - in /maven/shared/trunk/file-management: ./ src/main/java/org/apache/maven/shared/model/fileset/util/ src/test/java/org/apache/maven/shared/model/fileset/util/

Author: jdcasey
Date: Tue Feb 14 15:51:15 2006
New Revision: 377892

URL: http://svn.apache.org/viewcvs?rev=377892&view=rev
Log:
Changed the FileSetUtils to FileSetManager, and added a constructor that passes in a monitor from maven-shared-monitor and a flag on whether to provide verbose output.

Added:
    maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java   (with props)
Removed:
    maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetUtils.java
Modified:
    maven/shared/trunk/file-management/pom.xml
    maven/shared/trunk/file-management/src/test/java/org/apache/maven/shared/model/fileset/util/FileSetUtilsTest.java

Modified: maven/shared/trunk/file-management/pom.xml
URL: http://svn.apache.org/viewcvs/maven/shared/trunk/file-management/pom.xml?rev=377892&r1=377891&r2=377892&view=diff
==============================================================================
--- maven/shared/trunk/file-management/pom.xml (original)
+++ maven/shared/trunk/file-management/pom.xml Tue Feb 14 15:51:15 2006
@@ -25,6 +25,11 @@
   
   <dependencies>
     <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-shared-monitor</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <version>1.0.4</version>

Added: maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
URL: http://svn.apache.org/viewcvs/maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java?rev=377892&view=auto
==============================================================================
--- maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java (added)
+++ maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java Tue Feb 14 15:51:15 2006
@@ -0,0 +1,313 @@
+package org.apache.maven.shared.model.fileset.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.maven.shared.model.fileset.FileSet;
+import org.apache.maven.shared.monitor.Monitor;
+import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.FileUtils;
+
+public class FileSetManager
+{
+    
+    private static final int DELETE_RETRY_SLEEP_MILLIS = 10;
+    private static final String[] EMPTY_STRING_ARRAY = new String[0];
+    
+    private final Monitor monitor;
+    private final boolean verbose;
+
+    public FileSetManager( Monitor monitor, boolean verbose )
+    {
+        this.monitor = monitor;
+        this.verbose = verbose;
+    }
+    
+    public String[] getIncludedFiles( FileSet fileSet )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner != null )
+        {
+            return scanner.getIncludedFiles();
+        }        
+        else
+        {
+            return EMPTY_STRING_ARRAY;
+        }
+    }
+
+    public String[] getIncludedDirectories( FileSet fileSet )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner != null )
+        {
+            return scanner.getIncludedDirectories();
+        }        
+        else
+        {
+            return EMPTY_STRING_ARRAY;
+        }
+    }
+
+    public String[] getExcludedFiles( FileSet fileSet )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner != null )
+        {
+            return scanner.getExcludedFiles();
+        }        
+        else
+        {
+            return EMPTY_STRING_ARRAY;
+        }
+    }
+
+    public String[] getExcludedDirectories( FileSet fileSet )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner != null )
+        {
+            return scanner.getExcludedDirectories();
+        }        
+        else
+        {
+            return EMPTY_STRING_ARRAY;
+        }
+    }
+    
+    public void delete( FileSet fileSet ) throws IOException
+    {
+        Set deletablePaths = findDeletablePaths( fileSet );
+        
+        for ( Iterator it = deletablePaths.iterator(); it.hasNext(); )
+        {
+            String path = (String) it.next();
+            
+            File file = new File( fileSet.getDirectory(), path );
+            
+            if ( file.exists() )
+            {
+                if ( file.isDirectory() && ( fileSet.isFollowSymlinks() || !isSymlink( file ) ) )
+                {
+                    if ( verbose )
+                    {
+                        monitor.info( "Deleting directory: " + file );
+                    }
+                    
+                    FileUtils.deleteDirectory( file );
+                }
+                else
+                {
+                    if ( verbose )
+                    {
+                        monitor.info( "Deleting file: " + file );
+                    }
+                    
+                    if ( !delete( file ) )
+                    {
+                        throw new IOException( "Failed to delete file: " + file + ". Reason is unknown." );
+                    }
+                }
+            }            
+        }
+    }
+    
+    private boolean isSymlink( File file ) throws IOException
+    {
+        File parent = file.getParentFile();
+        File canonicalFile = file.getCanonicalFile();
+        
+        return parent != null && ( !canonicalFile.getName().equals( file.getName() ) || !canonicalFile.getPath().startsWith( parent.getCanonicalPath() ) );
+    }
+
+    private Set findDeletablePaths( FileSet fileSet )
+    {
+        Set includes = findDeletableDirectories( fileSet );
+        includes.addAll( findDeletableFiles( fileSet, includes ) );
+        
+        return includes;
+    }
+
+    private Set findDeletableDirectories( FileSet fileSet )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner == null )
+        {
+            return Collections.EMPTY_SET;
+        }
+        
+        String[] includedDirs = scanner.getIncludedDirectories();
+        String[] excludedDirs = scanner.getExcludedDirectories();
+        
+        Set includes = new HashSet( Arrays.asList( includedDirs ) );
+        List excludes = new ArrayList( Arrays.asList( excludedDirs ) );
+        List linksForDeletion = new ArrayList();
+        
+        if ( !fileSet.isFollowSymlinks() )
+        {
+            // we need to see which entries were excluded because they're symlinks...
+            scanner.setFollowSymlinks( true );
+            scanner.scan();
+            
+            List notSymlinks = Arrays.asList( scanner.getIncludedDirectories() );
+            
+            linksForDeletion.addAll( excludes );
+            linksForDeletion.retainAll( notSymlinks );
+            
+            excludes.removeAll( notSymlinks );
+        }
+        
+        for ( int i = 0; i < excludedDirs.length; i++ )
+        {
+            String path = excludedDirs[i];
+            
+            File excluded = new File( path );
+            
+            String parentPath = excluded.getParent();
+            
+            while( parentPath != null )
+            {
+                includes.remove( parentPath );
+                
+                parentPath = new File( parentPath ).getParent();
+            }
+        }
+        
+        includes.addAll( linksForDeletion );
+        
+        return includes;
+    }
+
+    private Set findDeletableFiles( FileSet fileSet, Set deletableDirectories )
+    {
+        DirectoryScanner scanner = scan( fileSet );
+        
+        if ( scanner == null )
+        {
+            return deletableDirectories;
+        }
+        
+        String[] includedFiles = scanner.getIncludedFiles();
+        String[] excludedFiles = scanner.getExcludedFiles();
+        
+        Set includes = deletableDirectories;
+        includes.addAll( Arrays.asList( includedFiles ) );
+        List excludes = new ArrayList( Arrays.asList( excludedFiles ) );
+        List linksForDeletion = new ArrayList();
+        
+        if ( !fileSet.isFollowSymlinks() )
+        {
+            // we need to see which entries were excluded because they're symlinks...
+            scanner.setFollowSymlinks( true );
+            scanner.scan();
+            
+            List notSymlinks = Arrays.asList( scanner.getExcludedFiles() );
+            
+            linksForDeletion.addAll( excludes );
+            linksForDeletion.retainAll( notSymlinks );
+            
+            excludes.removeAll( notSymlinks );
+        }
+        
+        for ( int i = 0; i < excludedFiles.length; i++ )
+        {
+            String path = excludedFiles[i];
+            
+            File excluded = new File( path );
+            
+            String parentPath = excluded.getParent();
+            
+            while( parentPath != null )
+            {
+                includes.remove( parentPath );
+                
+                parentPath = new File( parentPath ).getParent();
+            }
+        }
+        
+        includes.addAll( linksForDeletion );
+        
+        for ( Iterator it = includes.iterator(); it.hasNext(); )
+        {
+            String path = (String) it.next();
+            
+            if ( includes.contains( new File( path ).getParent() ) )
+            {
+                it.remove();
+            }
+        }
+        
+        return includes;
+    }
+
+    /**
+     * Accommodate Windows bug encountered in both Sun and IBM JDKs.
+     * Others possible. If the delete does not work, call System.gc(),
+     * wait a little and try again.
+     */
+    private boolean delete( File f )
+    {
+        if ( !f.delete() )
+        {
+            if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) > -1 )
+            {
+                System.gc();
+            }
+            try
+            {
+                Thread.sleep( DELETE_RETRY_SLEEP_MILLIS );
+                return f.delete();
+            }
+            catch ( InterruptedException ex )
+            {
+                return f.delete();
+            }
+        }
+        
+        return true;
+    }
+
+    private DirectoryScanner scan( FileSet fileSet )
+    {
+        File basedir = new File( fileSet.getDirectory() );
+        if ( !basedir.exists() )
+        {
+            return null;
+        }
+        
+        DirectoryScanner scanner = new DirectoryScanner();
+        
+        String[] includesArray = fileSet.getIncludesArray();
+        String[] excludesArray = fileSet.getExcludesArray();
+        
+        if ( includesArray.length < 1 && excludesArray.length < 1 )
+        {
+            scanner.setIncludes( new String[]{ "**" } );
+        }
+        else
+        {
+            scanner.setIncludes( includesArray );
+            scanner.setExcludes( excludesArray );
+        }
+        
+        scanner.setBasedir( basedir );
+        scanner.setFollowSymlinks( fileSet.isFollowSymlinks() );
+
+        scanner.scan();
+        
+        return scanner;
+    }
+
+}

Propchange: maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/shared/trunk/file-management/src/test/java/org/apache/maven/shared/model/fileset/util/FileSetUtilsTest.java
URL: http://svn.apache.org/viewcvs/maven/shared/trunk/file-management/src/test/java/org/apache/maven/shared/model/fileset/util/FileSetUtilsTest.java?rev=377892&r1=377891&r2=377892&view=diff
==============================================================================
--- maven/shared/trunk/file-management/src/test/java/org/apache/maven/shared/model/fileset/util/FileSetUtilsTest.java (original)
+++ maven/shared/trunk/file-management/src/test/java/org/apache/maven/shared/model/fileset/util/FileSetUtilsTest.java Tue Feb 14 15:51:15 2006
@@ -13,6 +13,8 @@
 import junit.framework.TestCase;
 
 import org.apache.maven.shared.model.fileset.FileSet;
+import org.apache.maven.shared.monitor.BasicMonitor;
+import org.apache.maven.shared.monitor.Monitor;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.cli.CommandLineException;
 import org.codehaus.plexus.util.cli.Commandline;
@@ -50,7 +52,10 @@
         set.setDirectory( directory.getPath() );
         set.addInclude( "**/included.txt" );
         
-        String[] included = FileSetUtils.getIncludedFiles( set );
+        Monitor monitor = new BasicMonitor( System.out );
+        FileSetManager fileSetManager = new FileSetManager( monitor, true );
+        
+        String[] included = fileSetManager.getIncludedFiles( set );
         
         Assert.assertEquals( 1, included.length );
     }
@@ -72,7 +77,10 @@
             set.addInclude( "**/included.txt" );
             set.setFollowSymlinks( false );
             
-            String[] included = FileSetUtils.getIncludedFiles( set );
+            Monitor monitor = new BasicMonitor( System.out );
+            FileSetManager fileSetManager = new FileSetManager( monitor, true );
+            
+            String[] included = fileSetManager.getIncludedFiles( set );
             
             Assert.assertEquals( 1, included.length );
         }        
@@ -96,7 +104,10 @@
             set.addInclude( "**/linked-to-self" );
             set.setFollowSymlinks( false );
             
-            FileSetUtils.delete( set );
+            Monitor monitor = new BasicMonitor( System.out );
+            FileSetManager fileSetManager = new FileSetManager( monitor, true );
+            
+            fileSetManager.delete( set );
             
             Assert.assertFalse( subdir.exists() );
         }        
@@ -112,7 +123,10 @@
         set.addInclude( "**/included.txt" );
         set.addInclude( "**/subdir" );
         
-        FileSetUtils.delete( set );
+        Monitor monitor = new BasicMonitor( System.out );
+        FileSetManager fileSetManager = new FileSetManager( monitor, true );
+        
+        fileSetManager.delete( set );
         
         Assert.assertFalse( "file in marked subdirectory still exists.", subdirFile.exists() );
     }