use of java.util.ServiceLoader in project aries by apache.
the class Util method serviceLoaderLoad.
public static <C, S> ServiceLoader<S> serviceLoaderLoad(Class<S> service, Class<C> caller) {
if (BaseActivator.activator == null) {
// The system is not yet initialized. We can't do anything.
return null;
}
ClassLoader bundleLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return caller.getClassLoader();
}
});
if (!(bundleLoader instanceof BundleReference)) {
BaseActivator.activator.log(Level.FINE, "Classloader of consuming bundle doesn't implement BundleReference: " + bundleLoader);
return ServiceLoader.load(service);
}
BundleReference bundleReference = (BundleReference) bundleLoader;
final ClassLoader bundleClassloader = findContextClassloader(bundleReference.getBundle(), ServiceLoader.class.getName(), "load", service);
if (bundleClassloader == null) {
return ServiceLoader.load(service);
}
Thread thread = Thread.currentThread();
return AccessController.doPrivileged(new PrivilegedAction<ServiceLoader<S>>() {
@Override
public ServiceLoader<S> run() {
ClassLoader contextClassLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(bundleClassloader);
return ServiceLoader.load(service);
} finally {
thread.setContextClassLoader(contextClassLoader);
}
}
});
}
use of java.util.ServiceLoader in project aries by apache.
the class Util method serviceLoaderLoad.
public static <C, S> ServiceLoader<S> serviceLoaderLoad(Class<S> service, ClassLoader specifiedClassLoader, Class<C> caller) {
if (BaseActivator.activator == null) {
// The system is not yet initialized. We can't do anything.
return null;
}
ClassLoader bundleLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return caller.getClassLoader();
}
});
if (!(bundleLoader instanceof BundleReference)) {
BaseActivator.activator.log(Level.FINE, "Classloader of consuming bundle doesn't implement BundleReference: " + bundleLoader);
return ServiceLoader.load(service, specifiedClassLoader);
}
BundleReference bundleReference = (BundleReference) bundleLoader;
final ClassLoader bundleClassloader = findContextClassloader(bundleReference.getBundle(), ServiceLoader.class.getName(), "load", service);
if (bundleClassloader == null) {
return ServiceLoader.load(service, specifiedClassLoader);
}
return ServiceLoader.load(service, new WrapperCL(specifiedClassLoader, bundleClassloader));
}
use of java.util.ServiceLoader in project Java-9-Spring-Webflux by kkTranslation.
the class Gui method main.
public static void main(String[] args) {
ServiceLoader<Analysis> analyses = ServiceLoader.load(Analysis.class);
// analyses.stream().map(ServiceLoader.Provider::get).forEach(analyzer -> System.out.println(analyzer.getName()));
Set<Analysis> collect = analyses.stream().filter(p -> isAnno(p.type())).map(ServiceLoader.Provider::get).collect(Collectors.toSet());
Person nao = new Person(0, 0);
Person miniperson = new Person(10, 0);
Person minperson = new Person(1001, 0);
Person minxperson = new Person(1001, 11);
Person maxperson = new Person(100000, 11);
Person maxxperson = new Person(100000, 26);
Person poorperson = new Person(100000, 0);
collect.forEach(p -> System.out.println(p.analyze(maxxperson)));
}
use of java.util.ServiceLoader in project knox by apache.
the class Log4jMessageLoggerFactoryTest method testServiceLoader.
@Test
public void testServiceLoader() throws Exception {
ServiceLoader loader = ServiceLoader.load(MessageLoggerFactory.class);
Iterator iterator = loader.iterator();
assertThat("Service iterator empty.", iterator.hasNext());
while (iterator.hasNext()) {
Object object = iterator.next();
if (object instanceof Log4jMessageLoggerFactory) {
return;
}
}
fail("Failed to find " + Log4jMessageLoggerFactory.class.getName() + " via service loader.");
}
use of java.util.ServiceLoader in project knox by apache.
the class Sl4jMessageLoggerFactoryTest method testServiceLoader.
@Test
public void testServiceLoader() throws Exception {
ServiceLoader loader = ServiceLoader.load(MessageLoggerFactory.class);
Iterator iterator = loader.iterator();
assertThat("Service iterator empty.", iterator.hasNext());
while (iterator.hasNext()) {
Object object = iterator.next();
if (object instanceof Sl4jMessageLoggerFactory) {
return;
}
}
fail("Failed to find " + Sl4jMessageLoggerFactory.class.getName() + " via service loader.");
}
Aggregations