com.google.protobuf.DescriptorProtos.FileDescriptorSet Java Examples

The following examples show how to use com.google.protobuf.DescriptorProtos.FileDescriptorSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 6 votes vote down vote up
public static FileDescriptorSet resolveService(Channel channel, String serviceName) {
    ServerReflectionClient reflectionClient = ServerReflectionClient.create(channel);
    try {
        List<String> serviceNames = reflectionClient.listServices().get();
        if (!serviceNames.contains(serviceName)) {
            throw Status.NOT_FOUND.withDescription(
                    String.format("Remote server does not have service %s. Services: %s", serviceName, serviceNames))
                    .asRuntimeException();
        }

        return reflectionClient.lookupService(serviceName).get();
    } catch (InterruptedException | ExecutionException e) {
        logger.error("Resolve services get error", e);
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 6 votes vote down vote up
public static FileDescriptorSet resolveService(Channel channel, String serviceName) {
    ServerReflectionClient reflectionClient = ServerReflectionClient.create(channel);
    try {
        List<String> serviceNames = reflectionClient.listServices().get();
        if (!serviceNames.contains(serviceName)) {
            throw Status.NOT_FOUND.withDescription(
                    String.format("Remote server does not have service %s. Services: %s", serviceName, serviceNames))
                    .asRuntimeException();
        }

        return reflectionClient.lookupService(serviceName).get();
    } catch (InterruptedException | ExecutionException e) {
        logger.error("Resolve services get error", e);
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
    logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
    fileDescriptor.getDependencyList().forEach(dep -> {
        if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
            requestedDescriptors.add(dep);
            ++outstandingRequests;
            requestStream.onNext(requestForDescriptor(dep));
        }
    });

    --outstandingRequests;
    if (outstandingRequests == 0) {
        logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
        resultFuture.set(FileDescriptorSet.newBuilder()
                .addAllFile(resolvedDescriptors.values())
                .build());
        requestStream.onCompleted();
    }
}
 
Example #4
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
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);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #5
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */
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());
}
 
Example #6
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 6 votes vote down vote up
public static List<FileDescriptorSet> resolveServices(Channel channel) {
    ServerReflectionClient serverReflectionClient = ServerReflectionClient.create(channel);
    try {
        List<String> services = serverReflectionClient.listServices().get();
        if (isEmpty(services)) {
            logger.info("Can't find services by channel {}", channel);
            return emptyList();
        }
        return services.stream().map(serviceName -> {
            ListenableFuture<FileDescriptorSet> future = serverReflectionClient.lookupService(serviceName);
            try {
                return future.get();
            } catch (InterruptedException | ExecutionException e) {
                logger.error("Get {} fileDescriptor occurs error", serviceName, e);
                return null;
            }
        }).filter(Objects::nonNull).collect(toList());
    } catch (Throwable t) {
        logger.error("Exception resolve service", t);
        throw new RuntimeException(t);
    }
}
 
Example #7
Source File: ServerReflectionClient.java    From milkman with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
  logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
  fileDescriptor.getDependencyList().forEach(dep -> {
    if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
      requestedDescriptors.add(dep);
      ++outstandingRequests;
      requestStream.onNext(requestForDescriptor(dep));
    }
  });

  --outstandingRequests;
  if (outstandingRequests == 0) {
    logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
    resultFuture.set(FileDescriptorSet.newBuilder()
        .addAllFile(resolvedDescriptors.values())
        .build());
    requestStream.onCompleted();
  }
}
 
Example #8
Source File: GrpcReflectionUtils.java    From grpc-swagger with MIT License 6 votes vote down vote up
public static List<FileDescriptorSet> resolveServices(Channel channel) {
    ServerReflectionClient serverReflectionClient = ServerReflectionClient.create(channel);
    try {
        List<String> services = serverReflectionClient.listServices().get();
        if (isEmpty(services)) {
            logger.info("Can't find services by channel {}", channel);
            return emptyList();
        }
        return services.stream().map(serviceName -> {
            ListenableFuture<FileDescriptorSet> future = serverReflectionClient.lookupService(serviceName);
            try {
                return future.get();
            } catch (InterruptedException | ExecutionException e) {
                logger.error("Get {} fileDescriptor occurs error", serviceName, e);
                return null;
            }
        }).filter(Objects::nonNull).collect(toList());
    } catch (Throwable t) {
        logger.error("Exception resolve service", t);
        throw new RuntimeException(t);
    }
}
 
