Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SingletonInstanceFileLock.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util.parallel.locks.impl;
28
29import java.io.File;
30import java.io.IOException;
31import java.io.RandomAccessFile;
32import java.nio.channels.FileLock;
33
34import org.jau.lang.InterruptSource;
35
36import jau.test.util.parallel.locks.SingletonInstance;
37
39
40 static final String temp_file_path;
41
42 static {
43 String s = null;
44 try {
45 final File tmpFile = File.createTempFile("TEST", "tst");
46 final String absTmpFile = tmpFile.getCanonicalPath();
47 tmpFile.delete();
48 s = absTmpFile.substring(0, absTmpFile.lastIndexOf(File.separator));
49 } catch (final IOException ex) {
50 ex.printStackTrace();
51 }
52 temp_file_path = s;
53 }
54
55 public static String getCanonicalTempPath() {
56 return temp_file_path;
57 }
58
59 public static String getCanonicalTempLockFilePath(final String basename) {
60 return getCanonicalTempPath() + File.separator + basename;
61 }
62
63 public SingletonInstanceFileLock(final long poll_ms, final String lockFileBasename) {
64 super(poll_ms);
65 file = new File ( getCanonicalTempLockFilePath ( lockFileBasename ) );
66 setupFileCleanup();
67 }
68
69 public SingletonInstanceFileLock(final long poll_ms, final File lockFile) {
70 super(poll_ms);
71 file = lockFile ;
72 setupFileCleanup();
73 }
74
75 @Override
76 public final String getName() { return file.getPath(); }
77
78 private void setupFileCleanup() {
79 file.deleteOnExit();
80 Runtime.getRuntime().addShutdownHook(new InterruptSource.Thread() {
81 @Override
82 public void run() {
83 if(isLocked()) {
84 System.err.println(infoPrefix()+" XXX "+SingletonInstanceFileLock.this.getName()+" - Unlock @ JVM Shutdown");
85 }
86 unlock();
87 }
88 });
89 }
90
91 @Override
92 protected boolean tryLockImpl() {
93 try {
94 randomAccessFile = new RandomAccessFile(file, "rw");
95 fileLock = randomAccessFile.getChannel().tryLock();
96
97 if (fileLock != null) {
98 return true;
99 }
100 } catch (final Exception e) {
101 System.err.println(infoPrefix()+" III "+getName()+" - Unable to create and/or lock file");
102 e.printStackTrace();
103 }
104 return false;
105 }
106
107 @Override
108 protected boolean unlockImpl() {
109 try {
110 if(null != fileLock) {
111 fileLock.release();
112 fileLock = null;
113 }
114 if(null != randomAccessFile) {
115 randomAccessFile.close();
116 randomAccessFile = null;
117 }
118 if(null != file) {
119 file.delete();
120 }
121 return true;
122 } catch (final Exception e) {
123 System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock file");
124 e.printStackTrace();
125 } finally {
126 fileLock = null;
127 randomAccessFile = null;
128 }
129 return false;
130 }
131
132 private final File file;
133 private RandomAccessFile randomAccessFile = null;
134 private FileLock fileLock = null;
135}
SingletonInstanceFileLock(final long poll_ms, final String lockFileBasename)