Search in sources :

Example 41 with ServiceLoader

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);
            }
        }
    });
}
Also used : ServiceLoader(java.util.ServiceLoader) BundleReference(org.osgi.framework.BundleReference)

Example 42 with ServiceLoader

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));
}
Also used : ServiceLoader(java.util.ServiceLoader) BundleReference(org.osgi.framework.BundleReference)

Example 43 with ServiceLoader

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)));
}
Also used : ServiceLoader(java.util.ServiceLoader) Analysis(com.dockerx.traffic.providespro.service.Analysis) Person(com.dockerx.traffic.providespro.entity.Person)

Example 44 with ServiceLoader

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.");
}
Also used : ServiceLoader(java.util.ServiceLoader) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 45 with ServiceLoader

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.");
}
Also used : ServiceLoader(java.util.ServiceLoader) Iterator(java.util.Iterator) Test(org.junit.Test)

Aggregations

ServiceLoader (java.util.ServiceLoader)68 Iterator (java.util.Iterator)46 Test (org.junit.Test)46 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)3 Optional (java.util.Optional)3 ServiceConfigurationError (java.util.ServiceConfigurationError)3 ServletContext (javax.servlet.ServletContext)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CharsetProvider (java.nio.charset.spi.CharsetProvider)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2