Example #9
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
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);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #10
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
    logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
    fileDescriptor.getDependencyList().forEach(dep -> {
        if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
            requestedDescriptors.add(dep);
            ++outstandingRequests;
            requestStream.onNext(requestForDescriptor(dep));
        }
    });

    --outstandingRequests;
    if (outstandingRequests == 0) {
        logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
        resultFuture.set(FileDescriptorSet.newBuilder()
                .addAllFile(resolvedDescriptors.values())
                .build());
        requestStream.onCompleted();
    }
}
 
Example #11
Source File: Bootstrap.java    From krpc with Apache License 2.0 6 votes vote down vote up
void loadProtos(RpcApp app, String proto) {

        if (isEmpty(proto)) {
            log.info("no dynamic proto resource need to load");
            return;
        }

        if (!proto.endsWith("/")) proto = proto + "/";

        try {
            InputStream basein = RpcMetas.class.getResourceAsStream("descriptor.proto.pb");
            FileDescriptorSet baseSet = FileDescriptorSet.parseFrom(basein);
            basein.close();
            FileDescriptor base = FileDescriptor.buildFrom(baseSet.getFile(0), new FileDescriptor[]{});

            List<String> files = getProtoFiles(proto);
            for (String file : files) {
                loadProtoFile(app, base, proto + file);
            }
        } catch (Exception e) {
            log.error("load dynamic proto resource failed", e);
        }
    }
 
Example #12
Source File: DynamicProtoUtil.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the data portion of an ExecutionResult as ByteString.
 *
 * <p>The FileDescriptorSet must contain a message with the name "{operationName}Response". This
 * message will be populated with data from the execution result and encoded as a ByteString.
 */
public static ByteString encodeResponse(
    String operationName, FileDescriptorSet fileDescriptorSet, ExecutionResult executionResult) {
  try {
    // TODO: Support multiple FileDescriptors in FileDescriptorSet
    FileDescriptor fileDescriptor =
        FileDescriptor.buildFrom(fileDescriptorSet.getFileList().get(0), new FileDescriptor[] {});

    Descriptor messageType = fileDescriptor.findMessageTypeByName(operationName + "Response");

    Message message = DynamicMessage.parseFrom(messageType, ByteString.EMPTY);
    Message responseData = QueryResponseToProto.buildMessage(message, executionResult.getData());

    return responseData.toByteString();
  } catch (DescriptorValidationException | InvalidProtocolBufferException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: CommonProto2Java.java    From saluki with Apache License 2.0 6 votes vote down vote up
public void generateFile(String protoPath) {
  try {
    if (pojoTypes == null) {
      pojoTypes = Maps.newHashMap();
    }
  } finally {
    if (!new File(protoPath).exists()) {
      logger.warn("protoPath:" + protoPath
          + " not exist, it may be in the third party jars, so it can't be generate");
      return;
    }
    FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath);
    for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) {
      Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions());
      if (packageClassName == null) {
        continue;
      }
      ProtocolStringList dependencyList = fdp.getDependencyList();
      for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) {
        String dependencyPath = discoveryRoot + "/" + it.next();
        generateFile(dependencyPath);
      }
      doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight());
    }
  }
}
 
Example #14
Source File: Model.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a model from a normalized service config, rather than from descriptor and .yaml files.
 */
public static Model create(Service normalizedConfig) {
  FileDescriptorSet regeneratedDescriptor = DescriptorGenerator.generate(normalizedConfig);
  Model model = create(regeneratedDescriptor);

  // Configured with a stripped Service
  Service.Builder builder = normalizedConfig.toBuilder();
  ImmutableList.Builder<Api> strippedApis = ImmutableList.builder();
  for (Api api : normalizedConfig.getApisList()) {
    strippedApis.add(
        Api.newBuilder().setName(api.getName()).setVersion(api.getVersion()).build());
  }
  // NOTE: Documentation may still contain text from the original protos.
  builder.clearEnums();
  builder.clearTypes();
  builder.clearApis();
  builder.addAllApis(strippedApis.build());
  ConfigSource strippedConfig = ConfigSource.newBuilder(builder.build()).build();

  model.setConfigSources(ImmutableList.of(strippedConfig));

  return model;
}
 
Example #15
Source File: ModelBuilder.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorSet parseFileDescriptors(
    ToolOptions options, ModelBuildOverrides registry, DiagCollector diagCollector) {
  String fileDescriptor = options.get(ToolOptions.DESCRIPTOR_SET);
  if (!Strings.isNullOrEmpty(fileDescriptor)) {
    try {
      return parseFileAsDescriptorSet(FileWrapper.from(fileDescriptor), registry, diagCollector);
    } catch (IOException ex) {
      diagCollector.addDiag(
          Diag.error(
              SimpleLocation.TOPLEVEL,
              "Cannot read FileDescriptorSet file '%s': %s",
              fileDescriptor,
              ex.getMessage()));
      return null;
    }
  } else {
    return parseFileAsDescriptorSet(
        options.get(ToolOptions.DESCRIPTOR_SET_CONTENTS), registry, diagCollector);
  }
}
 
