11package org .cryptomator .integrations .common ;
22
3+ import org .jetbrains .annotations .NotNull ;
34import org .jetbrains .annotations .Nullable ;
45import org .jetbrains .annotations .VisibleForTesting ;
56import org .slf4j .Logger ;
910import java .lang .reflect .Modifier ;
1011import java .util .Arrays ;
1112import java .util .Comparator ;
13+ import java .util .Objects ;
1214import java .util .Optional ;
1315import java .util .ServiceConfigurationError ;
1416import 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