Search in sources :

Example 1 with FileDescriptor

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;
    }
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition) ProtoFileDescriptorSupplier(io.grpc.protobuf.ProtoFileDescriptorSupplier) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet)

Example 2 with FileDescriptor

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());
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor)

Example 3 with FileDescriptor

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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto)

Example 4 with FileDescriptor

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);
}
Also used : Message(com.google.protobuf.Message) Person(com.twitter.elephantbird.thrift.test.Person) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 5 with FileDescriptor

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);
}
Also used : JsonFormat(com.googlecode.protobuf.format.JsonFormat) Descriptor(com.google.protobuf.Descriptors.Descriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ParseException(com.googlecode.protobuf.format.JsonFormat.ParseException) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)13 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)7 DescriptorValidationException (com.google.protobuf.Descriptors.DescriptorValidationException)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)2 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)2 ServerServiceDefinition (io.grpc.ServerServiceDefinition)2 ProtoFileDescriptorSupplier (io.grpc.protobuf.ProtoFileDescriptorSupplier)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 FileDescriptorSet (com.google.protobuf.DescriptorProtos.FileDescriptorSet)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 JsonFormat (com.googlecode.protobuf.format.JsonFormat)1 ParseException (com.googlecode.protobuf.format.JsonFormat.ParseException)1 FileDescriptorTreeProto (com.palantir.atlasdb.protos.generated.TableMetadataPersistence.FileDescriptorTreeProto)1 Person (com.twitter.elephantbird.thrift.test.Person)1 InternalServer (io.grpc.InternalServer)1