
io.tracee.contextlogger.utility.GenericServiceLocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of contextlogger-core Show documentation
Show all versions of contextlogger-core Show documentation
Please refer to https://github.com/tracee/contextlogger.
package io.tracee.contextlogger.utility;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
/**
* A generic service locator.
*/
public class GenericServiceLocator {
private GenericServiceLocator() {
}
public static T locate(final Class clazz) {
final List services = locateAll(clazz);
return services.isEmpty() ? (T)null : (T)services.get(0);
}
public static List locateAll(final Class clazz) {
final Iterator iterator = ServiceLoader.load(clazz).iterator();
final List services = new ArrayList();
while (iterator.hasNext()) {
try {
services.add(iterator.next());
}
catch (Error e) {
e.printStackTrace(System.err);
}
}
return services;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy