Example usage for java.util ServiceLoader iterator

List of usage examples for java.util ServiceLoader iterator

Introduction

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

Prototype

public Iterator<S> iterator() 

Source Link

Document

Returns an iterator to lazily load and instantiate the available providers of this loader's service.

Usage

From source file:org.apache.geode.geospatial.utils.ToolBox.java

public static <T> T getService(Class clazz) {
    ServiceLoader<T> loader = ServiceLoader.load(clazz);

    T returnValue = null;//from   ww  w  .  ja  va  2  s. c o  m

    Iterator<T> it = loader.iterator();
    //In theory the service providers are loaded lazily - so if there is a problem with a given service provider we
    // will log it and try the next.
    while (it.hasNext()) {
        try {
            returnValue = it.next();
            if (returnValue != null) {
                break;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("Trying to find another ", clazz.getName());
        }
    }
    return returnValue;
}

From source file:com.fluidops.iwb.api.valueresolver.ValueResolverUtil.java

/**
 * This method initializes custom {@link ValueResolver} extensions
 * using the Java {@link ServiceLoader} mechanism. It is invoked
 * on startup of the application, and looks up all registered
 * {@link ValueResolverFactory} in META-INF. See {@link ValueResolver}
 * for details.//from   ww  w  .ja va2 s  .co m
 */
public static void initializeValueResolverExtensions() {

    logger.info("Initializing value resolver extensions");

    ServiceLoader<ValueResolverFactory> serviceLoader = ServiceLoader.load(ValueResolverFactory.class);
    Iterator<ValueResolverFactory> iter = serviceLoader.iterator();
    while (iter.hasNext()) {
        ValueResolverFactory vrf = iter.next();
        ValueResolver valueResolver = vrf.create();
        logger.debug("Registered custom value resolver: " + valueResolver.name());
        ValueResolverRegistry.getInstance().register(valueResolver);
    }
}

From source file:org.apache.hadoop.gateway.GatewaySslFuncTest.java

private static GatewayServices instantiateGatewayServices() {
    ServiceLoader<GatewayServices> loader = ServiceLoader.load(GatewayServices.class);
    Iterator<GatewayServices> services = loader.iterator();
    if (services.hasNext()) {
        return services.next();
    }//from   w w w  . jav a 2s  .  co m
    return null;
}

From source file:com.alibaba.dragoon.common.protocol.transport.DragoonMessageCodec.java

public DragoonMessageCodec() {
    decoders.add(new DefaultDragoonMessageDecoder());

    ServiceLoader<DragoonMessageDecoder> serviceLoader = ServiceLoader.load(DragoonMessageDecoder.class);
    Iterator<DragoonMessageDecoder> it = serviceLoader.iterator();
    while (it.hasNext()) {
        DragoonMessageDecoder decoder = it.next();
        decoders.add(decoder);/*  w  w w.  j  av a2 s.  c  o m*/
    }
}

From source file:com.twosigma.beaker.sql.JDBCClient.java

public void loadDrivers(List<String> pathList) {
    synchronized (this) {

        dsMap = new HashMap<>();
        drivers = new HashSet<>();

        Set<URL> urlSet = new HashSet<>();

        String dbDriverString = System.getenv("BEAKER_JDBC_DRIVER_LIST");
        if (dbDriverString != null && !dbDriverString.isEmpty()) {
            String[] dbDriverList = dbDriverString.split(File.pathSeparator);
            for (String s : dbDriverList) {
                try {
                    urlSet.add(toURL(s));
                } catch (MalformedURLException e) {
                    logger.error(e.getMessage());
                }/* w  w w.  j  a  v a  2 s . co m*/
            }
        }

        if (pathList != null) {
            for (String path : pathList) {
                path = path.trim();
                if (path.startsWith("--") || path.startsWith("#")) {
                    continue;
                }
                try {
                    urlSet.add(toURL(path));
                } catch (MalformedURLException e) {
                    logger.error(e.getMessage());
                }
            }
        }

        URLClassLoader loader = new URLClassLoader(urlSet.toArray(new URL[urlSet.size()]));

        ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class, loader);
        Iterator<Driver> driversIterator = loadedDrivers.iterator();
        try {
            while (driversIterator.hasNext()) {
                Driver d = driversIterator.next();
                drivers.add(d);
            }
        } catch (Throwable t) {
            logger.error(t.getMessage());
        }
    }
}

From source file:org.camunda.bpm.admin.impl.web.SetupResource.java

protected ProcessEngine lookupProcessEngine(String engineName) {

    ServiceLoader<ProcessEngineProvider> serviceLoader = ServiceLoader.load(ProcessEngineProvider.class);
    Iterator<ProcessEngineProvider> iterator = serviceLoader.iterator();

    if (iterator.hasNext()) {
        ProcessEngineProvider provider = iterator.next();
        return provider.getProcessEngine(engineName);

    } else {/* w  w  w. ja  va 2s. c  o  m*/
        throw new RestException(Status.BAD_REQUEST,
                "Could not find an implementation of the " + ProcessEngineProvider.class + "- SPI");

    }

}

From source file:com.clustercontrol.plugin.HinemosPluginService.java

/**
 * private(Singleton)./*from w w w  .  ja  va 2 s  .  c  o m*/
 */
private HinemosPluginService() {
    String pluginDir = System.getProperty("hinemos.manager.home.dir") + File.separator + "plugins";
    try {
        // /opt/hinemos/plugins??jar??
        ClassUtils.addDirToClasspath(new File(pluginDir));
        log.info("initialized plugin directory : " + pluginDir);
    } catch (Exception e) {
        log.warn("classpath configure failure. (pluginDir = " + pluginDir + ")", e);
    }

    ServiceLoader<HinemosPlugin> serviceLoader = ServiceLoader.load(HinemosPlugin.class);
    Iterator<HinemosPlugin> itr = serviceLoader.iterator();
    while (itr.hasNext()) {
        HinemosPlugin plugin = itr.next();

        pluginMap.put(plugin.getClass().getName(), plugin);
        pluginStatusMap.put(plugin.getClass().getName(), PluginStatus.NULL);
    }

}

From source file:org.apache.hadoop.gateway.identityasserter.function.UsernameFunctionProcessorTest.java

@Test
public void testServiceLoader() throws Exception {
    ServiceLoader loader = ServiceLoader.load(UrlRewriteFunctionProcessor.class);
    Iterator iterator = loader.iterator();
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof UsernameFunctionProcessor) {
            return;
        }/*  w w w.jav  a 2s  .  c  o  m*/
    }
    fail("Failed to find UsernameFunctionProcessor via service loader.");
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.FrontendFunctionProcessorTest.java

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

From source file:com.intelligentsia.dowsers.entity.reference.ReferenceTest.java

@Test
public void testReferenceFactoryProviderReference() {
    final ServiceLoader<ReferenceFactory> loader = ServiceLoader.load(ReferenceFactory.class,
            Thread.currentThread().getContextClassLoader());
    assertTrue(loader.iterator().hasNext());
    final Reference urn = new Reference(Person.class);
    assertEquals("com.intelligentsia.dowsers.entity.model.Person", urn.getEntityClassName());
}