From f47c44c41ca57d785b4c8026cd087a0810c7fbc8 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 28 Apr 2011 11:22:27 +0200 Subject: Unit Test Framework SingletonInstance: setup file unlock/delete asap --- .../opengl/test/junit/util/SingletonInstance.java | 50 ++++++++++++++-------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'src/test/com') 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; } -- cgit v1.2.3