Skip to content

Commit 6b9d7fe

Browse files
committed
initial commit
0 parents  commit 6b9d7fe

File tree

11 files changed

+601
-0
lines changed

11 files changed

+601
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
/.idea/
9+
*.iws
10+
*.iml
11+
*.ipr
12+
out/
13+
!**/src/main/**/out/
14+
!**/src/test/**/out/
15+
16+
### Eclipse ###
17+
.apt_generated
18+
.classpath
19+
.factorypath
20+
.project
21+
.settings
22+
.springBeans
23+
.sts4-cache
24+
bin/
25+
!**/src/main/**/bin/
26+
!**/src/test/**/bin/
27+
28+
### NetBeans ###
29+
/nbproject/private/
30+
/nbbuild/
31+
/dist/
32+
/nbdist/
33+
/.nb-gradle/
34+
35+
### VS Code ###
36+
.vscode/
37+
38+
### Mac OS ###
39+
.DS_Store

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 iamcalledrob
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# singleinstance
2+
3+
## Introduction
4+
Lightweight Kotlin/JVM library that prevents multiple concurrent instances of a Kotlin application.
5+
Launch arguments from subsequent instances are communicated back to the first instance via a UNIX domain socket.
6+
7+
A Kotlin equivalent to [unique4j](https://github.com/prat-man/unique4j)
8+
9+
## Installation
10+
Add the [jitpack](https://jitpack.io/) repository to your build file:
11+
```kotlin
12+
repositories {
13+
maven { url 'https://jitpack.io' }
14+
}
15+
```
16+
17+
Then add as a module dependency:
18+
```kotlin
19+
dependencies {
20+
implementation("com.github.iamcalledrob:singleinstance:1.0.0")
21+
}
22+
```
23+
24+
## Usage
25+
Instantiate `SingleInstance` with a file path and call `args()`.
26+
1. If this is the first instance, `args` will listen indefinitely for subsequent instances to connect to it.
27+
2. If there is an existing instance, `args` will connect and send its arguments, then exit.
28+
29+
```kotlin
30+
fun main(args: Array<String>) {
31+
SingleInstance("/path/to/a/unique/file.sock").args(args) { receivedArgs ->
32+
// Handle received args from another instance
33+
println("Subsequent instance args: ${receivedArgs.joinToString()}")
34+
}
35+
36+
// Handle original args
37+
println("Original launch args: ${args.joinToString()}")
38+
39+
// The rest of the application logic
40+
}
41+
```
42+
43+
There is also a helper function for generating a suitable sock file path from an identifier:
44+
```kotlin
45+
socketPath(identifier = "org.foo.widget")
46+
// -> /tmp/org.foo.widget.sock
47+
```
48+
49+
## How it works
50+
1. A system-wide file lock is used to determine which instance is "first" to launch
51+
2. The first instance then listens on a domain socket for arguments from other instances
52+
3. Other instances are unable to acquire the file lock, and instead connect to the first instance via the socket.
53+
Arguments are written to the socket, then the process exits.
54+
4. When the original process exits, the file lock is released.
55+
56+
## Why you might consider this over [unique4j](https://github.com/prat-man/unique4j)
57+
1. Uses [UNIX Domain Sockets](https://inside.java/2021/02/03/jep380-unix-domain-sockets-channels/), which avoids
58+
triggering the Windows firewall. unique4j listens on a local TCP socket, which will pop a firewall dialog.
59+
2. Minimal design, leaves the exact implementation up to you.
60+
3. Filesystem permissions can be used to control which processes are able to send arguments, if desired.
61+
62+
## Considerations
63+
Domain sockets were added to Windows version 1809, so this library is unsuitable for legacy versions of Windows.

build.gradle.kts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
kotlin("jvm") version "1.9.23"
3+
id("maven-publish")
4+
}
5+
6+
group = "com.github.iamcalledrob"
7+
version = "1.0.0"
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
java {
14+
withSourcesJar()
15+
}
16+
17+
kotlin {
18+
jvmToolchain(17)
19+
20+
dependencies {
21+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
22+
}
23+
}
24+
25+
publishing {
26+
publications {
27+
create<MavenPublication>("maven") {
28+
from(components["java"])
29+
}
30+
}
31+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kotlin.code.style=official

gradle/wrapper/gradle-wrapper.jar

59.3 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Tue Aug 06 11:36:36 BST 2024
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)