net.sourceforge.cobertura.util

Class FileLocker

public class FileLocker extends 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.

Author: John Lewis Mark Doliner

Field Summary
Objectlock
An object of type FileLock, created using reflection.
ObjectlockChannel
An object of type FileChannel, created using reflection.
FilelockFile
A file called "filename.lock" that resides in the same directory as "filename"
Constructor Summary
FileLocker(File file)
Method Summary
static ObjectcloseChannel(Object channel)
booleanlock()
Obtains a lock on the file.
voidrelease()
Releases the lock on the file.
static ObjectreleaseFileLock(Object lock)

Field Detail

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 Detail

FileLocker

public FileLocker(File file)

Method Detail

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)