diff options
author | Sven Gothel <[email protected]> | 2011-04-28 11:22:27 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-06-11 01:52:56 +0200 |
commit | f47c44c41ca57d785b4c8026cd087a0810c7fbc8 (patch) | |
tree | b11be57daf0780a653cf4a66fb6892f585cd8e9d | |
parent | 175fd4686a0919fa7c6a933810547f167d84d050 (diff) |
Unit Test Framework SingletonInstance: setup file unlock/delete asap
-rw-r--r-- | src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java b/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java index 7e3b9eb..90acd7b 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java +++ b/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java @@ -61,10 +61,21 @@ public class SingletonInstance { public SingletonInstance(String lockFileBasename) { this.file = new File ( getCanonicalTempLockFilePath ( lockFileBasename ) ); + setupFileCleanup(); } public SingletonInstance(File lockFile) { this.file = lockFile ; + setupFileCleanup(); + } + + void setupFileCleanup() { + file.deleteOnExit(); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + unlock(); + } + }); } public synchronized void lock(long timeout_ms, long poll_ms) { @@ -92,14 +103,6 @@ public class SingletonInstance { fileLock = randomAccessFile.getChannel().tryLock(); if (fileLock != null) { - //final File f_file = file; - //final RandomAccessFile f_randomAccessFile = randomAccessFile; - //final FileLock f_fileLock = fileLock; - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - unlock(); - } - }); locked = true; if(DEBUG) { System.err.println("Locked " + file); @@ -114,20 +117,29 @@ public class SingletonInstance { } public synchronized boolean unlock() { - if(locked) { - try { - fileLock.release(); - randomAccessFile.close(); - file.delete(); - return true; - } catch (Exception e) { - System.err.println("Unable to remove lock file: " + file); - e.printStackTrace(); - } finally { + try { + if(null != fileLock) { + if(locked) { + fileLock.release(); + } fileLock = null; + } + if(null != randomAccessFile) { + randomAccessFile.close(); randomAccessFile = null; - locked = false; } + if(null != file) { + file.delete(); + file = null; + } + return true; + } catch (Exception e) { + System.err.println("Unable to remove lock file: " + file); + e.printStackTrace(); + } finally { + fileLock = null; + randomAccessFile = null; + locked = false; } return false; } |