net.sourceforge.cobertura.util

Class FileLocker


public class FileLocker
extends java.lang.Object

This class controls access to any file so that multiple JVMs will not be able to write to the file at the same time. A file called "filename.lock" is created and Java's FileLock class is used to lock the file. The java.nio classes were introduced in Java 1.4, so this class does a no-op when used with Java 1.3. The class maintains compatability with Java 1.3 by accessing the java.nio classes using reflection.
Authors:
John Lewis
Mark Doliner

Field Summary

private Object
lock
An object of type FileLock, created using reflection.
private Object
lockChannel
An object of type FileChannel, created using reflection.
private File
lockFile
A file called "filename.lock" that resides in the same directory as "filename"

Constructor Summary

FileLocker(File file)

Method Summary

private static Object
closeChannel(Object channel)
boolean
lock()
Obtains a lock on the file.
void
release()
Releases the lock on the file.
private static Object
releaseFileLock(Object lock)

Field Details

lock

private Object lock
An object of type FileLock, created using reflection.

lockChannel

private Object lockChannel
An object of type FileChannel, created using reflection.

lockFile

private File lockFile
A file called "filename.lock" that resides in the same directory as "filename"

Constructor Details

FileLocker

public FileLocker(File file)

Method Details

closeChannel

private static Object closeChannel(Object channel)

lock

public boolean lock()
Obtains a lock on the file. This blocks until the lock is obtained.

release

public void release()
Releases the lock on the file.

releaseFileLock

private static Object releaseFileLock(Object lock)