本文整理汇总了Java中java.util.ServiceLoader.iterator方法的典型用法代码示例。如果您正苦于以下问题:Java ServiceLoader.iterator方法的具体用法?Java ServiceLoader.iterator怎么用?Java ServiceLoader.iterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.ServiceLoader
的用法示例。
在下文中一共展示了ServiceLoader.iterator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadProviderAsService
import java.util.ServiceLoader; //导入方法依赖的package包/类
private static AsynchronousChannelProvider loadProviderAsService() {
ServiceLoader<AsynchronousChannelProvider> sl =
ServiceLoader.load(AsynchronousChannelProvider.class,
ClassLoader.getSystemClassLoader());
Iterator<AsynchronousChannelProvider> i = sl.iterator();
for (;;) {
try {
return (i.hasNext()) ? i.next() : null;
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore the security exception, try the next provider
continue;
}
throw sce;
}
}
}
示例2: loadProviderAsService
import java.util.ServiceLoader; //导入方法依赖的package包/类
private static boolean loadProviderAsService() {
ServiceLoader<SelectorProvider> sl =
ServiceLoader.load(SelectorProvider.class,
ClassLoader.getSystemClassLoader());
Iterator<SelectorProvider> i = sl.iterator();
for (;;) {
try {
if (!i.hasNext())
return false;
provider = i.next();
return true;
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore the security exception, try the next provider
continue;
}
throw sce;
}
}
}
示例3: initialize
import java.util.ServiceLoader; //导入方法依赖的package包/类
/**
* Initialize a specific type
*
* @param type
* the initializer specific type, see
* {@link Initializer#initialize(Object)}
* @param classloader
* a specific class loader to use
*/
public static void initialize ( final Object type, final ClassLoader classloader )
{
logger.debug ( "Initializing: {}", type );
final ServiceLoader<Initializer> loader = ServiceLoader.load ( Initializer.class, classloader );
final Iterator<Initializer> i = loader.iterator ();
while ( i.hasNext () )
{
final Initializer initializer = i.next ();
logger.debug ( "Processing: {}", initializer );
try
{
initializer.initialize ( type );
}
catch ( final Exception e )
{
logger.info ( "Failed to initialize", e );
}
}
}
示例4: test
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Test
public void test() {
ServiceLoader<ClientModuleFactory> loader = ServiceLoader.load(ClientModuleFactory.class);
Iterator<ClientModuleFactory> iterator = loader.iterator();
Set<Class> moduleClasses = new HashSet<>();
while (iterator.hasNext()) {
ClientModuleFactory moduleFactory = iterator.next();
Module module = moduleFactory.create();
moduleClasses.add(module.getClass());
}
Assert.assertEquals(2, moduleClasses.size());
Assert.assertTrue(moduleClasses.contains(ValidationModule.class));
Assert.assertTrue(moduleClasses.contains(MetaModule.class));
}
示例5: testIteratorNext
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Test(expectedExceptions = { ConcurrentModificationException.class })
public void testIteratorNext() {
ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);
Iterator<ScriptEngineFactory> iterator = sl.iterator();
assertTrue(iterator.hasNext());
sl.reload();
iterator.next();
}
示例6: getInstance
import java.util.ServiceLoader; //导入方法依赖的package包/类
public static BookService getInstance(){
ServiceLoader<BookServiceProvider> sl
= ServiceLoader.load(BookServiceProvider.class);
Iterator<BookServiceProvider> iter = sl.iterator();
if (!iter.hasNext())
throw new RuntimeException("No service providers found!");
BookServiceProvider provider = null;
while(iter.hasNext()){
provider = iter.next();
System.out.println(provider.getClass());
}
return provider.getBookService();
}
示例7: testSequentialUse1
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Test
public void testSequentialUse1() {
ServiceLoader<S> sl = ServiceLoader.load(S.class, testClassLoader);
Iterator<S> iterator1 = sl.iterator();
iterator1.next();
iterator1.next();
assertFalse(iterator1.hasNext());
Iterator<S> iterator2 = sl.iterator();
iterator2.next();
iterator2.next();
assertFalse(iterator2.hasNext());
}
示例8: validateServiceIsLocatableViaServiceLoader
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Test
public void validateServiceIsLocatableViaServiceLoader() {
ServiceLoader<ControllerService> loader = ServiceLoader.<ControllerService> load(ControllerService.class);
Iterator<ControllerService> iter = loader.iterator();
boolean present = false;
while (iter.hasNext()) {
ControllerService cs = iter.next();
assertTrue(cs instanceof JMSConnectionFactoryProviderDefinition);
present = true;
}
assertTrue(present);
}
示例9: loadFirst
import java.util.ServiceLoader; //导入方法依赖的package包/类
public static <S> S loadFirst(Class<S> clazz) {
final ServiceLoader<S> loader = loadAll(clazz);
final Iterator<S> iterator = loader.iterator();
if (!iterator.hasNext()) {
throw new IllegalStateException(String.format(
"No implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!",
clazz.getName()));
}
return iterator.next();
}
示例10: getInstance
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Override
public ServiceDiscovery getInstance() {
ServiceLoader<ServiceDiscovery> loader = ServiceLoader.load(ServiceDiscovery.class);
Iterator<ServiceDiscovery> iterator = loader.iterator();
if (iterator.hasNext()) {
ServiceDiscovery discovery = iterator.next();
PreconditionUtil.assertFalse("expected unique ServiceDiscovery implementation, got: " + loader, iterator.hasNext());
return discovery;
}
return null;
}
示例11: test
import java.util.ServiceLoader; //导入方法依赖的package包/类
@Test
public void test() {
ServiceLoader<ClientModuleFactory> loader = ServiceLoader.load(ClientModuleFactory.class);
Iterator<ClientModuleFactory> iterator = loader.iterator();
Assert.assertTrue(iterator.hasNext());
ClientModuleFactory moduleFactory = iterator.next();
Assert.assertFalse(iterator.hasNext());
Module module = moduleFactory.create();
Assert.assertTrue(module instanceof MetaModule);
}
示例12: getInstance
import java.util.ServiceLoader; //导入方法依赖的package包/类
public static ReportWriter getInstance() {
ServiceLoader<ReportWriter> loader = ServiceLoader.load(ReportWriter.class);
final Iterator<ReportWriter> it = loader.iterator();
final ReportWriter server;
if (!it.hasNext()) {
throw new RuntimeException("No ReportWriter in classpath");
} else {
server = it.next();
}
if (it.hasNext()) {
LoggerFactory.getLogger(ReportWriter.class).warn("Multiple bindings for ReportWriter found: {}", loader.iterator());
}
return server;
}
示例13: loadAdditionalSuits
import java.util.ServiceLoader; //导入方法依赖的package包/类
public static void loadAdditionalSuits(List<PluginProvider> providers) {
ServiceLoader<PluginProvider> serviceLoader = ServiceLoader
.load(PluginProvider.class);
Iterator<PluginProvider> iterator = serviceLoader.iterator();
while (iterator.hasNext()) {
PluginProvider provider = iterator.next();
providers.add(provider);
}
}
示例14: create
import java.util.ServiceLoader; //导入方法依赖的package包/类
public static HttpServer create(int port) {
ServiceLoader<HttpServerProvider> sl
= ServiceLoader.load(HttpServerProvider.class);
Iterator<HttpServerProvider> iterator = sl.iterator();
if (iterator.hasNext()) {
HttpServerProvider provider = iterator.next();
return provider.createHttpServer(port);
} else {
return new HttpServer(port) { };
}
}
示例15: determineCompilerOrThrow
import java.util.ServiceLoader; //导入方法依赖的package包/类
private void determineCompilerOrThrow()
{
final String preferedCompiler = getProperty("preferedCompiler");
LinkedList<JavaCompiler> allCompilers = null;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler != null)
{
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
_compiler = compiler;
return;
}
allCompilers = new LinkedList<>();
allCompilers.add(compiler);
}
final ServiceLoader<JavaCompiler> thirdPartyCompilers = ServiceLoader.load(JavaCompiler.class);
Iterator<JavaCompiler> compilersIterator = thirdPartyCompilers.iterator();
while (compilersIterator.hasNext())
{
compiler = compilersIterator.next();
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
_compiler = compiler;
return;
}
if (allCompilers == null)
{
allCompilers = new LinkedList<>();
}
allCompilers.add(compilersIterator.next());
}
if (allCompilers != null)
{
compilersIterator = allCompilers.iterator();
while (compilersIterator.hasNext())
{
compiler = compilersIterator.next();
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
break;
}
}
}
if (compiler == null)
{
throw new IllegalStateException("No javax.tools.JavaCompiler service installed!");
}
_compiler = compiler;
}