Initial source import.

This commit is contained in:
2014-05-29 20:45:10 +02:00
commit 400ee6a079
411 changed files with 18552 additions and 0 deletions
@@ -0,0 +1,54 @@
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;
}
}
}