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

Feature: Robust service provider loading #27

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.stream.Stream;

Expand Down Expand Up @@ -47,7 +49,8 @@ public static <T> Stream<T> loadAll(Class<T> clazz) {
.filter(IntegrationsLoader::isSupportedOperatingSystem)
.filter(IntegrationsLoader::passesStaticAvailabilityCheck)
.sorted(Comparator.comparingInt(IntegrationsLoader::getPriority).reversed())
.map(ServiceLoader.Provider::get)
.map(IntegrationsLoader::instantiateServiceProvider)
.filter(Objects::nonNull)
infeo marked this conversation as resolved.
Show resolved Hide resolved
.filter(IntegrationsLoader::passesInstanceAvailabilityCheck)
.peek(impl -> logServiceIsAvailable(clazz, impl.getClass()));
}
Expand All @@ -68,18 +71,37 @@ private static boolean isSupportedOperatingSystem(ServiceLoader.Provider<?> prov
return annotations.length == 0 || Arrays.stream(annotations).anyMatch(OperatingSystem.Value::isCurrent);
}

private static <T> T instantiateServiceProvider(ServiceLoader.Provider<T> provider) {
try {
return provider.get();
} catch (ServiceConfigurationError err) {
infeo marked this conversation as resolved.
Show resolved Hide resolved
LOG.warn("Unable to load service provider {}.", provider.type().getName(), err);
return null;
}
}
infeo marked this conversation as resolved.
Show resolved Hide resolved

private static boolean passesStaticAvailabilityCheck(ServiceLoader.Provider<?> provider) {
return passesStaticAvailabilityCheck(provider.type());
}

@VisibleForTesting
static boolean passesStaticAvailabilityCheck(Class<?> type) {
return passesAvailabilityCheck(type, null);
try {
return passesAvailabilityCheck(type, null);
} catch (ExceptionInInitializerError | NoClassDefFoundError | RuntimeException t) {
LOG.warn("Unable to load service provider {}.", type.getName(), t);
return false;
}
}

@VisibleForTesting
static boolean passesInstanceAvailabilityCheck(Object instance) {
return passesAvailabilityCheck(instance.getClass(), instance);
try {
return passesAvailabilityCheck(instance.getClass(), instance);
} catch (RuntimeException rte) {
LOG.warn("Unable to load service provider {}.", instance.getClass().getName(), rte);
return false;
}
}
overheadhunter marked this conversation as resolved.
Show resolved Hide resolved

private static void logServiceIsAvailable(Class<?> apiType, Class<?> implType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.cryptomator.integrations.common;

@CheckAvailability
public class InitExceptionTestClass {

private static final String TEST;

static {
TEST = throwSomethig();
}

public InitExceptionTestClass() {

}

static String throwSomethig() {
infeo marked this conversation as resolved.
Show resolved Hide resolved
throw new RuntimeException("STATIC FAIL");
}

@CheckAvailability
public static boolean test() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ class C2 extends StaticFalse {
Assertions.assertFalse(IntegrationsLoader.passesStaticAvailabilityCheck(C3.class));
}

@Test
@DisplayName("throwing @CheckAvailability methods are treated as false")
public void testPassesAvailabilityCheckThrowing() {

@CheckAvailability class C1 {
@CheckAvailability public static boolean test() { throw new RuntimeException("FAIL"); }
}

Assertions.assertFalse(IntegrationsLoader.passesStaticAvailabilityCheck(C1.class));
Assertions.assertFalse(IntegrationsLoader.passesStaticAvailabilityCheck(InitExceptionTestClass.class));
Assertions.assertFalse(IntegrationsLoader.passesStaticAvailabilityCheck(InitExceptionTestClass.class)); //NoClassDefFoundError due to repated call
}
infeo marked this conversation as resolved.
Show resolved Hide resolved

}

Expand Down Expand Up @@ -190,6 +202,26 @@ class C2 extends InstanceFalse {
Assertions.assertFalse(IntegrationsLoader.passesInstanceAvailabilityCheck(new C3()));
}


@Test
@DisplayName("throwing @CheckAvailability methods are treated as false")
public void testPassesAvailabilityCheckThrowing() {

@CheckAvailability
class C1 {
@CheckAvailability public boolean test1() { throw new RuntimeException("FAIL"); }
}

@CheckAvailability
class C2 {
@CheckAvailability public boolean test1() { return true; }
@CheckAvailability public boolean test2() { throw new RuntimeException("FAIL"); }
}

Assertions.assertFalse(IntegrationsLoader.passesInstanceAvailabilityCheck(new C1()));
Assertions.assertFalse(IntegrationsLoader.passesInstanceAvailabilityCheck(new C2()));
}

}

}