mirror of https://github.com/grpc/grpc-java.git
core: add getService to MethodDescriptor
This commit is contained in:
parent
0c304b1863
commit
ce6358e57f
|
|
@ -41,6 +41,7 @@ public final class MethodDescriptor<ReqT, RespT> {
|
||||||
|
|
||||||
private final MethodType type;
|
private final MethodType type;
|
||||||
private final String fullMethodName;
|
private final String fullMethodName;
|
||||||
|
@Nullable private final String serviceName;
|
||||||
private final Marshaller<ReqT> requestMarshaller;
|
private final Marshaller<ReqT> requestMarshaller;
|
||||||
private final Marshaller<RespT> responseMarshaller;
|
private final Marshaller<RespT> responseMarshaller;
|
||||||
private final @Nullable Object schemaDescriptor;
|
private final @Nullable Object schemaDescriptor;
|
||||||
|
|
@ -226,6 +227,7 @@ public final class MethodDescriptor<ReqT, RespT> {
|
||||||
|
|
||||||
this.type = Preconditions.checkNotNull(type, "type");
|
this.type = Preconditions.checkNotNull(type, "type");
|
||||||
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
|
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
|
||||||
|
this.serviceName = extractFullServiceName(fullMethodName);
|
||||||
this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller");
|
this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller");
|
||||||
this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller");
|
this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller");
|
||||||
this.schemaDescriptor = schemaDescriptor;
|
this.schemaDescriptor = schemaDescriptor;
|
||||||
|
|
@ -254,6 +256,17 @@ public final class MethodDescriptor<ReqT, RespT> {
|
||||||
return fullMethodName;
|
return fullMethodName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A convenience method for {@code extractFullServiceName(getFullMethodName())}.
|
||||||
|
*
|
||||||
|
* @since 1.21.0
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635")
|
||||||
|
public String getServiceName() {
|
||||||
|
return serviceName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a response payload from the given {@link InputStream}.
|
* Parse a response payload from the given {@link InputStream}.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ public final class ServerServiceDefinition {
|
||||||
public <ReqT, RespT> Builder addMethod(ServerMethodDefinition<ReqT, RespT> def) {
|
public <ReqT, RespT> Builder addMethod(ServerMethodDefinition<ReqT, RespT> def) {
|
||||||
MethodDescriptor<ReqT, RespT> method = def.getMethodDescriptor();
|
MethodDescriptor<ReqT, RespT> method = def.getMethodDescriptor();
|
||||||
checkArgument(
|
checkArgument(
|
||||||
serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())),
|
serviceName.equals(method.getServiceName()),
|
||||||
"Method name should be prefixed with service name and separated with '/'. "
|
"Method name should be prefixed with service name and separated with '/'. "
|
||||||
+ "Expected service name: '%s'. Actual fully qualifed method name: '%s'.",
|
+ "Expected service name: '%s'. Actual fully qualifed method name: '%s'.",
|
||||||
serviceName, method.getFullMethodName());
|
serviceName, method.getFullMethodName());
|
||||||
|
|
|
||||||
|
|
@ -110,8 +110,7 @@ public final class ServiceDescriptor {
|
||||||
Set<String> allNames = new HashSet<>(methods.size());
|
Set<String> allNames = new HashSet<>(methods.size());
|
||||||
for (MethodDescriptor<?, ?> method : methods) {
|
for (MethodDescriptor<?, ?> method : methods) {
|
||||||
checkNotNull(method, "method");
|
checkNotNull(method, "method");
|
||||||
String methodServiceName =
|
String methodServiceName = method.getServiceName();
|
||||||
MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
|
||||||
checkArgument(serviceName.equals(methodServiceName),
|
checkArgument(serviceName.equals(methodServiceName),
|
||||||
"service names %s != %s", methodServiceName, serviceName);
|
"service names %s != %s", methodServiceName, serviceName);
|
||||||
checkArgument(allNames.add(method.getFullMethodName()),
|
checkArgument(allNames.add(method.getFullMethodName()),
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,12 @@ import static junit.framework.TestCase.assertSame;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import io.grpc.MethodDescriptor.Marshaller;
|
||||||
import io.grpc.MethodDescriptor.MethodType;
|
import io.grpc.MethodDescriptor.MethodType;
|
||||||
|
import io.grpc.testing.TestMethodDescriptors;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
@ -135,6 +138,28 @@ public class MethodDescriptorTest {
|
||||||
assertTrue(md4.isSampledToLocalTracing());
|
assertTrue(md4.isSampledToLocalTracing());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getServiceName_extractsService() {
|
||||||
|
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
|
||||||
|
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
|
||||||
|
.setType(MethodType.UNARY)
|
||||||
|
.setFullMethodName("foo/bar")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals("foo", md.getServiceName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getServiceName_returnsNull() {
|
||||||
|
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
|
||||||
|
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
|
||||||
|
.setType(MethodType.UNARY)
|
||||||
|
.setFullMethodName("foo-bar")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertNull(md.getServiceName());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toBuilderTest() {
|
public void toBuilderTest() {
|
||||||
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
|
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ public final class ClientAuthInterceptor implements ClientInterceptor {
|
||||||
// Always use HTTPS, by definition.
|
// Always use HTTPS, by definition.
|
||||||
final String scheme = "https";
|
final String scheme = "https";
|
||||||
final int defaultPort = 443;
|
final int defaultPort = 443;
|
||||||
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
String path = "/" + method.getServiceName();
|
||||||
URI uri;
|
URI uri;
|
||||||
try {
|
try {
|
||||||
uri = new URI(scheme, authority, path, null, null);
|
uri = new URI(scheme, authority, path, null, null);
|
||||||
|
|
|
||||||
|
|
@ -159,7 +159,7 @@ final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials2 {
|
||||||
// Always use HTTPS, by definition.
|
// Always use HTTPS, by definition.
|
||||||
final String scheme = "https";
|
final String scheme = "https";
|
||||||
final int defaultPort = 443;
|
final int defaultPort = 443;
|
||||||
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
String path = "/" + method.getServiceName();
|
||||||
URI uri;
|
URI uri;
|
||||||
try {
|
try {
|
||||||
uri = new URI(scheme, authority, path, null, null);
|
uri = new URI(scheme, authority, path, null, null);
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,7 @@ final class ServiceConfigInterceptor implements ClientInterceptor {
|
||||||
info = mcsc.getServiceMethodMap().get(method.getFullMethodName());
|
info = mcsc.getServiceMethodMap().get(method.getFullMethodName());
|
||||||
}
|
}
|
||||||
if (info == null && mcsc != null) {
|
if (info == null && mcsc != null) {
|
||||||
String serviceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
String serviceName = method.getServiceName();
|
||||||
info = mcsc.getServiceMap().get(serviceName);
|
info = mcsc.getServiceMap().get(serviceName);
|
||||||
}
|
}
|
||||||
return info;
|
return info;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue