Example usage for java.util ServiceLoader load

List of usage examples for java.util ServiceLoader load

Introduction

In this page you can find the example usage for java.util ServiceLoader load.

Prototype

@CallerSensitive
public static <S> ServiceLoader<S> load(Class<S> service) 

Source Link

Document

Creates a new service loader for the given service type, using the current thread's java.lang.Thread#getContextClassLoader context class loader .

Usage

From source file:org.apache.hadoop.gateway.hostmap.impl.HostmapDeploymentContributorTest.java

@Test
public void testServiceLoader() throws Exception {
    ServiceLoader loader = ServiceLoader.load(ProviderDeploymentContributor.class);
    Iterator iterator = loader.iterator();
    assertThat("Service iterator empty.", iterator.hasNext());
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof HostmapDeploymentContributor) {
            return;
        }// w  w w.j  a v  a 2s.  c o m
    }
    fail("Failed to find " + HostmapDeploymentContributor.class.getName() + " via service loader.");
}

From source file:uk.bl.wa.analyser.payload.AbstractPayloadAnalyser.java

/**
 * This dynamically loads the available parser implementations on the
 * classpath. Passes in the provided configuration to get things set up.
 * //from   w  w  w . j  a  v a 2s .c  o  m
 * @return
 */
public static List<AbstractPayloadAnalyser> getPayloadAnalysers(Config conf) {

    // load our plugins
    ServiceLoader<AbstractPayloadAnalyser> serviceLoader = ServiceLoader.load(AbstractPayloadAnalyser.class);

    // Get the list:
    List<AbstractPayloadAnalyser> providers = new ArrayList<AbstractPayloadAnalyser>();
    for (AbstractPayloadAnalyser provider : serviceLoader) {
        // Perform any necessary configuration:
        provider.configure(conf);
        providers.add(provider);
    }

    return providers;
}

From source file:dictionary.DictionaryService.java

private DictionaryService() {
    loader = ServiceLoader.load(Dictionary.class);
}

From source file:com.caricah.iotracah.bootstrap.runner.ResourceService.java

public List<BaseSystemHandler> getSystemBaseSetLoader() {

    List<BaseSystemHandler> listBaseSystemHandler = new ArrayList<>();
    for (BaseSystemHandler baseSystemHandler : ServiceLoader.load(BaseSystemHandler.class))
        listBaseSystemHandler.add(baseSystemHandler);

    Collections.sort(listBaseSystemHandler);

    return listBaseSystemHandler;
}

From source file:org.apache.phoenix.util.InstanceResolver.java

/**
 * Resolves all instances of a specified class and add it to the list of default implementations
 * @param clazz Type of the instance to resolve
 * @param defaultInstances {@link List} of instances that match the type clazz
 * @param <T> Type of class passed
 * @return {@link List} of instance of the specified class. Newly found instances will be added
 *          to the existing contents of defaultInstances
 *///from   w w  w  .  j a  v  a2  s  .  com
@SuppressWarnings("unchecked")
public static <T> List get(Class<T> clazz, List<T> defaultInstances) {
    Iterator<T> iterator = ServiceLoader.load(clazz).iterator();
    if (defaultInstances != null) {
        defaultInstances.addAll(IteratorUtils.toList(iterator));
    } else {
        defaultInstances = IteratorUtils.toList(iterator);
    }

    return defaultInstances;
}

From source file:net.sf.hajdbc.state.sql.SQLStateManagerFactory.java

private String defaultUrlPattern() {
    ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);

    for (EmbeddedVendor vendor : EmbeddedVendor.values()) {
        String url = MessageFormat.format(vendor.pattern, "test", Strings.USER_HOME);

        for (Driver driver : drivers) {
            try {
                if (driver.acceptsURL(url)) {
                    return vendor.pattern;
                }//from  ww  w . j  av  a 2  s.  c  om
            } catch (SQLException e) {
                // Ignore
            }
        }
    }

    return null;
}

From source file:com.artistech.tuio.dispatch.TuioSink.java

/**
 * Constructor
 */
public TuioSink() {
    services = ServiceLoader.load(ProtoConverter.class);
}

From source file:de.jfachwert.bank.GeldbetragIT.java

/**
 * Start der TCK-Suite.//from  w  ww.j av  a 2 s. com
 * 
 * @throws IOException falls Resultat nicht gelesen werden kann
 */
@Test
public void runTCK() throws IOException {
    ServiceLoader.load(GeldbetragIT.class);
    TCKRunner.main();
    assertThat("number of failed tests", getNumberOfFailedTests(), lessThanOrEqualTo(1));
}

From source file:com.netflix.iep.gov.Governator.java

/**
 * Returns a list of all guice modules in the classpath using ServiceLoader. Modules that do
 * not have a corresponding provider config will not get loaded.
 *///from www.j a va 2  s. c  o m
public static List<Module> getModulesUsingServiceLoader() {
    ServiceLoader<Module> loader = ServiceLoader.load(Module.class);
    List<Module> modules = new ArrayList<>();
    for (Module m : loader) {
        modules.add(m);
    }
    return modules;
}

From source file:org.trellisldp.api.TrellisUtils.java

/**
 * Get a service.//from  w  w w  .j a v  a 2  s  .  co  m
 * @param service the interface or abstract class representing the service
 * @param <T> the class of the service type
 * @return the first service provider or empty Optional if no service providers are located
 */
private static <T> Optional<T> findFirst(final Class<T> service) {
    return Optional.of(ServiceLoader.load(service)).map(ServiceLoader::iterator).filter(Iterator::hasNext)
            .map(Iterator::next);
}