Skip to content

Commit

Permalink
Merge pull request #29 from gradle/lptr/require-explicit-init
Browse files Browse the repository at this point in the history
Require explicit call to FileEvents.init()
  • Loading branch information
lptr authored Jan 14, 2025
2 parents 0450ef4 + 12d47e9 commit 54a3393
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/org/gradle/fileevents/FileEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
@ThreadSafe
public class FileEvents {
private static final Map<Class<?>, Object> integrations = new HashMap<>();
private static boolean initialized;

@ThreadSafe
static public void init(File extractDir) throws NativeException {
synchronized (FileEvents.class) {
if (initialized) {
return;
}
Platform platform = Platform.current();
String platformName = getPlatformName(platform);
try {
Expand All @@ -33,6 +37,7 @@ 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 Down Expand Up @@ -115,8 +120,10 @@ private static boolean isLinuxWithMusl() {
@ThreadSafe
public static <T extends NativeIntegration> T get(Class<T> type)
throws NativeIntegrationUnavailableException, NativeException {
init(null);
synchronized (FileEvents.class) {
if (!initialized) {
throw new NativeException(String.format("File-system watching native library has not been initialized.", type.getSimpleName()));
}
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 @@ -84,6 +84,8 @@ abstract class AbstractFileEventFunctionsTest extends Specification {
assert rootDir.mkdirs()
uncaughtFailureOnThread = []
expectedLogMessages = [:]

FileEvents.init(null)
}

def cleanup() {
Expand Down

0 comments on commit 54a3393

Please sign in to comment.