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

io.scalecube.services.ServiceScanner Maven / Gradle / Ivy

package io.scalecube.services;

import io.scalecube.services.annotations.Service;
import io.scalecube.services.annotations.ServiceMethod;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ServiceScanner {

  private ServiceScanner() {
    // Do not instantiate
  }

  /**
   * Scans {@code ServiceInfo} and builds list of {@code ServiceRegistration}-s.
   *
   * @param serviceInfo service info instance
   * @return list of {@code ServiceRegistration}-s
   */
  public static List scanServiceInfo(ServiceInfo serviceInfo) {
    return Arrays.stream(serviceInfo.serviceInstance().getClass().getInterfaces())
        .filter(serviceInterface -> serviceInterface.isAnnotationPresent(Service.class))
        .map(
            serviceInterface -> {
              Map serviceInfoTags = serviceInfo.tags();
              Map apiTags = Reflect.serviceTags(serviceInterface);
              Map buffer = new HashMap<>(apiTags);
              // service tags override tags from @Service
              buffer.putAll(serviceInfoTags);
              return new InterfaceInfo(serviceInterface, Collections.unmodifiableMap(buffer));
            })
        .map(
            interfaceInfo -> {
              Class serviceInterface = interfaceInfo.serviceInterface;
              Map serviceTags = interfaceInfo.tags;
              String namespace = Reflect.serviceName(serviceInterface);
              List actions =
                  Arrays.stream(serviceInterface.getMethods())
                      .filter(method -> method.isAnnotationPresent(ServiceMethod.class))
                      .map(
                          method ->
                              new ServiceMethodDefinition(
                                  Reflect.methodName(method),
                                  Reflect.serviceMethodTags(method),
                                  Reflect.isSecured(method)))
                      .collect(Collectors.toList());
              return new ServiceRegistration(namespace, serviceTags, actions);
            })
        .collect(Collectors.toList());
  }

  /** Tuple class. Contains service interface along with tags map. */
  private static class InterfaceInfo {
    private final Class serviceInterface;
    private final Map tags;

    private InterfaceInfo(Class serviceInterface, Map tags) {
      this.serviceInterface = serviceInterface;
      this.tags = tags;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy