aboutsummaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2011-04-28 11:22:27 +0200
committerSven Gothel <[email protected]>2011-04-28 11:22:27 +0200
commit09a63b963c2bd4212a072c373b36511478721daa (patch)
tree56d038c145bae67bdde60128a0db4276f8bef9fa /src/test
parenta34adb62a78c033a5caf27f3660b18bfc3e59edd (diff)
Unit Test Framework SingletonInstance: setup file unlock/delete asap
Diffstat (limited to 'src/test')
-rw-r--r--src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java50
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;
}