Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not initialize library implicitly #31

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading