1
|
|
|
/* |
2
|
|
|
* This is free software; you can redistribute it and/or modify it |
3
|
|
|
* under the terms of the GNU Lesser General Public License as |
4
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
5
|
|
|
* the License, or (at your option) any later version. |
6
|
|
|
* |
7
|
|
|
* This software is distributed in the hope that it will be useful, |
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
10
|
|
|
* Lesser General Public License for more details. |
11
|
|
|
*/ |
12
|
|
|
package com.strider.datadefender.utils; |
13
|
|
|
|
14
|
|
|
import java.io.File; |
15
|
|
|
import java.io.FileNotFoundException; |
16
|
|
|
import java.io.IOException; |
17
|
|
|
import java.io.RandomAccessFile; |
18
|
|
|
|
19
|
|
|
import java.nio.channels.FileChannel; |
20
|
|
|
import java.nio.channels.FileLock; |
21
|
|
|
import java.nio.channels.OverlappingFileLockException; |
22
|
|
|
|
23
|
|
|
import com.strider.datadefender.DataDefenderException; |
24
|
|
|
import lombok.extern.log4j.Log4j2; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Most of the code is copied from this page: http://www.rgagnon.com/javadetails/java-0288.html |
28
|
|
|
* |
29
|
|
|
* Modified by Armenak Grigoryan |
30
|
|
|
*/ |
31
|
|
|
@Log4j2 |
32
|
|
|
public class ApplicationLock { |
33
|
|
|
|
34
|
|
|
private final String appName; |
35
|
|
|
private File file; |
36
|
|
|
private FileChannel channel; |
37
|
|
|
private FileLock lock; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
* |
42
|
|
|
* @param appName application name |
43
|
|
|
*/ |
44
|
|
|
public ApplicationLock(final String appName) { |
45
|
|
|
this.appName = appName; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private void closeLock() throws DataDefenderException { |
49
|
|
|
try { |
50
|
|
|
lock.release(); |
51
|
|
|
} catch (IOException e) { |
52
|
|
|
throw new DataDefenderException("Problem releasing file lock", e); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
channel.close(); |
57
|
|
|
} catch (IOException e) { |
58
|
|
|
throw new DataDefenderException("Problem closing channel", e); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private void deleteFile() { |
63
|
|
|
file.delete(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns true if there is another instance of the application is running. |
68
|
|
|
* Otherwise returns false. |
69
|
|
|
* |
70
|
|
|
* @return boolean |
71
|
|
|
* @throws com.strider.datadefender.DataDefenderException |
72
|
|
|
*/ |
73
|
|
|
public boolean isAppActive() throws DataDefenderException { |
74
|
|
|
try { |
75
|
|
|
file = new File(System.getProperty("user.home"), appName + ".tmp"); |
76
|
|
|
channel = new RandomAccessFile(file, "rw").getChannel(); |
77
|
|
|
log.debug("Creating lock file " + file.getName()); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
lock = channel.tryLock(); |
81
|
|
|
log.debug("Locking file ..."); |
82
|
|
|
} catch (OverlappingFileLockException | IOException e) { |
83
|
|
|
|
84
|
|
|
// already locked |
85
|
|
|
log.error("File " + file.getName() + " already locket"); |
86
|
|
|
closeLock(); |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (lock == null) { |
92
|
|
|
closeLock(); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() { |
98
|
|
|
|
99
|
|
|
// destroy the lock when the JVM is closing |
100
|
|
|
@Override |
101
|
|
|
public void run() { |
102
|
|
|
try { |
103
|
|
|
log.debug("Closing lock file"); |
104
|
|
|
closeLock(); |
105
|
|
|
deleteFile(); |
106
|
|
|
} catch (DataDefenderException ae) { |
107
|
|
|
log.error("Problem closing file lock"); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} catch (FileNotFoundException fnfe) { |
114
|
|
|
closeLock(); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
//~ Formatted by Jindent --- http://www.jindent.com |
123
|
|
|
|