Java Code Examples for java.util.ServiceLoader#forEach()

The following examples show how to use java.util.ServiceLoader#forEach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmallRyeConfigBuilder.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
List<ConfigSource> discoverSources() {
    List<ConfigSource> discoveredSources = new ArrayList<>();
    ServiceLoader<ConfigSource> configSourceLoader = ServiceLoader.load(ConfigSource.class, classLoader);
    configSourceLoader.forEach(discoveredSources::add);

    // load all ConfigSources from ConfigSourceProviders
    ServiceLoader<ConfigSourceProvider> configSourceProviderLoader = ServiceLoader.load(ConfigSourceProvider.class,
            classLoader);
    configSourceProviderLoader.forEach(configSourceProvider -> configSourceProvider.getConfigSources(classLoader)
            .forEach(discoveredSources::add));

    ServiceLoader<ConfigSourceFactory> configSourceFactoryLoader = ServiceLoader.load(ConfigSourceFactory.class,
            classLoader);
    configSourceFactoryLoader.forEach(factory -> discoveredSources.add(new ConfigurableConfigSource(factory)));

    return discoveredSources;
}
 
Example 2
Source File: SnippetRegistry.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Snippet registry for a given language id.
 * 
 * @param languageId  the language id and null otherwise.
 * @param loadDefault true if default snippets from SPI must be loaded and false
 *                    otherwise.
 */
public SnippetRegistry(String languageId, boolean loadDefault) {
	snippets = new ArrayList<>();
	// Load snippets from SPI
	if (loadDefault) {
		ServiceLoader<ISnippetRegistryLoader> loaders = ServiceLoader.load(ISnippetRegistryLoader.class);
		loaders.forEach(loader -> {
			if (Objects.equals(languageId, loader.getLanguageId())) {
				try {
					loader.load(this);
				} catch (Exception e) {
					LOGGER.log(Level.SEVERE, "Error while consumming snippet loader " + loader.getClass().getName(),
							e);
				}
			}
		});
	}
}
 
Example 3
Source File: FractionUsageAnalyzer.java    From thorntail with Apache License 2.0 6 votes vote down vote up
private void loadDetectorsAndScanners() {
    if (detectorsLoaded) {
        return;
    }

    ServiceLoader<FractionDetector> detectorLoader = ServiceLoader.load(FractionDetector.class);
    detectorLoader.forEach(d -> detectors.add(d));

    scanners.add(new WarScanner());
    scanners.add(new JarScanner());
    scanners.add(new ClassAndPackageScanner());
    scanners.add(new WebXmlDescriptorScanner());
    scanners.add(new FilePresenceScanner());

    ClassAndPackageScanner.classesPackagesAlreadyDetected.clear();

    detectorsLoaded = true;
}
 
Example 4
Source File: DefaultBleach.java    From DocBleach with MIT License 5 votes vote down vote up
/**
 * Finds all statically loadable bleaches
 *
 * @return ordered list of statically loadable bleaches
 */
private static Bleach[] getDefaultBleaches() {
  ServiceLoader<Bleach> services = ServiceLoader.load(Bleach.class);

  Collection<Bleach> list = new ArrayList<>();
  services.forEach(list::add);

  return list.toArray(new Bleach[0]);
}
 
Example 5
Source File: ExchangeRate.java    From tutorials with MIT License 5 votes vote down vote up
public static List<ExchangeRateProvider> providers() {
    List<ExchangeRateProvider> services = new ArrayList<>();
    ServiceLoader<ExchangeRateProvider> loader = ServiceLoader.load(ExchangeRateProvider.class);
    loader.forEach(exchangeRateProvider -> {
        services.add(exchangeRateProvider);
    });
    return services;
}
 
Example 6
Source File: ModuleScanner.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public static List<Module> findModules() {
  // also search if classes are provided through service loader mechanism
  // It's useful when the scanning is disabled or ServletContainerInitializer is disabled.
  // onStartup may not be called at all so it's another way of plugging modules.
  ServiceLoader<ModuleFinder> moduleFinderServiceLoader = ServiceLoader.load(ModuleFinder.class);
  moduleFinderServiceLoader.forEach(moduleFinder -> modules.addAll(moduleFinder.getModules()));
  return new ArrayList<>(modules);
}
 
Example 7
Source File: DataConverterImpl.java    From yandex-translate-api with MIT License 5 votes vote down vote up
private static Gson createConverter() {
    final GsonBuilder builder = new GsonBuilder();
    final ServiceLoader<TypeAdapterFactory> serviceLoader =
        ServiceLoader.load(TypeAdapterFactory.class);

    serviceLoader.forEach(builder::registerTypeAdapterFactory);

    return builder.create();
}
 
Example 8
Source File: SubsystemRegistryBasic.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
public void load() {
    synchronized (registryLock) {
        // Find subsystems asking for initialization. 
        ServiceLoader<T> sl = 
            // Use this->classloader form : better for OSGi 
            ServiceLoader.load(classAtRuntime, this.getClass().getClassLoader()) ;
        sl.forEach(this::add) ;
    }
}
 
