Neminda Prabhashwara
Neminda Prabhashwara

Reputation: 434

Parameters, return value of static ServiceLoader.load() method?

Actually this a three part question.

When I looked at the documentation of static ServiceLoader.load() method of ServiceLoader class it contained

public static <S> ServiceLoader<S> load(Class<S> service)

1 I need to know how to call this method, what is this parameter Class<S> service?

Assumed SomeClass is an interface or abstract class,

I saw in examples it used ServiceLoader.load(SomeClass.class)

2 What is class keyword doing in here can't we simply support SomeClass as the parameter?

like this,

ServiceLoader.load(SomeClass)

In the documentation it contained this method returns a new service loader, I'm confused with this.

3 Does this method returns an array of all the implementations of the interface?

Upvotes: 0

Views: 518

Answers (1)

Mohsen Zamani
Mohsen Zamani

Reputation: 506

  1. Class<S> service is the class of the service type; e.g. Interface or Abstract class that should be implemented by service providers. load method is a static method, so to call it just use ServiceLoader.load(SomeService.class) syntax.

  2. In load(Class<S> service) parameter is the the class of the service type; So .class should be indicated when calling this method.

  3. The load(Class<S> service) returns a new instance of ServiceLoader class. This class implements Iterable interface. When you invoke iterator() for this instance, an iterator will be returned that lazily loads providers for this loader's service.

Here a sample code is provided that meybe helps. The Adapter interface is the service that should be implemented by service providers:

import java.util.Map;

public interface Adapter {
  String getName();

  String getApiVersion();

  void initialize();

  int calculateScore(Map<String, String> data);
}

And AdapterManager class is used to load service providers:

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public final class AdapterManager {
  private final static ArrayList<Adapter> adapters = new ArrayList<>();
  private final static AdapterManager INSTANCE = new AdapterManager();

  private AdapterManager() {
    load();
  }

  private static void load() {
    updateAdapters(findServiceProviders());
  }

  private static List<Adapter> findServiceProviders() {
    ServiceLoader<Adapter> serviceLoader = ServiceLoader.load(Adapter.class);
    List<Adapter> providerList = new ArrayList<>();
    for (Adapter provider : serviceLoader) {
      providerList.add(provider);
    }
    return providerList;
  }

  private static synchronized void updateAdapters(List<Adapter> adapterProviders) {
    adapters.clear();
    adapters.addAll(adapterProviders);
  }

  public static AdapterManager getInstance() {
    return INSTANCE;
  }

  public void reload() {
    load();
  }

  public Adapter getAdapter(String name) {
    if (name == null) {
      throw new IllegalArgumentException("Adapter name must not be null");
    }

    for (Adapter adapter : adapters) {
      if (name.equals(adapter.getName())) {
        return adapter;
      }
    }
    return null;
  }
}

Upvotes: 1

Related Questions