Collection of useful utilities for Java games and apps. A lot of interesting utilities that could maybe still find some use if you work with Java...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mightyutils/src/mightypork/utils/files/InstanceLock.java

54 lines
1.1 KiB

package mightypork.utils.files;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
/**
* Instance lock (avoid running twice)
*
* @author Ondřej Hruška (MightyPork)
*/
public class InstanceLock {
@SuppressWarnings("resource")
public static boolean onFile(final File lockFile)
{
try {
lockFile.getParentFile().mkdirs();
final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw");
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
if (fileLock != null) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run()
{
try {
fileLock.release();
randomAccessFile.close();
if (!lockFile.delete()) throw new IOException();
} catch (final Throwable t) {
System.err.println("Unable to remove lock file.");
t.printStackTrace();
}
}
});
return true;
}
return false;
} catch (final IOException e) {
System.err.println("IO error while obtaining lock.");
e.printStackTrace();
return false;
}
}
}