Migrating transaction module to java.nio

This commit is contained in:
Martin Stockhammer 2017-09-08 17:12:01 +02:00
parent 9148a900ee
commit 413b6d7f1a
8 changed files with 140 additions and 134 deletions

View File

@ -215,7 +215,7 @@ public class LegacyToDefaultConverter
}
if ( force || !matching )
{
transaction.createFile( contents, targetFile.toFile( ), digesters );
transaction.createFile( contents, targetFile, digesters );
}
}
else
@ -247,7 +247,7 @@ public class LegacyToDefaultConverter
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();
@ -359,7 +359,7 @@ public class LegacyToDefaultConverter
{
if ( testChecksums( artifact, sourceFile ) )
{
transaction.copyFile( sourceFile.toFile(), targetFile.toFile(), digesters );
transaction.copyFile( sourceFile, targetFile, digesters );
}
else
{
@ -557,7 +557,7 @@ public class LegacyToDefaultConverter
mappingWriter.write( writer, metadata );
transaction.createFile( writer.toString(), file.toFile(), digesters );
transaction.createFile( writer.toString(), file, digesters );
}
catch ( IOException e )
{
@ -645,7 +645,7 @@ public class LegacyToDefaultConverter
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 )

View File

@ -23,14 +23,13 @@ import org.apache.commons.io.FileUtils;
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
@ -40,11 +39,11 @@ import java.util.Map;
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}&lt;{@link Digester}&gt;
@ -72,32 +71,25 @@ public abstract class AbstractTransactionEvent
* @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() )
{
Path directory = createDirs.remove( createDirs.size() - 1 );
Files.createDirectories(directory);
createdDirs.add( directory );
}
else
{
throw new IOException( "Failed to create directory: " + directory.getAbsolutePath() );
}
}
}
protected void revertMkDirs()
@ -109,11 +101,15 @@ public abstract class AbstractTransactionEvent
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
{
@ -127,25 +123,25 @@ public abstract class AbstractTransactionEvent
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 );
}
@ -154,19 +150,19 @@ public abstract class AbstractTransactionEvent
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() );
}
}
@ -177,13 +173,13 @@ public abstract class AbstractTransactionEvent
* @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 )
{
@ -198,7 +194,7 @@ public abstract class AbstractTransactionEvent
try
{
writeStringToFile( checksumFile, digester.calc( file ) );
writeStringToFile( checksumFile, digester.calc( file.toFile() ) );
}
catch ( DigesterException e )
{
@ -210,10 +206,10 @@ public abstract class AbstractTransactionEvent
/**
* 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 );
}
/**

View File

@ -19,13 +19,15 @@ package org.apache.archiva.transaction;
* 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.
*
@ -34,9 +36,9 @@ import org.codehaus.plexus.digest.Digester;
public class CopyFileEvent
extends AbstractTransactionEvent
{
private final File source;
private final Path source;
private final File destination;
private final Path destination;
/**
*
@ -44,7 +46,7 @@ public class CopyFileEvent
* @param destination
* @param digesters {@link List}&lt;{@link Digester}&gt; 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;
@ -57,9 +59,9 @@ public class CopyFileEvent
{
createBackup( destination );
mkDirs( destination.getParentFile() );
mkDirs( destination.getParent() );
FileUtils.copyFile( source, destination );
FileUtils.copyFile( source.toFile(), destination.toFile() );
createChecksums( destination, true );
copyChecksums();
@ -91,11 +93,11 @@ public class CopyFileEvent
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;
@ -105,7 +107,7 @@ public class CopyFileEvent
public void rollback()
throws IOException
{
destination.delete();
Files.deleteIfExists(destination);
revertFilesCreated();

View File

@ -19,8 +19,9 @@ package org.apache.archiva.transaction;
* 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;
@ -33,7 +34,7 @@ import org.codehaus.plexus.digest.Digester;
public class CreateFileEvent
extends AbstractTransactionEvent
{
private final File destination;
private final Path destination;
private final String content;
@ -43,7 +44,7 @@ public class CreateFileEvent
* @param destination
* @param digesters {@link List}&lt;{@link Digester}&gt; 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;
@ -56,11 +57,11 @@ public class CreateFileEvent
{
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 );
@ -72,7 +73,7 @@ public class CreateFileEvent
public void rollback()
throws IOException
{
destination.delete();
Files.deleteIfExists(destination);
revertFilesCreated();

View File

@ -19,13 +19,13 @@ package org.apache.archiva.transaction;
* 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.
*
@ -79,7 +79,7 @@ public class FileTransaction
* @param destination
* @param digesters {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; 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 ) );
}
@ -89,7 +89,7 @@ public class FileTransaction
* @param destination
* @param digesters {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; 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 ) );
}

View File

@ -25,8 +25,11 @@ import org.codehaus.plexus.digest.Digester;
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;
@ -53,46 +56,47 @@ public abstract class AbstractFileEventTest
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 );
}
}

View File

@ -19,26 +19,27 @@ package org.apache.archiva.transaction;
* 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;
@ -49,23 +50,23 @@ public class CopyFileEventTest
{
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 );
@ -78,11 +79,11 @@ public class CopyFileEventTest
{
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 );
@ -92,7 +93,7 @@ public class CopyFileEventTest
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 );
}
@ -101,13 +102,13 @@ public class CopyFileEventTest
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 );
@ -138,15 +139,15 @@ public class CopyFileEventTest
{
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 );
@ -162,11 +163,11 @@ public class CopyFileEventTest
{
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 );
@ -177,7 +178,7 @@ public class CopyFileEventTest
}
@Override
protected void assertChecksumRollback( File file )
protected void assertChecksumRollback( Path file )
throws IOException
{
assertChecksumDoesNotExist( file, "md5" );

View File

@ -19,54 +19,56 @@ package org.apache.archiva.transaction;
* 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" );
@ -97,19 +99,19 @@ public class CreateFileEventTest
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 );
}
@ -121,6 +123,6 @@ public class CreateFileEventTest
{
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" ) );
}
}