package io.vertx.core;
import java.util.*;
public class ServiceHelper {
public static <T> T loadFactory(Class<T> clazz) {
T factory = loadFactoryOrNull(clazz);
if (factory == null) {
throw new IllegalStateException("Cannot find META-INF/services/" + clazz.getName() + " on classpath");
}
return factory;
}
public static <T> T loadFactoryOrNull(Class<T> clazz) {
Collection<T> collection = loadFactories(clazz);
if (!collection.isEmpty()) {
return collection.iterator().next();
} else {
return null;
}
}
public static <T> Collection<T> loadFactories(Class<T> clazz) {
return loadFactories(clazz, null);
}
public static <T> Collection<T> loadFactories(Class<T> clazz, ClassLoader classLoader) {
List<T> list = new ArrayList<>();
ServiceLoader<T> factories;
if (classLoader != null) {
factories = ServiceLoader.load(clazz, classLoader);
} else {
factories = ServiceLoader.load(clazz);
}
if (factories.iterator().hasNext()) {
factories.iterator().forEachRemaining(list::add);
return list;
} else {
factories = ServiceLoader.load(clazz, ServiceHelper.class.getClassLoader());
if (factories.iterator().hasNext()) {
factories.iterator().forEachRemaining(list::add);
return list;
} else {
return Collections.emptyList();
}
}
}
}