Skip to content

Commit

Permalink
Merge pull request #31 from gradle/lptr/noexec-tmp
Browse files Browse the repository at this point in the history
Do not initialize library implicitly
  • Loading branch information
lptr authored Jan 30, 2025
2 parents 8d1d672 + c53ee52 commit 8fd406a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ group = "org.gradle.fileevents"

dependencies {
compileOnly("com.google.code.findbugs:jsr305:3.0.2")
api("net.rubygrapefruit:native-platform:0.22-milestone-26")
api("net.rubygrapefruit:native-platform:0.22-milestone-28")
implementation("org.slf4j:slf4j-api:1.7.36")
testImplementation(platform("org.junit:junit-bom:5.11.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/org/gradle/fileevents/FileEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
*/
@ThreadSafe
public class FileEvents {
private static final Map<Class<?>, Object> integrations = new HashMap<>();
private static boolean initialized;
private static FileEvents instance;

private final Map<Class<?>, Object> integrations = new HashMap<>();

private FileEvents() {
}

/**
* Initializes the native file events integration.
Expand All @@ -35,10 +39,10 @@ public class FileEvents {
* @throws NativeException On failure to initialize the native integration.
*/
@ThreadSafe
static public void init(File extractDir) throws NativeException {
static public FileEvents init(File extractDir) throws NativeException {
synchronized (FileEvents.class) {
if (initialized) {
return;
if (instance != null) {
throw new NativeException("File-system watching native library has already been initialized.");
}
Platform platform = Platform.current();
String platformName = getPlatformName(platform);
Expand All @@ -49,7 +53,6 @@ static public void init(File extractDir) throws NativeException {
throw new NativeIntegrationUnavailableException(String.format("Native file events integration is not available for %s.", platform));
}
System.load(library.getCanonicalPath());
initialized = true;

String nativeVersion = AbstractNativeFileEventFunctions.getVersion();
if (!nativeVersion.equals(FileEventsVersion.VERSION)) {
Expand All @@ -59,6 +62,9 @@ static public void init(File extractDir) throws NativeException {
nativeVersion
));
}

instance = new FileEvents();
return instance;
} catch (NativeException e) {
throw e;
} catch (Throwable t) {
Expand Down Expand Up @@ -132,12 +138,9 @@ private static boolean isLinuxWithMusl() {
* @throws NativeException On failure to load the native integration.
*/
@ThreadSafe
public static <T extends NativeIntegration> T get(Class<T> type)
public <T extends NativeIntegration> T get(Class<T> type)
throws NativeIntegrationUnavailableException, NativeException {
synchronized (FileEvents.class) {
if (!initialized) {
throw new NativeException(String.format("File-system watching native library has not been initialized.", type.getSimpleName()));
}
synchronized (this) {
Platform platform = Platform.current();
Object instance = integrations.get(type);
if (instance == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import spock.lang.Specification
import spock.lang.TempDir
import spock.lang.Timeout

import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit
Expand All @@ -51,6 +53,18 @@ abstract class AbstractFileEventFunctionsTest extends Specification {

LoggingCapture logging = new LoggingCapture(NativeLogger, Level.INFO)

private static FileEvents fileEventsIntegration

protected static FileEvents getFileEvents() {
if (fileEventsIntegration == null) {
def testOutputDir = Paths.get("build/test-outputs")
Files.createDirectories(testOutputDir)
def tempDir = Files.createTempDirectory(testOutputDir, "file-events")
fileEventsIntegration = FileEvents.init(tempDir.toFile())
}
return fileEventsIntegration
}

@TempDir
File tmpDir
String testName
Expand Down Expand Up @@ -84,8 +98,6 @@ abstract class AbstractFileEventFunctionsTest extends Specification {
assert rootDir.mkdirs()
uncaughtFailureOnThread = []
expectedLogMessages = [:]

FileEvents.init(null)
}

def cleanup() {
Expand Down Expand Up @@ -152,7 +164,7 @@ abstract class AbstractFileEventFunctionsTest extends Specification {
@Memoized
@Override
OsxFileEventFunctions getService() {
FileEvents.get(OsxFileEventFunctions)
AbstractFileEventFunctionsTest.getFileEvents().get(OsxFileEventFunctions)
}

@Override
Expand All @@ -173,7 +185,7 @@ abstract class AbstractFileEventFunctionsTest extends Specification {
@Memoized
@Override
LinuxFileEventFunctions getService() {
FileEvents.get(LinuxFileEventFunctions)
AbstractFileEventFunctionsTest.getFileEvents().get(LinuxFileEventFunctions)
}

@Override
Expand All @@ -193,7 +205,7 @@ abstract class AbstractFileEventFunctionsTest extends Specification {
@Memoized
@Override
WindowsFileEventFunctions getService() {
FileEvents.get(WindowsFileEventFunctions)
AbstractFileEventFunctionsTest.getFileEvents().get(WindowsFileEventFunctions)
}

@Override
Expand Down

0 comments on commit 8fd406a

Please sign in to comment.