Example 9
Source File: BuckExtensionFinder.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
  Map<Class<?>, ExtensionWrapper<T>> extensions = new HashMap<>();

  ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
  serviceLoader.forEach(extension -> addExtension(extensions, extension));

  for (PluginWrapper pluginWrapper : pluginManager.getPlugins()) {
    ServiceLoader<T> pluginServiceLoader =
        ServiceLoader.load(type, pluginWrapper.getPluginClassLoader());
    pluginServiceLoader.forEach(extension -> addExtension(extensions, extension));
  }

  return new ArrayList<>(extensions.values());
}
 
Example 10
Source File: XMLExtensionsRegistry.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized void initialize() {

		if (initialized) {
			return;
		}

		ServiceLoader<IXMLExtension> extensions = ServiceLoader.load(IXMLExtension.class);
		extensions.forEach(extension -> {
			registerExtension(extension);
		});
		initialized = true;
	}
 
Example 11
Source File: SmallRyeConfigBuilder.java    From smallrye-config with Apache License 2.0 5 votes vote down vote up
List<InterceptorWithPriority> discoverInterceptors() {
    List<InterceptorWithPriority> interceptors = new ArrayList<>();
    ServiceLoader<ConfigSourceInterceptor> interceptorLoader = ServiceLoader.load(ConfigSourceInterceptor.class,
            classLoader);
    interceptorLoader.forEach(interceptor -> interceptors.add(new InterceptorWithPriority(interceptor)));

    ServiceLoader<ConfigSourceInterceptorFactory> interceptorFactoryLoader = ServiceLoader
            .load(ConfigSourceInterceptorFactory.class, classLoader);
    interceptorFactoryLoader.forEach(interceptor -> interceptors.add(new InterceptorWithPriority(interceptor)));

    return interceptors;
}
 
Example 12
Source File: ExpressionEvaluatorProvider.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public ExpressionEvaluatorProvider() {
    ServiceLoader<ExpressionEvaluator> foundExpressionEvaluators = ServiceLoader.load(ExpressionEvaluator.class);
    foundExpressionEvaluators.forEach(expressionEvaluator -> {
        expressionEvaluatorMap.put(expressionEvaluator.getName(),
                                   expressionEvaluator);
        logger.info("Found expression evaluator with name: " + expressionEvaluator.getName());
    });
}
 
Example 13
Source File: JavaSPITest.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Test
public void sayHello() {
    ServiceLoader<Robot> serviceLoader = ServiceLoader.load(Robot.class);
    System.out.println("Java SPI");
    serviceLoader.forEach(Robot::sayHello);
}
 
Example 14
Source File: ModelTransformer.java    From smithy with Apache License 2.0 4 votes vote down vote up
private static ModelTransformer createWithServiceLoader(ServiceLoader<ModelTransformerPlugin> serviceLoader) {
    List<ModelTransformerPlugin> plugins = new ArrayList<>();
    serviceLoader.forEach(plugins::add);
    return createWithPlugins(plugins);
}
 
Example 15
Source File: ErrorInterpreterRegistry.java    From rug-cli with GNU General Public License v3.0 4 votes vote down vote up
private void init() {
    ServiceLoader<ErrorInterpreter> loader = ServiceLoader.load(ErrorInterpreter.class);
    loader.forEach(i -> interpreters.add(i));
    interpreters = interpreters.stream().sorted(Comparator.comparingInt(ErrorInterpreter::order))
            .collect(toList());
}
 
Example 16
Source File: ServiceLoadingCommandInfoRegistry.java    From rug-cli with GNU General Public License v3.0 4 votes vote down vote up
private void init() {
    ServiceLoader<CommandInfo> loader = ServiceLoader.load(CommandInfo.class);
    loader.forEach(c -> commands.add(c));
    commands = commands.stream().sorted(Comparator.comparingInt(CommandInfo::order))
            .collect(toList());
}
 
Example 17
Source File: DefaultConfigBuilder.java    From microprofile-jwt-auth with Apache License 2.0 4 votes vote down vote up
@Override
public ConfigBuilder addDiscoveredConverters() {
    ServiceLoader<Converter> converters = ServiceLoader.load(Converter.class, loader);
    converters.forEach(converter -> config.addConverter(converter));
    return this;
}
 
Example 18
Source File: ServiceCatalogMappingTest.java    From dekorate with Apache License 2.0 4 votes vote down vote up
@Test
void shouldLoadProvider() {
  ServiceLoader loader = ServiceLoader.load(KubernetesResourceMappingProvider.class);
  loader.forEach(l -> System.out.println("Found loader:" + l ));
}
 
Example 19
Source File: UriTransportRegistry.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
public UriTransportRegistry(ServiceLoader<UriHandler> services) {
  handlers = new ArrayList<>();
  services.forEach(handlers::add);
}
 
Example 20
Source File: FhirContexts.java    From bunsen with Apache License 2.0 3 votes vote down vote up
/**
 * Loads structure definitions for supported profiles.
 */
private static void loadProfiles(FhirContext context) {

  ServiceLoader<ProfileProvider> loader = ServiceLoader.load(ProfileProvider.class);

  loader.forEach(provider ->
      provider.loadStructureDefinitions(context));

}