Example #16
Source File: ModelBuilder.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorSet parseFileAsDescriptorSet(
    FileWrapper inputFile, ModelBuildOverrides registry, DiagCollector diagCollector) {
  ByteString extensionFile = inputFile.getFileContents();
  try {
    return FileDescriptorSet.parseFrom(extensionFile, registry.getPlatformExtensions());
  } catch (InvalidProtocolBufferException e) {

    diagCollector.addDiag(
        Diag.error(
            SimpleLocation.TOPLEVEL,
            "Cannot read file descriptor file '%s': %s",
            inputFile.getFilename(),
            e.getMessage()));
    return null;
  }
}
 
Example #17
Source File: ResolverTest.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
@Test
public void resolvesWithErrors() {
  // Modify the descriptor injecting some errors.
  FileDescriptorSet.Builder builder = descriptors.toBuilder();
  builder
      .getFileBuilder(0)
      .getMessageTypeBuilder(0)
      .getFieldBuilder(1) // required N n
      .setTypeName("undef_N");
  builder
      .getFileBuilder(0)
      .getMessageTypeBuilder(0)
      .getFieldBuilder(2) // optional E e
      .setTypeName("undef_E");
  Model testApi = Model.create(builder.build());
  testApi.registerProcessor(new Resolver());
  Truth.assertThat(testApi.establishStage(Resolved.KEY)).isFalse();
  Truth.assertThat(testApi.getDiagReporter().getDiagCollector().getErrorCount()).isEqualTo(2);
  assertThat(testApi.getDiagReporter().getDiagCollector().getDiags().get(0).toString())
      .contains("undef_N");
  assertThat(testApi.getDiagReporter().getDiagCollector().getDiags().get(1).toString())
      .contains("undef_E");
}
 
Example #18
Source File: GrpcDocStringExtractor.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, String> getDocStringsFromFiles(Map<String, byte[]> files) {
    return files.entrySet().stream()
                .flatMap(entry -> {
                    try {
                        final FileDescriptorSet descriptors = FileDescriptorSet.parseFrom(entry.getValue());
                        return descriptors.getFileList().stream();
                    } catch (IOException e) {
                        logger.info("Could not parse file at '{}', skipping. " +
                                    "Is the file a protobuf descriptor file?",
                                    entry.getKey());
                        return Stream.empty();
                    }
                })
                .flatMap(f -> parseFile(f).entrySet().stream())
                .collect(toImmutableMap(Entry::getKey, Entry::getValue, (entry, unused) -> entry));
}
 
Example #19
Source File: AISToProtobufIT.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testAIS() throws Exception {
    FileDescriptorSet set = null;
    for (File file : files) {
        String sql = fileContents(file);
        runDDL(sql);
        AISToProtobuf ais2p = new AISToProtobuf(ProtobufRowFormat.Type.GROUP_MESSAGE, set);
        for (Group group : ais().getGroups().values()) {
            if (group.getName().getSchemaName().equals(SCHEMA)) {
                ais2p.addGroup(group);
            }
        }
        set = ais2p.build();
        StringBuilder proto = new StringBuilder();
        new ProtobufDecompiler(proto).decompile(set);
        String actual = proto.toString();
        String expected = null;
        File expectedFile = changeSuffix(file, ".proto");
        if (expectedFile.exists()) {
            expected = fileContents(expectedFile);
        }
        String fullCaseName = file.getName().replace("\\.sql$", "");
        if (expected == null) {
            fail(fullCaseName + " no expected result given. actual='" + actual + "'");
        }
        else {
            assertEqualsWithoutPattern(fullCaseName, expected, actual, UUID_REGEX);
        }
    }
}
 
Example #20
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 5 votes vote down vote up
private DynamicSchema(FileDescriptorSet fileDescSet) throws DescriptorValidationException {
	mFileDescSet = fileDescSet;
	Map<String,FileDescriptor> fileDescMap = init(fileDescSet);
	
	Set<String> msgDupes = new HashSet<String>();
	Set<String> enumDupes = new HashSet<String>();
	for (FileDescriptor fileDesc : fileDescMap.values()) {
		for (Descriptor msgType : fileDesc.getMessageTypes()) addMessageType(msgType, null, msgDupes, enumDupes);			
		for (EnumDescriptor enumType : fileDesc.getEnumTypes()) addEnumType(enumType, null, enumDupes);						
	}
	
	for (String msgName : msgDupes) mMsgDescriptorMapShort.remove(msgName);
	for (String enumName : enumDupes) mEnumDescriptorMapShort.remove(enumName);
}
 
