Search in sources :

Example 26 with ServiceLoader

use of java.util.ServiceLoader in project jdk8u_jdk by JetBrains.

the class Charset method providers.

// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown.  Should be invoked with full privileges.
//
private static Iterator<CharsetProvider> providers() {
    return new Iterator<CharsetProvider>() {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        ServiceLoader<CharsetProvider> sl = ServiceLoader.load(CharsetProvider.class, cl);

        Iterator<CharsetProvider> i = sl.iterator();

        CharsetProvider next = null;

        private boolean getNext() {
            while (next == null) {
                try {
                    if (!i.hasNext())
                        return false;
                    next = i.next();
                } catch (ServiceConfigurationError sce) {
                    if (sce.getCause() instanceof SecurityException) {
                        // Ignore security exceptions
                        continue;
                    }
                    throw sce;
                }
            }
            return true;
        }

        public boolean hasNext() {
            return getNext();
        }

        public CharsetProvider next() {
            if (!getNext())
                throw new NoSuchElementException();
            CharsetProvider n = next;
            next = null;
            return n;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ServiceLoader(java.util.ServiceLoader) CharsetProvider(java.nio.charset.spi.CharsetProvider) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) NoSuchElementException(java.util.NoSuchElementException)

Example 27 with ServiceLoader

use of java.util.ServiceLoader in project felix by apache.

the class AbstractPojoSRTestCase method setUp.

/* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
protected void setUp() throws Exception {
    super.setUp();
    // Initialize service registry
    ServiceLoader loader = ServiceLoader.load(PojoServiceRegistryFactory.class);
    registry = ((PojoServiceRegistryFactory) loader.iterator().next()).newPojoServiceRegistry(new HashMap());
    assertNotNull(registry);
    // Initialize bundle
    activator = new Activator();
    activator.start(registry.getBundleContext());
}
Also used : ServiceLoader(java.util.ServiceLoader) HashMap(java.util.HashMap) Activator(org.apache.felix.httplite.osgi.Activator)

Example 28 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 29 with ServiceLoader

use of java.util.ServiceLoader in project Bytecoder by mirkosertic.

the class UrlDeserializedState method providers.

private static Iterator<URLStreamHandlerProvider> providers() {
    return new Iterator<>() {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        ServiceLoader<URLStreamHandlerProvider> sl = ServiceLoader.load(URLStreamHandlerProvider.class, cl);

        Iterator<URLStreamHandlerProvider> i = sl.iterator();

        URLStreamHandlerProvider next = null;

        private boolean getNext() {
            while (next == null) {
                try {
                    if (!i.hasNext())
                        return false;
                    next = i.next();
                } catch (ServiceConfigurationError sce) {
                    if (sce.getCause() instanceof SecurityException) {
                        // Ignore security exceptions
                        continue;
                    }
                    throw sce;
                }
            }
            return true;
        }

        public boolean hasNext() {
            return getNext();
        }

        public URLStreamHandlerProvider next() {
            if (!getNext())
                throw new NoSuchElementException();
            URLStreamHandlerProvider n = next;
            next = null;
            return n;
        }
    };
}
Also used : ServiceLoader(java.util.ServiceLoader) URLStreamHandlerProvider(java.net.spi.URLStreamHandlerProvider) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) NoSuchElementException(java.util.NoSuchElementException)

Example 30 with ServiceLoader

use of java.util.ServiceLoader in project Bytecoder by mirkosertic.

the class Charset method providers.

// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown.  Should be invoked with full privileges.
// 
private static Iterator<CharsetProvider> providers() {
    return new Iterator<>() {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        ServiceLoader<CharsetProvider> sl = ServiceLoader.load(CharsetProvider.class, cl);

        Iterator<CharsetProvider> i = sl.iterator();

        CharsetProvider next = null;

        private boolean getNext() {
            while (next == null) {
                try {
                    if (!i.hasNext())
                        return false;
                    next = i.next();
                } catch (ServiceConfigurationError sce) {
                    if (sce.getCause() instanceof SecurityException) {
                        // Ignore security exceptions
                        continue;
                    }
                    throw sce;
                }
            }
            return true;
        }

        public boolean hasNext() {
            return getNext();
        }

        public CharsetProvider next() {
            if (!getNext())
                throw new NoSuchElementException();
            CharsetProvider n = next;
            next = null;
            return n;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ServiceLoader(java.util.ServiceLoader) CharsetProvider(java.nio.charset.spi.CharsetProvider) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ServiceLoader (java.util.ServiceLoader)59 Iterator (java.util.Iterator)42 Test (org.junit.Test)40 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 NoSuchElementException (java.util.NoSuchElementException)3 ServiceConfigurationError (java.util.ServiceConfigurationError)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CharsetProvider (java.nio.charset.spi.CharsetProvider)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Optional (java.util.Optional)2 ResourceBundle (java.util.ResourceBundle)2 BundleReference (org.osgi.framework.BundleReference)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Person (com.dockerx.traffic.providespro.entity.Person)1 Analysis (com.dockerx.traffic.providespro.service.Analysis)1 Joiner.on (com.google.common.base.Joiner.on)1 ImmutableMap (com.google.common.collect.ImmutableMap)1