-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminClientMain.java
More file actions
47 lines (44 loc) · 1.67 KB
/
AdminClientMain.java
File metadata and controls
47 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.StandardOpenOption;
public class AdminClientMain {
private static final String LOCK_FILE_PATH = "admin.lock";
private static FileChannel fileChannel;
private static FileLock lock;
public static void main(String[] args) {
try {
// Try to acquire the lock
File lockFile = new File(LOCK_FILE_PATH);
fileChannel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
lock = fileChannel.tryLock();
if (lock != null) {
// Proceed with the application logic
AdminMenus menu = new AdminMenus();
boolean adminActive = true;
while (adminActive) {
adminActive = menu.displayMainMenu();
}
} else {
System.out.println("Admin client instance already exists! Goodbye!");
}
} catch (IOException e) {
System.out.println("Failed to acquire lock: " + e.getMessage());
} finally {
// Release the lock and close the file channel on exit
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
if (lock != null) {
lock.release();
}
if (fileChannel != null) {
fileChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}));
}
}
}