Skip to content

Commit ce2e513

Browse files
committed
Extend API to allow loading arbitrary services with IntegrationsLoader
1 parent 5b9dbae commit ce2e513

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cryptomator.integrations.common;
22

3+
import org.jetbrains.annotations.NotNull;
34
import org.jetbrains.annotations.Nullable;
45
import org.jetbrains.annotations.VisibleForTesting;
56
import org.slf4j.Logger;
@@ -9,6 +10,7 @@
910
import java.lang.reflect.Modifier;
1011
import java.util.Arrays;
1112
import java.util.Comparator;
13+
import java.util.Objects;
1214
import java.util.Optional;
1315
import java.util.ServiceConfigurationError;
1416
import java.util.ServiceLoader;
@@ -34,23 +36,41 @@ public static <T> Optional<T> load(Class<T> clazz) {
3436
return loadAll(clazz).findFirst();
3537
}
3638

39+
3740
/**
3841
* Loads all suited service providers ordered by priority in descending order.
42+
* </p>
43+
* Only services declared in the `org.cryptomator.integrations.api` module can be loaded with this method.
44+
* Foreign services need to use {@link IntegrationsLoader#loadAll(ServiceLoader, Class)}.
3945
*
4046
* @param clazz Service class
4147
* @param <T> Type of the service
4248
* @return An ordered stream of all suited service providers
4349
*/
4450
public static <T> Stream<T> loadAll(Class<T> clazz) {
45-
return ServiceLoader.load(clazz, ClassLoaderFactory.forPluginDir())
46-
.stream()
51+
return loadAll(ServiceLoader.load(clazz, ClassLoaderFactory.forPluginDir()), clazz);
52+
}
53+
54+
/**
55+
* Loads all suited service providers ordered by priority in descending order.
56+
* <p>
57+
* This method allows arbitrary services to be loaded, as long as the provided service loader has module access to them.
58+
*
59+
* @param serviceLoader Loader with own module scope
60+
* @param clazz Service class
61+
* @param <T> Type of the service
62+
* @return An ordered stream of all suited service providers
63+
*/
64+
public static <T> Stream<T> loadAll(ServiceLoader<T> serviceLoader, @NotNull Class<T> clazz) {
65+
Objects.requireNonNull(clazz, "Service to load not specified.");
66+
return serviceLoader.stream()
4767
.peek(serviceProvider -> logFoundServiceProvider(clazz, serviceProvider.type()))
4868
.filter(IntegrationsLoader::isSupportedOperatingSystem)
4969
.filter(IntegrationsLoader::passesStaticAvailabilityCheck)
5070
.sorted(Comparator.comparingInt(IntegrationsLoader::getPriority).reversed())
5171
.flatMap(IntegrationsLoader::instantiateServiceProvider)
5272
.filter(IntegrationsLoader::passesInstanceAvailabilityCheck)
53-
.peek(impl -> logServiceIsAvailable(clazz, impl.getClass()));
73+
.peek(impl -> logServiceIsAvailable(clazz, impl.getClass())); //TODO
5474
}
5575

5676
private static void logFoundServiceProvider(Class<?> apiType, Class<?> implType) {

0 commit comments

Comments
 (0)