
io.scalecube.services.discovery.ServiceScanner Maven / Gradle / Ivy
package io.scalecube.services.discovery;
import io.scalecube.cluster.membership.IdGenerator;
import io.scalecube.services.Microservices;
import io.scalecube.services.Reflect;
import io.scalecube.services.ServiceEndpoint;
import io.scalecube.services.ServiceMethodDefinition;
import io.scalecube.services.ServiceRegistration;
import io.scalecube.services.annotations.ContentType;
import io.scalecube.services.annotations.Service;
import io.scalecube.services.annotations.ServiceMethod;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ServiceScanner {
public static ServiceEndpoint scan(List serviceInstances, String host, int port,
Map endpointTags) {
String endpointId = IdGenerator.generateId();
List serviceRegistrations = serviceInstances.stream()
.flatMap(inst -> Arrays.stream(inst.service().getClass().getInterfaces())
.map(serviceInterface -> new InterfaceInfo(serviceInterface, inst.tags())))
.filter(iAndTags -> iAndTags.serviceInterface.isAnnotationPresent(Service.class))
.map(iAndTags -> {
Class> serviceInterface = iAndTags.serviceInterface;
Map serviceTags = iAndTags.tags;
String namespace = Reflect.serviceName(serviceInterface);
ContentType ctAnnotation = serviceInterface.getAnnotation(ContentType.class);
String serviceContentType = ctAnnotation != null ? ctAnnotation.value() : ContentType.DEFAULT;
List actions = Arrays.stream(serviceInterface.getMethods())
.filter(m -> m.isAnnotationPresent(ServiceMethod.class))
.map(m -> {
String action = Reflect.methodName(m);
String contentType = ContentType.DEFAULT;
Map methodTags = methodTags(m);
return new ServiceMethodDefinition(action, contentType, methodTags);
}).collect(Collectors.toList());
return new ServiceRegistration(namespace,
serviceContentType,
serviceTags,
actions);
}).collect(Collectors.toList());
return new ServiceEndpoint(endpointId, host, port, endpointTags, serviceRegistrations);
}
private static class InterfaceInfo {
private final Class> serviceInterface;
private final Map tags;
private InterfaceInfo(Class> serviceInterface, Map tags) {
this.serviceInterface = serviceInterface;
this.tags = tags;
}
}
private static String merge(String lowPriority, String highPriority) {
return highPriority == null ? lowPriority : highPriority;
}
private static Map merge(Map lowPriority, Map highPriority) {
Map result = new HashMap<>();
result.putAll(lowPriority);
result.putAll(highPriority);
return result;
}
private static Map methodTags(Method m) {
// TODO: tags are not yet implemented on API level
return new HashMap<>();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy