Example usage for java.util ServiceLoader reload

List of usage examples for java.util ServiceLoader reload

Introduction

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

Prototype

public void reload() 

Source Link

Document

Clear this loader's provider cache so that all providers will be reloaded.

Usage

From source file:org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader.java

/**
 * Return a reference to the shim service.  This method may return a cached shim service
 * unless {@code forceReload} is true.  Calls to this method need not be externally
 * synchonized./*from w ww.  ja v  a  2s .  co m*/
 *
 * @param forceReload if false, this method may use its internal service cache; if true,
 *                    this method must ignore cache, and it must invoke {@link ServiceLoader#reload()}
 *                    before selecting a new service to return
 * @return the shim service
 */
private static KryoShimService load(final boolean forceReload) {
    // if the service is loaded and doesn't need reloading, simply return in
    if (null != cachedShimService && !forceReload)
        return cachedShimService;

    // if a service is already loaded, close it
    if (null != cachedShimService)
        cachedShimService.close();

    // if the configuration is null, try and load the configuration from System.properties
    if (null == configuration)
        configuration = SystemUtil.getSystemPropertiesConfiguration("tinkerpop", true);

    // get all of the shim services
    final ArrayList<KryoShimService> services = new ArrayList<>();
    final ServiceLoader<KryoShimService> serviceLoader = ServiceLoader.load(KryoShimService.class);
    synchronized (KryoShimServiceLoader.class) {
        if (forceReload)
            serviceLoader.reload();
        for (final KryoShimService kss : serviceLoader) {
            services.add(kss);
        }
    }
    // if a shim service class is specified in the configuration, use it -- else, priority-based
    if (configuration.containsKey(KRYO_SHIM_SERVICE)) {
        for (final KryoShimService kss : services) {
            if (kss.getClass().getCanonicalName().equals(configuration.getString(KRYO_SHIM_SERVICE))) {
                log.info("Set KryoShimService to {} because of configuration {}={}",
                        kss.getClass().getSimpleName(), KRYO_SHIM_SERVICE,
                        configuration.getString(KRYO_SHIM_SERVICE));
                cachedShimService = kss;
                break;
            }
        }
    } else {
        Collections.sort(services, KryoShimServiceComparator.INSTANCE);
        for (final KryoShimService kss : services) {
            log.debug("Found KryoShimService: {} (priority {})", kss.getClass().getCanonicalName(),
                    kss.getPriority());
        }
        if (0 != services.size()) {
            cachedShimService = services.get(services.size() - 1);
            log.info("Set KryoShimService to {} because its priority value ({}) is the best available",
                    cachedShimService.getClass().getSimpleName(), cachedShimService.getPriority());
        }
    }

    // no shim service was available
    if (null == cachedShimService)
        throw new IllegalStateException("Unable to load KryoShimService");

    // once the shim service is defined, configure it
    log.info(
            "Configuring KryoShimService {} with the following configuration:\n#######START########\n{}\n########END#########",
            cachedShimService.getClass().getCanonicalName(), ConfigurationUtils.toString(configuration));
    cachedShimService.applyConfiguration(configuration);
    return cachedShimService;
}