}
if ( force || !matching )
{
- transaction.createFile( contents, targetFile.toFile( ), digesters );
+ transaction.createFile( contents, targetFile, digesters );
}
}
else
MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
xpp3Writer.write( writer, v4Model );
- transaction.createFile( writer.toString(), targetFile.toFile(), digesters );
+ transaction.createFile( writer.toString(), targetFile, digesters );
List<String> warnings = translator.getWarnings();
{
if ( testChecksums( artifact, sourceFile ) )
{
- transaction.copyFile( sourceFile.toFile(), targetFile.toFile(), digesters );
+ transaction.copyFile( sourceFile, targetFile, digesters );
}
else
{
mappingWriter.write( writer, metadata );
- transaction.createFile( writer.toString(), file.toFile(), digesters );
+ transaction.createFile( writer.toString(), file, digesters );
}
catch ( IOException e )
{
MavenXpp3Writer pomWriter = new MavenXpp3Writer();
pomWriter.write( strWriter, pom );
- transaction.createFile( strWriter.toString(), pomFile.toFile(), digesters );
+ transaction.createFile( strWriter.toString(), pomFile, digesters );
}
private void addWarning( Artifact artifact, String message )
import org.codehaus.plexus.digest.Digester;
import org.codehaus.plexus.digest.DigesterException;
-import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.*;
+import java.util.stream.Stream;
/**
* Abstract class for the TransactionEvents
public abstract class AbstractTransactionEvent
implements TransactionEvent
{
- private Map<File, File> backups = new HashMap<>();
+ private Map<Path, Path> backups = new HashMap<>();
- private List<File> createdDirs = new ArrayList<>();
+ private List<Path> createdDirs = new ArrayList<>();
- private List<File> createdFiles = new ArrayList<>();
+ private List<Path> createdFiles = new ArrayList<>();
/**
* {@link List}<{@link Digester}>
* @param dir The File directory to be created
* @throws IOException when an unrecoverable error occurred
*/
- protected void mkDirs( File dir )
+ protected void mkDirs( Path dir )
throws IOException
{
- List<File> createDirs = new ArrayList<>();
+ List<Path> createDirs = new ArrayList<>();
- File parent = dir;
- while ( !parent.exists() || !parent.isDirectory() )
+ Path parent = dir;
+ while ( !Files.exists(parent) || !Files.isDirectory(parent) )
{
createDirs.add( parent );
- parent = parent.getParentFile();
+ parent = parent.getParent();
}
while ( !createDirs.isEmpty() )
{
- File directory = createDirs.remove( createDirs.size() - 1 );
-
- if ( directory.mkdir() )
- {
- createdDirs.add( directory );
- }
- else
- {
- throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
- }
+ Path directory = createDirs.remove( createDirs.size() - 1 );
+ Files.createDirectories(directory);
+ createdDirs.add( directory );
}
}
while ( !createdDirs.isEmpty() )
{
- File dir = (File) createdDirs.remove( 0 );
+ Path dir = createdDirs.remove( 0 );
- if ( dir.isDirectory() && dir.list().length == 0 )
+ if ( Files.isDirectory(dir))
{
- FileUtils.deleteDirectory( dir );
+ try(Stream<Path> str = Files.list(dir)) {
+ if (str.count()==0) {
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory(dir);
+ }
+ }
}
else
{
protected void revertFilesCreated()
throws IOException
{
- Iterator<File> it = createdFiles.iterator();
+ Iterator<Path> it = createdFiles.iterator();
while ( it.hasNext() )
{
- File file = (File) it.next();
- file.delete();
+ Path file = it.next();
+ Files.deleteIfExists(file);
it.remove();
}
}
- protected void createBackup( File file )
+ protected void createBackup( Path file )
throws IOException
{
- if ( file.exists() && file.isFile() )
+ if ( Files.exists(file) && Files.isRegularFile(file) )
{
- File backup = File.createTempFile( "temp-", ".backup" );
+ Path backup = Files.createTempFile( "temp-", ".backup" );
- FileUtils.copyFile( file, backup );
+ FileUtils.copyFile( file.toFile(), backup.toFile() );
- backup.deleteOnExit();
+ backup.toFile().deleteOnExit();
backups.put( file, backup );
}
protected void restoreBackups()
throws IOException
{
- for ( Map.Entry<File, File> entry : backups.entrySet() )
+ for ( Map.Entry<Path, Path> entry : backups.entrySet() )
{
- FileUtils.copyFile( entry.getValue(), entry.getKey() );
+ FileUtils.copyFile( entry.getValue().toFile(), entry.getKey().toFile() );
}
}
- protected void restoreBackup( File file )
+ protected void restoreBackup( Path file )
throws IOException
{
- File backup = (File) backups.get( file );
+ Path backup = backups.get( file );
if ( backup != null )
{
- FileUtils.copyFile( backup, file );
+ FileUtils.copyFile( backup.toFile(), file.toFile() );
}
}
* @param force whether existing checksums should be overwritten or not
* @throws IOException
*/
- protected void createChecksums( File file, boolean force )
+ protected void createChecksums( Path file, boolean force )
throws IOException
{
for ( Digester digester : getDigesters() )
{
- File checksumFile = new File( file.getAbsolutePath() + "." + getDigesterFileExtension( digester ) );
- if ( checksumFile.exists() )
+ Path checksumFile = Paths.get(file.toAbsolutePath() + "." + getDigesterFileExtension( digester ) );
+ if ( Files.exists(checksumFile) )
{
if ( !force )
{
try
{
- writeStringToFile( checksumFile, digester.calc( file ) );
+ writeStringToFile( checksumFile, digester.calc( file.toFile() ) );
}
catch ( DigesterException e )
{
/**
* TODO: Remove in favor of using FileUtils directly.
*/
- protected void writeStringToFile( File file, String content )
+ protected void writeStringToFile( Path file, String content )
throws IOException
{
- FileUtils.writeStringToFile( file, content );
+ org.apache.archiva.common.utils.FileUtils.writeStringToFile( file, Charset.defaultCharset(), content );
}
/**
* under the License.
*/
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
-
import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.digest.Digester;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
/**
* Event to copy a file.
*
public class CopyFileEvent
extends AbstractTransactionEvent
{
- private final File source;
+ private final Path source;
- private final File destination;
+ private final Path destination;
/**
*
* @param destination
* @param digesters {@link List}<{@link Digester}> digesters to use for checksumming
*/
- public CopyFileEvent( File source, File destination, List<? extends Digester> digesters )
+ public CopyFileEvent( Path source, Path destination, List<? extends Digester> digesters )
{
super( digesters );
this.source = source;
{
createBackup( destination );
- mkDirs( destination.getParentFile() );
+ mkDirs( destination.getParent() );
- FileUtils.copyFile( source, destination );
+ FileUtils.copyFile( source.toFile(), destination.toFile() );
createChecksums( destination, true );
copyChecksums();
private boolean copyChecksum( String extension )
throws IOException
{
- File checksumSource = new File( source.getAbsolutePath() + "." + extension );
- if ( checksumSource.exists() )
+ Path checksumSource = Paths.get( source.toAbsolutePath() + "." + extension );
+ if ( Files.exists(checksumSource) )
{
- File checksumDestination = new File( destination.getAbsolutePath() + "." + extension );
- FileUtils.copyFile( checksumSource, checksumDestination );
+ Path checksumDestination = Paths.get( destination.toAbsolutePath() + "." + extension );
+ FileUtils.copyFile( checksumSource.toFile(), checksumDestination.toFile() );
return true;
}
return false;
public void rollback()
throws IOException
{
- destination.delete();
+ Files.deleteIfExists(destination);
revertFilesCreated();
* under the License.
*/
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
import org.codehaus.plexus.digest.Digester;
public class CreateFileEvent
extends AbstractTransactionEvent
{
- private final File destination;
+ private final Path destination;
private final String content;
* @param destination
* @param digesters {@link List}<{@link Digester}> digesters to use for checksumming
*/
- public CreateFileEvent( String content, File destination, List<? extends Digester> digesters )
+ public CreateFileEvent( String content, Path destination, List<? extends Digester> digesters )
{
super( digesters );
this.content = content;
{
createBackup( destination );
- mkDirs( destination.getParentFile() );
+ mkDirs( destination.getParent() );
- if ( !destination.exists() && !destination.createNewFile() )
+ if ( !Files.exists(destination))
{
- throw new IOException( "Unable to create new file" );
+ Files.createFile(destination);
}
writeStringToFile( destination, content );
public void rollback()
throws IOException
{
- destination.delete();
+ Files.deleteIfExists(destination);
revertFilesCreated();
* under the License.
*/
-import java.io.File;
+import org.codehaus.plexus.digest.Digester;
+
import java.io.IOException;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
-import org.codehaus.plexus.digest.Digester;
-
/**
* Implement commit/rollback semantics for a set of files.
*
* @param destination
* @param digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming
*/
- public void copyFile( File source, File destination, List<? extends Digester> digesters )
+ public void copyFile(Path source, Path destination, List<? extends Digester> digesters )
{
events.add( new CopyFileEvent( source, destination, digesters ) );
}
* @param destination
* @param digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming
*/
- public void createFile( String content, File destination, List<? extends Digester> digesters )
+ public void createFile( String content, Path destination, List<? extends Digester> digesters )
{
events.add( new CreateFileEvent( content, destination, digesters ) );
}
import org.codehaus.plexus.digest.Md5Digester;
import org.codehaus.plexus.digest.Sha1Digester;
-import java.io.File;
import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
digesters = Arrays.asList( (Digester) new Md5Digester(), (Digester) new Sha1Digester() );
}
- protected void assertChecksumExists( File file, String algorithm )
+ protected void assertChecksumExists(Path file, String algorithm )
{
+
assertChecksum( file, algorithm, true );
}
- protected void assertChecksumDoesNotExist( File file, String algorithm )
+ protected void assertChecksumDoesNotExist( Path file, String algorithm )
{
assertChecksum( file, algorithm, false );
}
- private void assertChecksum( File file, String algorithm, boolean exist )
+ private void assertChecksum( Path file, String algorithm, boolean exist )
{
String msg = exist ? "exists" : "does not exist";
- File checksumFile = new File( file.getPath() + "." + algorithm );
- assertEquals( "Test file " + algorithm + " checksum " + msg, exist, checksumFile.exists() );
+ Path checksumFile = Paths.get( file.toAbsolutePath() + "." + algorithm );
+ assertEquals( "Test file " + algorithm + " checksum " + msg, exist, Files.exists(checksumFile) );
}
- protected void assertChecksumCommit( File file )
+ protected void assertChecksumCommit( Path file )
throws IOException
{
assertChecksumExists( file, "md5" );
assertChecksumExists( file, "sha1" );
}
- protected void assertChecksumRollback( File file )
+ protected void assertChecksumRollback( Path file )
throws IOException
{
assertChecksumDoesNotExist( file, "md5" );
assertChecksumDoesNotExist( file, "sha1" );
}
- protected String readFile( File file )
+ protected String readFile( Path file )
throws IOException
{
- return FileUtils.readFileToString( file );
+ return FileUtils.readFileToString( file.toFile() );
}
- protected void writeFile( File file, String content )
+ protected void writeFile( Path file, String content )
throws IOException
{
- FileUtils.writeStringToFile( file, content );
+ org.apache.archiva.common.utils.FileUtils.writeStringToFile( file, Charset.defaultCharset(), content );
}
}
* under the License.
*/
-import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
/**
*/
public class CopyFileEventTest
extends AbstractFileEventTest
{
- private File testDir = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/copy-file" );
+ private Path testDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/copy-file");
- private File testDest = new File( testDir, "test-file.txt" );
+ private Path testDest = testDir.resolve( "test-file.txt" );
- private File testSource = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/test-file.txt" );
+ private Path testSource = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/test-file.txt" );
- private File testDestChecksum;
+ private Path testDestChecksum;
private String source, oldChecksum;
{
super.setUp();
- testSource.getParentFile().mkdirs();
+ Files.createDirectories(testSource.getParent());
- testSource.createNewFile();
+ Files.createFile(testSource);
writeFile( testSource, "source contents" );
- testDestChecksum = new File( testDest.getPath() + ".sha1" );
+ testDestChecksum = Paths.get( testDest.toAbsolutePath() + ".sha1" );
- testDestChecksum.getParentFile().mkdirs();
+ Files.createDirectories(testDestChecksum.getParent());
- testDestChecksum.createNewFile();
+ Files.createFile(testDestChecksum);
writeFile( testDestChecksum, "this is the checksum" );
- assertTrue( "Test if the source exists", testSource.exists() );
+ assertTrue( "Test if the source exists", Files.exists(testSource) );
- assertTrue( "Test if the destination checksum exists", testDestChecksum.exists() );
+ assertTrue( "Test if the destination checksum exists", Files.exists(testDestChecksum) );
source = readFile( testSource );
{
CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
- assertFalse( "Test that the destination is not yet created", testDest.exists() );
+ assertFalse( "Test that the destination is not yet created", Files.exists(testDest) );
event.commit();
- assertTrue( "Test that the destination is created", testDest.exists() );
+ assertTrue( "Test that the destination is created", Files.exists(testDest) );
assertChecksumCommit( testDest );
event.rollback();
- assertFalse( "Test that the destination file has been deleted", testDest.exists() );
+ assertFalse( "Test that the destination file has been deleted", Files.exists(testDest) );
assertChecksumRollback( testDest );
}
public void testCopyCommitRollbackWithBackup()
throws Exception
{
- testDest.getParentFile().mkdirs();
+ Files.createDirectories(testDest.getParent());
- testDest.createNewFile();
+ Files.createFile(testDest);
writeFile( testDest, "overwritten contents" );
- assertTrue( "Test that the destination exists", testDest.exists() );
+ assertTrue( "Test that the destination exists", Files.exists(testDest) );
CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
{
CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters );
- assertFalse( "Test that the destination is not yet created", testDest.exists() );
+ assertFalse( "Test that the destination is not yet created", Files.exists(testDest) );
event.rollback();
- assertFalse( "Test that the destination file is not yet created", testDest.exists() );
+ assertFalse( "Test that the destination file is not yet created", Files.exists(testDest) );
event.commit();
- assertTrue( "Test that the destination is created", testDest.exists() );
+ assertTrue( "Test that the destination is created", Files.exists(testDest) );
assertChecksumCommit( testDest );
{
super.tearDown();
- FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
}
@Override
- protected void assertChecksumCommit( File file )
+ protected void assertChecksumCommit( Path file )
throws IOException
{
super.assertChecksumCommit( file );
}
@Override
- protected void assertChecksumRollback( File file )
+ protected void assertChecksumRollback( Path file )
throws IOException
{
assertChecksumDoesNotExist( file, "md5" );
* under the License.
*/
-import java.io.File;
-import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Test;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
/**
*/
public class CreateFileEventTest
extends AbstractFileEventTest
{
- private File testDir = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/create-file" );
+ private Path testDir = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests/create-file" );
@Test
public void testCreateCommitRollback()
throws Exception
{
- File testFile = new File( testDir, "test-file.txt" );
+ Path testFile = testDir.resolve("test-file.txt" );
CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
- assertFalse( "Test file is not yet created", testFile.exists() );
+ assertFalse( "Test file is not yet created", Files.exists(testFile) );
event.commit();
- assertTrue( "Test file has been created", testFile.exists() );
+ assertTrue( "Test file has been created", Files.exists(testFile) );
assertChecksumCommit( testFile );
event.rollback();
- assertFalse( "Test file is has been deleted after rollback", testFile.exists() );
+ assertFalse( "Test file is has been deleted after rollback", Files.exists(testFile) );
assertChecksumRollback( testFile );
- assertFalse( "Test file parent directories has been rolledback too", testDir.exists() );
- assertTrue( "target directory still exists", new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target" ).exists() );
+ assertFalse( "Test file parent directories has been rolledback too", Files.exists(testDir) );
+ assertTrue( "target directory still exists", Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target" )) );
}
@Test
public void testCreateCommitRollbackWithBackup()
throws Exception
{
- File testFile = new File( testDir, "test-file.txt" );
+ Path testFile = testDir.resolve( "test-file.txt" );
- testFile.getParentFile().mkdirs();
+ Files.createDirectories(testFile.getParent());
- testFile.createNewFile();
+ Files.createFile(testFile);
writeFile( testFile, "original contents" );
public void testCreateRollbackCommit()
throws Exception
{
- File testFile = new File( testDir, "test-file.txt" );
+ Path testFile = testDir.resolve( "test-file.txt" );
CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters );
- assertFalse( "Test file is not yet created", testFile.exists() );
+ assertFalse( "Test file is not yet created", Files.exists(testFile) );
event.rollback();
- assertFalse( "Test file is not yet created", testFile.exists() );
+ assertFalse( "Test file is not yet created", Files.exists(testFile) );
event.commit();
- assertTrue( "Test file is not yet created", testFile.exists() );
+ assertTrue( "Test file is not yet created", Files.exists(testFile) );
assertChecksumCommit( testFile );
}
{
super.tearDown();
- FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/transaction-tests" ) );
}
}