use of com.google.protobuf.Descriptors.FileDescriptor in project grpc-java by grpc.
the class ProtoReflectionService method updateIndexIfNecessary.
/**
* Checks for updates to the server's mutable services and updates the index if any changes are
* detected. A change is any addition or removal in the set of file descriptors attached to the
* mutable services or a change in the service names.
*
* @return The (potentially updated) index.
*/
private ServerReflectionIndex updateIndexIfNecessary() {
synchronized (lock) {
if (serverReflectionIndex == null) {
serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), server.getMutableServices());
return serverReflectionIndex;
}
Set<FileDescriptor> serverFileDescriptors = new HashSet<FileDescriptor>();
Set<String> serverServiceNames = new HashSet<String>();
List<ServerServiceDefinition> serverMutableServices = server.getMutableServices();
for (ServerServiceDefinition mutableService : serverMutableServices) {
io.grpc.ServiceDescriptor serviceDescriptor = mutableService.getServiceDescriptor();
if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) {
String serviceName = serviceDescriptor.getName();
FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor()).getFileDescriptor();
serverFileDescriptors.add(fileDescriptor);
serverServiceNames.add(serviceName);
}
}
// Replace the index if the underlying mutable services have changed. Check both the file
// descriptors and the service names, because one file descriptor can define multiple
// services.
FileDescriptorIndex mutableServicesIndex = serverReflectionIndex.getMutableServicesIndex();
if (!mutableServicesIndex.getServiceFileDescriptors().equals(serverFileDescriptors) || !mutableServicesIndex.getServiceNames().equals(serverServiceNames)) {
serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), serverMutableServices);
}
return serverReflectionIndex;
}
}
use of com.google.protobuf.Descriptors.FileDescriptor in project tesla by linking12.
the class ServiceResolver method fromFileDescriptorSet.
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex = computeDescriptorProtoIndex(descriptorSet);
Map<String, FileDescriptor> descriptorCache = new HashMap<>();
ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
try {
result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
} catch (DescriptorValidationException e) {
logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
continue;
}
}
return new ServiceResolver(result.build());
}
use of com.google.protobuf.Descriptors.FileDescriptor in project tesla by linking12.
the class ServiceResolver method descriptorFromProto.
private static FileDescriptor descriptorFromProto(FileDescriptorProto descriptorProto, ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex, Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
String descritorName = descriptorProto.getName();
if (descriptorCache.containsKey(descritorName)) {
return descriptorCache.get(descritorName);
}
ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
for (String dependencyName : descriptorProto.getDependencyList()) {
if (!descriptorProtoIndex.containsKey(dependencyName)) {
throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
}
FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
}
FileDescriptor[] empty = new FileDescriptor[0];
return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
use of com.google.protobuf.Descriptors.FileDescriptor in project elephant-bird by twitter.
the class TestThriftToDynamicProto method testGetFileDescriptor.
@Test
public void testGetFileDescriptor() throws DescriptorValidationException {
ThriftToDynamicProto<Person> converter = new ThriftToDynamicProto<Person>(Person.class);
Person person = genPerson();
Message msg = converter.convert(person);
FileDescriptor expectedFd = msg.getDescriptorForType().getFile();
FileDescriptor actualFd = converter.getFileDescriptor();
assertEquals(expectedFd, actualFd);
}
use of com.google.protobuf.Descriptors.FileDescriptor in project atlasdb by palantir.
the class ColumnValueDescription method hydrateFromProto.
public static ColumnValueDescription hydrateFromProto(TableMetadataPersistence.ColumnValueDescription message) {
ValueType type = ValueType.hydrateFromProto(message.getType());
Compression compression = Compression.hydrateFromProto(message.getCompression());
if (!message.hasClassName()) {
return new ColumnValueDescription(type, compression);
}
Validate.isTrue(type == ValueType.BLOB);
if (message.hasFormat()) {
try {
Format format = Format.hydrateFromProto(message.getFormat());
Descriptor protoDescriptor = null;
if (message.hasProtoFileDescriptorTree()) {
FileDescriptor fileDescriptor = hydrateFileDescriptorTree(message.getProtoFileDescriptorTree());
protoDescriptor = fileDescriptor.findMessageTypeByName(message.getProtoMessageName());
} else if (message.hasProtoFileDescriptor()) {
FileDescriptorProto fileProto = FileDescriptorProto.parseFrom(message.getProtoFileDescriptor());
FileDescriptor fileDescriptor = FileDescriptor.buildFrom(fileProto, new FileDescriptor[0]);
protoDescriptor = fileDescriptor.findMessageTypeByName(message.getProtoMessageName());
}
return new ColumnValueDescription(format, message.getClassName(), message.getCanonicalClassName(), compression, protoDescriptor);
} catch (Exception e) {
log.error("Failed to parse FileDescriptorProto.", e);
}
}
/*
* All the code in the rest of this method is to support old protos that don't have a format field.
* Format and canonicalClassName were added at the same time.
*
* Once we upgrade all the old protos (after 3.6.0), we can remove the below code.
*/
Format format = Format.hydrateFromProto(message.getFormat());
Descriptor protoDescriptor = null;
if (message.hasProtoFileDescriptor()) {
try {
FileDescriptorProto fileProto = FileDescriptorProto.parseFrom(message.getProtoFileDescriptor());
FileDescriptor fileDescriptor = FileDescriptor.buildFrom(fileProto, new FileDescriptor[0]);
protoDescriptor = fileDescriptor.findMessageTypeByName(message.getProtoMessageName());
} catch (Exception e) {
log.warn("Failed to parse FileDescriptorProto.", e);
}
}
return new ColumnValueDescription(format, message.getClassName(), message.getCanonicalClassName(), compression, protoDescriptor);
}
Aggregations