Merge pull request #352 from yigitpirildak/separate_tmpdir_across_applications

Separate tmpdir across applications
This commit is contained in:
Will Hedgecock 2021-10-22 12:18:57 -05:00 committed by GitHub
commit 956364b449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -7,6 +7,13 @@ _A platform-independent serial port access library for Java._
For usage examples, please refer to the [Usage wiki](https://github.com/Fazecast/jSerialComm/wiki/Usage-Examples).
If you intend to use the library in multiple applications simultaneously, please make sure
to set ```fazecast.jSerialComm.appid``` property before accessing the SerialPort class so that
applications don't accidentally delete each others temporary files during boot-up:
```
System.setProperty("fazecast.jSerialComm.appid", "YOUR_APPLICATION_IDENTIFIER")
```
In order to use the ```jSerialComm``` library in your own project, you must simply
include the JAR file in your build path and import it like any other
Java package using ```import com.fazecast.jSerialComm.*;```.

View File

@ -51,6 +51,9 @@ public final class SerialPort
{
// Static initializer loads correct native library for this machine
private static final String versionString = "2.7.0";
// Used to distinguish multiple processes running this lib
private static final String TMPDIR_APPID_PROPERTY = "fazecast.jSerialComm.appid";
private static volatile boolean isAndroid = false;
private static volatile boolean isUnixBased = false;
private static volatile boolean isWindows = false;
@ -62,7 +65,13 @@ public final class SerialPort
String tempFileDirectory = System.getProperty("java.io.tmpdir");
if ((tempFileDirectory.charAt(tempFileDirectory.length()-1) != '\\') && (tempFileDirectory.charAt(tempFileDirectory.length()-1) != '/'))
tempFileDirectory += "/";
// Make sure to use appId to separate tmpdir directories if library is used by multiple modules, so they don't erase each others folders accidentally.
String appId = System.getProperty(TMPDIR_APPID_PROPERTY, "");
tempFileDirectory += "jSerialComm/";
if (!appId.isEmpty()) {
tempFileDirectory += appId + (!appId.endsWith("/") ? "/" : "");
}
deleteDirectory(new File(tempFileDirectory));
// Determine Operating System and architecture