-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleInstance.java
228 lines (206 loc) · 6.94 KB
/
SingleInstance.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* This file is part of SingleInstance.
*
* SingleInstance is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SingleInstance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SingleInstance. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2020 Vincent Quatrevieux
*/
package fr.quatrevieux.singleinstance;
import fr.quatrevieux.singleinstance.ipc.InstanceServer;
import fr.quatrevieux.singleinstance.ipc.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
/**
* Facade for a simple usage of {@link InstanceManager}
*
* Usage:
* <pre>{@code
* public static void main(String[] args) {
* // Check if an instance is running
* SingleInstance.onAlreadyRunning(instance -> {
* // Send the argument to the instance and stop the current process
* instance.send("Open", args[0].getBytes());
* System.exit(0);
* });
*
* // Initialize application
* MyApp app = xxx;
*
* // Start the IPC server
* SingleInstance.onMessage(message -> {
* // Receive an "Open" message
* if (message.name().equals("Open")) {
* app.open(new String(message.data()));
* }
* });
* }
* }</pre>
*/
final public class SingleInstance {
static private InstanceManager manager;
static private InstanceServer server;
static private ExecutorService executor;
static private DistantInstance distantInstance;
static private Thread shutdownHook;
final static private Logger LOGGER = LoggerFactory.getLogger(SingleInstance.class);
/**
* Initialize the SingleInstance system
* Calling this method is not required : any other call will implicitly call this method
*
* @throws IOException When error occurs during acquiring the lock
*/
static public void init() throws IOException {
init(null);
}
/**
* Initialize the SingleInstance system by specifying the lock file
*
* @param lockFile The lock file to use
*
* @throws IOException When error occurs during acquiring the lock
*/
static public void init(LockFile lockFile) throws IOException {
if (manager != null) {
return;
}
if (lockFile == null) {
lockFile = new LockFile();
}
manager = new InstanceManager(lockFile);
manager.acquire();
if (shutdownHook == null) {
Runtime.getRuntime().addShutdownHook(shutdownHook = new Thread(SingleInstance::close));
}
}
/**
* Check if the current process is the first running instance
*
* Usage:
* <pre>{@code
* public static void main(String[] args) {
* if (!SingleInstance.isFirst()) {
* System.err.println("Already running");
* return;
* }
* }
* }</pre>
*
* @return true if the current process is the first running instance
*
* @throws IOException When cannot initialize system
* @see SingleInstance#onAlreadyRunning(Consumer) For perform action when an instance is already running
*/
static public boolean isFirst() throws IOException {
return manager().acquire();
}
/**
* Start the IPC server if the current process is the first running instance,
* and handle received messages
*
* If the current process is not the first running instance, this method will be a noop.
*
* Note: This method will start a new thread to consume messages
*
* <pre>{@code
* SingleInstance.onMessage(message -> {
* if (message.name().equals("MyMessage")) {
* // Handle message
* }
* });
* }</pre>
*
* @param consumer The action to perform when receiving messages
*
* @throws IOException When cannot start the server
* @throws IllegalStateException When the server is already started
*/
static public void onMessage(Consumer<Message> consumer) throws IOException {
if (server != null) {
throw new IllegalStateException("IPC Server is already started");
}
manager().server().ifPresent(server -> {
SingleInstance.server = server;
executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
try {
server.consume(consumer);
} catch (IOException e) {
LOGGER.error("Error during reading message on IPC server", e);
}
});
});
}
/**
* Perform action is an already running instance is found
*
* Usage:
* <pre>{@code
* SingleInstance.onAlreadyRunning(instance -> {
* instance.send("MyMessage"); // Send a message to the running instance
* System.exit(0); // Stop the current process
* });
* }</pre>
*
* @param action Action to perform
*
* @throws IOException When cannot initialize the system
*/
static public void onAlreadyRunning(Consumer<DistantInstance> action) throws IOException {
if (distantInstance != null) {
action.accept(distantInstance);
return;
}
manager().find().ifPresent(instance -> {
distantInstance = instance;
action.accept(distantInstance);
});
}
/**
* Get (or create) the instance manager instance
*
* @return The manager instance
* @throws IOException When cannot initialize the system
*/
static public InstanceManager manager() throws IOException {
init();
return manager;
}
/**
* Close the SingleInstance system and release the lock
* This method should not be called manually : when init is called, a shutdown hook is registered
*/
static public void close() {
if (manager == null) {
return;
}
if (server != null) {
try {
server.close();
} catch (IOException e) {
LOGGER.error("Error during closing the IPC server", e);
}
server = null;
}
if (executor != null) {
executor.shutdown();
executor = null;
}
distantInstance = null;
manager.release();
}
}