diff options
author | Sven Gothel <[email protected]> | 2011-04-28 11:22:27 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-28 11:22:27 +0200 |
commit | 09a63b963c2bd4212a072c373b36511478721daa (patch) | |
tree | 56d038c145bae67bdde60128a0db4276f8bef9fa /src/test/com/jogamp/opengl | |
parent | a34adb62a78c033a5caf27f3660b18bfc3e59edd (diff) |
Unit Test Framework SingletonInstance: setup file unlock/delete asap
Diffstat (limited to 'src/test/com/jogamp/opengl')
-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 7e3b9ebc0..90acd7b74 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; } |