import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
-import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.channels.ClosedChannelException;
+import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.Path;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
// TODO currently we create lock for read and write!!
// the idea could be to store lock here with various clients read/write
// only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
- private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
+ private static final ConcurrentMap<Path, Lock> lockFiles = new ConcurrentHashMap<Path, Lock>( 64 );
private boolean skipLocking = true;
@Override
- public Lock readFileLock( File file )
+ public Lock readFileLock( Path file )
throws FileLockException, FileLockTimeoutException
{
if ( skipLocking )
}
StopWatch stopWatch = new StopWatch();
boolean acquired = false;
- mkdirs( file.getParentFile() );
+ try {
+ mkdirs(file.getParent());
+ } catch (IOException e) {
+ throw new FileLockException("Could not create directories "+file.getParent(), e);
+ }
Lock lock = null;
lock=null;
}
}
- catch ( FileNotFoundException e )
+ catch ( FileNotFoundException | NoSuchFileException e )
{
- // can happen if an other thread has deleted the file
- // close RandomAccessFile!!!
- if ( lock != null )
- {
- closeQuietly( lock.getRandomAccessFile() );
- }
+
log.debug( "read Lock skip: {} try to create file", e.getMessage() );
createNewFileQuietly( file );
}
@Override
- public Lock writeFileLock( File file )
+ public Lock writeFileLock( Path file )
throws FileLockException, FileLockTimeoutException
{
if ( skipLocking )
return new Lock( file );
}
- mkdirs( file.getParentFile() );
+ try {
+ mkdirs( file.getParent() );
+ } catch (IOException e) {
+ throw new FileLockException("Could not create directory "+file.getParent(), e);
+ }
StopWatch stopWatch = new StopWatch();
boolean acquired = false;
lock=null;
}
}
- catch ( FileNotFoundException e )
+ catch ( FileNotFoundException | NoSuchFileException e )
{
- // can happen if an other thread has deleted the file
- // close RandomAccessFile!!!
- if ( lock != null )
- {
- closeQuietly( lock.getRandomAccessFile() );
- }
+
log.debug( "write Lock skip: {} try to create file", e.getMessage() );
createNewFileQuietly( file );
}
}
- private void closeQuietly( RandomAccessFile randomAccessFile )
- {
- if ( randomAccessFile == null )
- {
- return;
- }
-
- try
- {
- randomAccessFile.close();
- }
- catch ( IOException e )
- {
- // ignore
- }
- }
-
- private void createNewFileQuietly( File file )
+ private void createNewFileQuietly( Path file )
{
try
{
- file.createNewFile();
+ Files.createFile(file);
}
catch ( IOException e )
{
lockFiles.clear();
}
- private boolean mkdirs( File directory )
- {
- return directory.mkdirs();
+ private Path mkdirs( Path directory ) throws IOException {
+ return Files.createDirectories(directory);
}
@Override
* under the License.
*/
-import java.io.File;
import java.io.FileNotFoundException;
+import java.nio.file.Path;
/**
* @author Olivier Lamy
* @throws FileLockException
* @throws FileLockTimeoutException
*/
- Lock writeFileLock( File file )
+ Lock writeFileLock( Path file )
throws FileLockException, FileLockTimeoutException;
/**
* @throws FileLockException
* @throws FileLockTimeoutException
*/
- Lock readFileLock( File file )
+ Lock readFileLock( Path file )
throws FileLockException, FileLockTimeoutException;
void release( Lock lock )
*/
import java.io.Closeable;
-import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
*/
public class Lock
{
- private File file;
+ private Path file;
private AtomicBoolean write;
private FileLock fileLock;
- private RandomAccessFile randomAccessFile;
-
private FileChannel fileChannel;
- public Lock( File file )
+ public Lock( Path file )
{
this.file = file;
}
- public Lock( File file, boolean write )
- throws FileNotFoundException
+ public Lock( Path file, boolean write )
+ throws IOException
{
this.file = file;
this.write = new AtomicBoolean( write );
- randomAccessFile = new RandomAccessFile( file, write ? "rw" : "r" );
- fileChannel = randomAccessFile.getChannel();
+ fileChannel = write ? FileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.READ) : FileChannel.open(file, StandardOpenOption.READ);
}
- public File getFile()
+ public Path getFile()
{
return file;
}
return write;
}
- public void setFile( File file )
+ public void setFile( Path file )
{
this.file = file;
}
}
closeQuietly( fileChannel );
- closeQuietly( randomAccessFile );
fileClients.remove( Thread.currentThread() );
}
- protected RandomAccessFile getRandomAccessFile()
- {
- return randomAccessFile;
- }
+
private void closeQuietly( Closeable closeable )
{
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
FileLockManager fileLockManager;
- File file = new File(System.getProperty("buildDirectory"), "foo.txt");
+ Path file = Paths.get(System.getProperty("buildDirectory"), "foo.txt");
- File largeJar = new File(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
+ Path largeJar = Paths.get(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
ConcurrentFileWrite(FileLockManager fileLockManager)
throws IOException {
logger.info("thread1");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
logger.info("thread2");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
Path outFile = null;
try {
outFile = Files.createTempFile("foo", ".jar");
- Files.copy(Paths.get(lock.getFile().getPath()),
+ Files.copy(lock.getFile(),
Files.newOutputStream(outFile));
} finally {
fileLockManager.release(lock);
logger.info("thread4");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
logger.info("thread5");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
Path outFile = null;
try {
outFile = Files.createTempFile("foo", ".jar");
- Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
+ Files.copy(lock.getFile(), Files.newOutputStream( outFile ));
} finally {
fileLockManager.release(lock);
if (outFile!=null) Files.delete( outFile );
logger.info("thread7");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
Path outFile = null;
try {
outFile = Files.createTempFile("foo", ".jar");
- Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
+ Files.copy(lock.getFile(), Files.newOutputStream( outFile ));
} finally {
fileLockManager.release(lock);
if (outFile!=null) Files.delete( outFile );
logger.info("thread9");
Lock lock = fileLockManager.writeFileLock(this.file);
try {
- lock.getFile().delete();
- copyFile(largeJar.toPath(), lock.getFile().toPath());
+ Files.deleteIfExists(lock.getFile());
+ copyFile(largeJar, lock.getFile());
} finally {
fileLockManager.release(lock);
}
Path outFile = null;
try {
outFile = Files.createTempFile("foo", ".jar");
- Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
+ Files.copy(lock.getFile(), Files.newOutputStream( outFile ));
} finally {
fileLockManager.release(lock);
if (outFile!=null) Files.delete(outFile);
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.File;
import java.io.IOException;
-import java.nio.file.FileSystemException;
-import java.nio.file.Files;
-import java.nio.file.StandardCopyOption;
+import java.nio.file.*;
/**
* @author Olivier Lamy
{
try {
- File file = new File(System.getProperty("buildDirectory"), "foo.txt");
+ Path file = Paths.get(System.getProperty("buildDirectory"), "foo.txt");
- Files.deleteIfExists(file.toPath());
+ Files.deleteIfExists(file);
- File largeJar = new File(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
+ Path largeJar = Paths.get(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
Lock lock = fileLockManager.writeFileLock(file);
try {
- Files.copy(largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
+ Files.copy(largeJar, lock.getFile(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.warn("Copy failed {}", e.getMessage());
// On windows a FileSystemException is thrown
Lock lock;
try
{
- lock = fileLockManager.writeFileLock( target.toFile() );
- if ( lock.getFile().exists() && !lock.getFile().delete() )
- {
+ lock = fileLockManager.writeFileLock( target );
+ try {
+ Files.deleteIfExists(lock.getFile());
+ } catch (IOException e) {
throw new ProxyException( "Unable to overwrite existing target file: " + target.toAbsolutePath() );
}
- lock.getFile().getParentFile().mkdirs();
+ try {
+ Files.createDirectories(lock.getFile().getParent());
+ } catch (IOException e) {
+ throw new ProxyException("Unable to create parent directory "+lock.getFile().getParent());
+ }
try
{
- Files.move(temp, lock.getFile().toPath() );
+ Files.move(temp, lock.getFile() );
}
catch ( IOException e )
{
try
{
- Files.copy( temp, lock.getFile().toPath() );
+ Files.copy( temp, lock.getFile());
}
catch ( IOException e2 )
{
- if ( lock.getFile().exists() )
+ if ( Files.exists(lock.getFile()) )
{
log.debug( "Tried to copy file {} to {} but file with this name already exists.",
- temp.getFileName(), lock.getFile().getAbsolutePath() );
+ temp.getFileName(), lock.getFile().toAbsolutePath() );
}
else
{
{
if ( !isCollection() && outputContext.hasStream() )
{
- Lock lock = fileLockManager.readFileLock( localResource.toFile() );
- try (InputStream is = Files.newInputStream( lock.getFile().toPath() ))
+ Lock lock = fileLockManager.readFileLock( localResource );
+ try (InputStream is = Files.newInputStream( lock.getFile()))
{
IOUtils.copy( is, outputContext.getOutputStream() );
}