Example #21
Source File: ToolProtoUtil.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
public static FileDescriptorSet openDescriptorSet(String fileName, ExtensionRegistry registry) {
  try {
    return FileDescriptorSet.parseFrom(new FileInputStream(fileName), registry);
  } catch (IOException e) {
    throw new RuntimeException(String.format("Cannot open+parse input file '%s'", fileName), e);
  }
}
 
Example #22
Source File: ExtensionPool.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
public Builder setFileDescriptorSet(FileDescriptorSet descriptorSet) {
  Preconditions.checkState(this.descriptor == null, "can only add one FileDescriptorSet");
  this.descriptor = descriptorSet;
  for (FileDescriptorProto fileDescriptor : descriptorSet.getFileList()) {
    add(fileDescriptor);
  }
  return this;
}
 
Example #23
Source File: ServiceResolver.java    From grpc-swagger with MIT License 5 votes vote down vote up
/**
 * Returns a map from descriptor proto name as found inside the descriptors to protos.
 */
private static ImmutableMap<String, FileDescriptorProto> computeDescriptorProtoIndex(
        FileDescriptorSet fileDescriptorSet) {
    ImmutableMap.Builder<String, FileDescriptorProto> resultBuilder = ImmutableMap.builder();
    for (FileDescriptorProto descriptorProto : fileDescriptorSet.getFileList()) {
        resultBuilder.put(descriptorProto.getName(), descriptorProto);
    }
    return resultBuilder.build();
}
 
Example #24
Source File: Model.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/** Creates an model where all protos in the descriptor are considered to be sources. */
public static Model create(FileDescriptorSet proto) {
  DiagCollector diagCollector = new BoundedDiagCollector();

  return new Model(
      proto,
      null,
      ExperimentsImpl.none(),
      ExtensionPool.EMPTY,
      diagCollector,
      new DiagSuppressor(diagCollector));
}
 
Example #25
Source File: ServerReflectionClient.java    From milkman with MIT License 5 votes vote down vote up
ListenableFuture<FileDescriptorSet> start(
    StreamObserver<ServerReflectionRequest> requestStream) {
  this.requestStream = requestStream;
  requestStream.onNext(requestForSymbol(serviceName));
  ++outstandingRequests;
  return resultFuture;
}
 
Example #26
Source File: Model.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
public static Model create(
    FileDescriptorSet proto,
    Iterable<String> sources,
    Experiments experiments,
    ExtensionPool extensionPool,
    DiagCollector diagCollector,
    DiagSuppressor diagSuppressor) {
  return new Model(proto, sources, experiments, extensionPool, diagCollector, diagSuppressor);
}
 
Example #27
Source File: Model.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new model based on the given file descriptor, list of source file names and list of
 * experiments to be enabled for the model.
 */
public static Model create(
    FileDescriptorSet proto,
    Iterable<String> sources,
    Experiments experiments,
    ExtensionPool extensionPool,
    DiagCollector diagCollector) {
  return new Model(
      proto,
      sources,
      experiments,
      extensionPool,
      diagCollector,
      new DiagSuppressor(diagCollector));
}
 
Example #28
Source File: Model.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new model based on the given file descriptor, list of source file names and list of
 * experiments to be enabled for the model.
 */
public static Model create(
    FileDescriptorSet proto,
    Iterable<String> sources,
    Experiments experiments,
    ExtensionPool extensionPool) {
  DiagCollector diagCollector = new BoundedDiagCollector();
  return new Model(
      proto,
      sources,
      experiments,
      extensionPool,
      diagCollector,
      new DiagSuppressor(diagCollector));
}
 
Example #29
Source File: Model.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new model based on the given file descriptor set and list of source file names. The
 * file descriptor set is self-contained and contains the descriptors for the source files as well
 * as for all dependencies.
 */
public static Model create(FileDescriptorSet proto, Iterable<String> sources) {
  DiagCollector diagCollector = new BoundedDiagCollector();

  return new Model(
      proto,
      sources,
      ExperimentsImpl.none(),
      ExtensionPool.EMPTY,
      diagCollector,
      new DiagSuppressor(diagCollector));
}
 
Example #30
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private FileDescriptorSet generate() {
  FileDescriptorSet.Builder setBuilder = FileDescriptorSet.newBuilder();
  for (Map.Entry<String, FileContents> entry : contentsByFile.entrySet()) {
    FileContents contents = entry.getValue();
    String fileName = entry.getKey();
    if (!contents.apis.isEmpty() || !contents.types.isEmpty() || !contents.enums.isEmpty()) {
      setBuilder.addFile(generateFile(fileName, contents));
    }
  }
  return setBuilder.build();
}