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

io.scalecube.services.methods.ServiceMethodRegistryImpl Maven / Gradle / Ivy

There is a newer version: 2.11.3.rc9
Show newest version
package io.scalecube.services.methods;

import io.scalecube.services.Reflect;
import io.scalecube.services.ServiceInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ServiceMethodRegistryImpl implements ServiceMethodRegistry {

  private static final Logger LOGGER = LoggerFactory.getLogger(ServiceMethodRegistry.class);

  private final List serviceInfos = new CopyOnWriteArrayList<>();

  private final ConcurrentMap methodInvokers =
      new ConcurrentHashMap<>();

  @Override
  public void registerService(ServiceInfo serviceInfo) {
    serviceInfos.add(serviceInfo);

    Reflect.serviceInterfaces(serviceInfo.serviceInstance())
        .forEach(
            serviceInterface ->
                Reflect.serviceMethods(serviceInterface)
                    .forEach(
                        (key, method) -> {

                          // validate method
                          Reflect.validateMethodOrThrow(method);

                          MethodInfo methodInfo =
                              new MethodInfo(
                                  Reflect.serviceName(serviceInterface),
                                  Reflect.methodName(method),
                                  Reflect.parameterizedReturnType(method),
                                  Reflect.isReturnTypeServiceMessage(method),
                                  Reflect.communicationMode(method),
                                  method.getParameterCount(),
                                  Reflect.requestType(method),
                                  Reflect.isRequestTypeServiceMessage(method),
                                  Reflect.isSecured(method));

                          checkMethodInvokerDoesntExist(methodInfo);

                          ServiceMethodInvoker methodInvoker =
                              new ServiceMethodInvoker(
                                  method,
                                  serviceInfo.serviceInstance(),
                                  methodInfo,
                                  serviceInfo.errorMapper(),
                                  serviceInfo.dataDecoder(),
                                  serviceInfo.authenticator(),
                                  serviceInfo.principalMapper());

                          methodInvokers.put(methodInfo.qualifier(), methodInvoker);
                          methodInvokers.put(methodInfo.oldQualifier(), methodInvoker);
                        }));
  }

  private void checkMethodInvokerDoesntExist(MethodInfo methodInfo) {
    if (methodInvokers.containsKey(methodInfo.qualifier())
        || methodInvokers.containsKey(methodInfo.oldQualifier())) {
      LOGGER.error("MethodInvoker already exists, methodInfo: {}", methodInfo);
      throw new IllegalStateException("MethodInvoker already exists");
    }
  }

  @Override
  public ServiceMethodInvoker getInvoker(String qualifier) {
    return methodInvokers.get(Objects.requireNonNull(qualifier, "[getInvoker] qualifier"));
  }

  @Override
  public List listInvokers() {
    return new ArrayList<>(methodInvokers.values());
  }

  @Override
  public List listServices() {
    return new ArrayList<>(serviceInfos);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy