All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.support.LazyServiceLoaderCache Maven / Gradle / Ivy

The newest version!
package net.jqwik.engine.support;

import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;

public class LazyServiceLoaderCache {
	private static final Logger LOG = Logger.getLogger(LazyServiceLoaderCache.class.getName());

	private final Class clz;
	private List services;

	public LazyServiceLoaderCache(Class clz) {
		this.clz = clz;
	}

	public List getServices() {
		if (services == null) {
			loadServices();
		}
		return services;
	}

	private synchronized void loadServices() {
		services = new CopyOnWriteArrayList<>();
		try {
			for (S s : ServiceLoader.load(clz)) {
				services.add(s);
			}
		} catch (ServiceConfigurationError serviceConfigurationError) {
			String message = String.format(
					"Cannot load services of type [%s].%n    %s",
					clz.getName(),
					serviceConfigurationError.getMessage()
			);
			LOG.log(Level.SEVERE, message);
		}
	}
}