com.google.protobuf.TextFormat Java Examples

The following examples show how to use com.google.protobuf.TextFormat. 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: TezUtilsInternal.java    From tez with Apache License 2.0 6 votes vote down vote up
/**
 * Convert DAGPlan to text. Skip sensitive informations like credentials.
 *
 * @param dagPlan
 * @return a string representation of the dag plan with sensitive information removed
 */
public static String convertDagPlanToString(DAGProtos.DAGPlan dagPlan) throws IOException {
  StringBuilder sb = new StringBuilder();
  for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : dagPlan.getAllFields().entrySet()) {
    if (entry.getKey().getNumber() != DAGProtos.DAGPlan.CREDENTIALS_BINARY_FIELD_NUMBER) {
      TextFormat.printField(entry.getKey(), entry.getValue(), sb);
    } else {
      Credentials credentials =
          DagTypeConverters.convertByteStringToCredentials(dagPlan.getCredentialsBinary());
      TextFormat.printField(entry.getKey(),
          ByteString.copyFrom(TezCommonUtils.getCredentialsInfo(credentials,"dag").getBytes(
              Charset.forName("UTF-8"))), sb);
    }
  }
  return sb.toString();
}
 
Example #2
Source File: Journal.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * @return the current state of the given segment, or null if the
 * segment does not exist.
 */
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
    throws IOException {
  EditLogFile elf = fjm.getLogFile(segmentTxId);
  if (elf == null) {
    return null;
  }
  if (elf.isInProgress()) {
    elf.scanLog();
  }
  if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
    LOG.info("Edit log file " + elf + " appears to be empty. " +
        "Moving it aside...");
    elf.moveAsideEmptyFile();
    return null;
  }
  SegmentStateProto ret = SegmentStateProto.newBuilder()
      .setStartTxId(segmentTxId)
      .setEndTxId(elf.getLastTxId())
      .setIsInProgress(elf.isInProgress())
      .build();
  LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
      TextFormat.shortDebugString(ret));
  return ret;
}
 
Example #3
Source File: StreamClientImpl.java    From onos with Apache License 2.0 6 votes vote down vote up
private void handlePacketIn(P4RuntimeOuterClass.PacketIn packetInMsg) {
    if (log.isTraceEnabled()) {
        log.trace("Received packet-in from {}: {}", deviceId, packetInMsg);
    }
    if (!pipeconfService.getPipeconf(deviceId).isPresent()) {
        log.warn("Unable to handle packet-in from {}, missing pipeconf: {}",
                 deviceId, TextFormat.shortDebugString(packetInMsg));
        return;
    }
    // Decode packet message and post event.
    // TODO: consider implementing a cache to speed up
    //  encoding/deconding of packet-in/out (e.g. LLDP, ARP)
    final PiPipeconf pipeconf = pipeconfService.getPipeconf(deviceId).get();
    final PiPacketOperation pktOperation;
    try {
        pktOperation = CODECS.packetIn().decode(
                packetInMsg, null, pipeconf);
    } catch (CodecException e) {
        log.warn("Unable to process packet-int: {}", e.getMessage());
        return;
    }
    controller.postEvent(new P4RuntimeEvent(
            P4RuntimeEvent.Type.PACKET_IN,
            new PacketInEvent(deviceId, pktOperation)));
}
 
Example #4
Source File: ProtoOutputFormatterCallback.java    From bazel with Apache License 2.0 6 votes vote down vote up
private void writeData(Message message) throws IOException {
  switch (outputType) {
    case BINARY:
      message.writeTo(outputStream);
      break;
    case TEXT:
      TextFormat.print(message, printStream);
      break;
    case JSON:
      jsonPrinter.appendTo(message, printStream);
      printStream.append('\n');
      break;
    default:
      throw new IllegalStateException("Unknown outputType " + outputType.formatName());
  }
}
 
Example #5
Source File: MultiServiceEventClient.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Maps the provided status to the service that owns its task, then queries that service with the status.
 *
 * <p>This is an optimization which avoids querying services about task statuses that don't relate to them.
 * <p>In addition to reducing unnecessary queries, this also improves isolation between services. They only see
 * task statuses which relate to them.
 */
@Override
public TaskStatusResponse taskStatus(Protos.TaskStatus status) {
  return multiServiceManager
      .getMatchingService(status)
      .map(x -> x.taskStatus(status))
      .orElseGet(() -> multiServiceManager
          .getServiceSanitized(frameworkName)
          .map(x -> {
            LOGGER.info("Forwarding task status to default service: {}", frameworkName);
            return x.taskStatus(status);
          })
          .orElseGet(() -> {
            // Unrecognized service. Status for old task ?
            LOGGER.info("Received status for unknown task {}: {}",
                status.getTaskId().getValue(), TextFormat.shortDebugString(status));
            return TaskStatusResponse.unknownTask();
          })
      );
}
 
Example #6
Source File: ManifestEditorTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
/** Tests the whole process of editing manifest to catch any unintended changes. */
@Test
public void complexManifest_featureSplit() throws Exception {
  XmlNode.Builder xmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNodeBuilder);
  AndroidManifest androidManifest = AndroidManifest.create(xmlNodeBuilder.build());

  XmlNode.Builder expectedXmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(
      TestData.openReader("testdata/manifest/feature_split_manifest1.textpb"),
      expectedXmlNodeBuilder);

  XmlNode generatedManifest =
      androidManifest
          .toEditor()
          .setSplitIdForFeatureSplit("testModule")
          .save()
          .getManifestRoot()
          .getProto();
  assertThat(generatedManifest).isEqualTo(expectedXmlNodeBuilder.build());
}
 
Example #7
Source File: Journal.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * @return the current state of the given segment, or null if the
 * segment does not exist.
 */
@VisibleForTesting
SegmentStateProto getSegmentInfo(long segmentTxId)
    throws IOException {
  EditLogFile elf = fjm.getLogFile(segmentTxId);
  if (elf == null) {
    return null;
  }
  if (elf.isInProgress()) {
    elf.scanLog();
  }
  if (elf.getLastTxId() == HdfsConstants.INVALID_TXID) {
    LOG.info("Edit log file " + elf + " appears to be empty. " +
        "Moving it aside...");
    elf.moveAsideEmptyFile();
    return null;
  }
  SegmentStateProto ret = SegmentStateProto.newBuilder()
      .setStartTxId(segmentTxId)
      .setEndTxId(elf.getLastTxId())
      .setIsInProgress(elf.isInProgress())
      .build();
  LOG.info("getSegmentInfo(" + segmentTxId + "): " + elf + " -> " +
      TextFormat.shortDebugString(ret));
  return ret;
}
 
Example #8
Source File: TFTrainer.java    From OpenLabeler with Apache License 2.0 6 votes vote down vote up
private Pipeline.TrainEvalPipelineConfig parse(Path path) {
    try {
        String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
        Pipeline.TrainEvalPipelineConfig.Builder builder = Pipeline.TrainEvalPipelineConfig.newBuilder();
        TextFormat.Parser parser = TextFormat.Parser.newBuilder().build();

        // Skip unknown fields
        Field f = parser.getClass().getDeclaredField("allowUnknownFields");
        f.setAccessible(true);
        f.set(parser, true);

        parser.merge(text, builder);
        return builder.build();
    }
    catch (Exception ex) {
        LOG.log(Level.SEVERE, "Unable to parse pipeline", ex);
    }
    return null;
}
 
Example #9
Source File: EditLogLedgerMetadata.java    From big-c with Apache License 2.0 6 votes vote down vote up
void write(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NodeExistsException {
  this.zkPath = path;

  EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
  builder.setDataLayoutVersion(dataLayoutVersion)
    .setLedgerId(ledgerId).setFirstTxId(firstTxId);

  if (!inprogress) {
    builder.setLastTxId(lastTxId);
  }
  try {
    zkc.create(path, TextFormat.printToString(builder.build()).getBytes(UTF_8),
               Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  } catch (KeeperException.NodeExistsException nee) {
    throw nee;
  } catch (KeeperException e) {
    throw new IOException("Error creating ledger znode", e);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted creating ledger znode", ie);
  }
}
 
Example #10
Source File: CassandraState.java    From dcos-cassandra-service with Apache License 2.0 5 votes vote down vote up
private Optional<Protos.TaskInfo> getTemplate(CassandraDaemonTask daemon) {
    String templateTaskName = CassandraTemplateTask.toTemplateTaskName(daemon.getName());
    try {
        Optional<Protos.TaskInfo> info = getStateStore().fetchTask(templateTaskName);
        LOGGER.info("Fetched template task for daemon '{}': {}",
                daemon.getName(), TextFormat.shortDebugString(info.get()));
        return info;
    } catch (Exception e) {
        LOGGER.warn(String.format(
                "Failed to retrieve template task '%s'", templateTaskName), e);
        return Optional.empty();
    }
}
 
Example #11
Source File: LinkBuildVariablesTestCase.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Creates a CcToolchainFeatures from features described in the given toolchain fragment. */
public static CcToolchainFeatures buildFeatures(RuleContext ruleContext, String... toolchain)
    throws Exception {
  CToolchain.Builder toolchainBuilder = CToolchain.newBuilder();
  TextFormat.merge(Joiner.on("").join(toolchain), toolchainBuilder);
  return new CcToolchainFeatures(
      CcToolchainConfigInfo.fromToolchain(toolchainBuilder.buildPartial()),
      /* ccToolchainPath= */ PathFragment.EMPTY_FRAGMENT);
}
 
Example #12
Source File: FileUtils.java    From startup-os with Apache License 2.0 5 votes vote down vote up
/** Reads a prototxt file into a proto from zip archive. */
public Message readPrototxtFromZip(
    String zipFilePath, String pathInsideZip, Message.Builder builder) throws IOException {
  ZipFile zipFile =
      new ZipFile(expandHomeDirectory(joinPaths(getCurrentWorkingDirectory(), zipFilePath)));
  String content = streamToString(zipFile.getInputStream(zipFile.getEntry(pathInsideZip)));
  TextFormat.merge(content, builder);
  return builder.build();
}
 
Example #13
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
public Service.Request transform(ByteString serializedMessage) throws
    InvalidProtocolBufferException {
  // This should already be an aliased CodedInputStream from the WireMessage parsing.
  Message msg = parser.parseFrom(serializedMessage.newCodedInput());
  if (LOG.isTraceEnabled()) {
    LOG.trace("Deserialized request '{}'", TextFormat.shortDebugString(msg));
  }
  return impl.deserialize(msg);
}
 
Example #14
Source File: TFTrainer.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
public void save() throws IOException {
    if (config == null) {
        return;
    }
    config = update(config);
    String pipelineConfig = TextFormat.printToString(config);
    Files.write(getModelConfigPath(baseModelDir), pipelineConfig.getBytes());
    LOG.info("Created "+ getModelConfigPath(baseModelDir));
}
 
Example #15
Source File: ClassMemberRetargetConfig.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Memoized
MethodInvocations invocationReplacementConfigProto() {
  try {
    String protoText = Resources.toString(invocationReplacementConfigUrl(), UTF_8);
    return TextFormat.parse(
        protoText, ExtensionRegistry.getEmptyRegistry(), MethodInvocations.class);
  } catch (IOException e) {
    throw new IOError(e);
  }
}
 
Example #16
Source File: P4InfoParser.java    From onos with Apache License 2.0 5 votes vote down vote up
private static P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException {
    InputStream p4InfoStream = p4InfoUrl.openStream();
    P4Info.Builder p4InfoBuilder = P4Info.newBuilder();
    TextFormat.getParser().merge(new InputStreamReader(p4InfoStream),
                                 ExtensionRegistry.getEmptyRegistry(),
                                 p4InfoBuilder);
    return p4InfoBuilder.build();
}
 
Example #17
Source File: FrameworkScheduler.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void statusUpdate(SchedulerDriver driver, Protos.TaskStatus status) {
  try {
    LOGGER.info("Received status update for taskId={} state={} message={} protobuf={}",
        status.getTaskId().getValue(),
        status.getState().toString(),
        status.getMessage(),
        TextFormat.shortDebugString(status));
    Metrics.record(status);
    TaskStatusResponse response = mesosEventClient.taskStatus(status);
    boolean eligibleToKill = TaskKiller.update(status);
    switch (response.result) { // SUPPRESS CHECKSTYLE MissingSwitchDefaultCheck
      case UNKNOWN_TASK:
        if (eligibleToKill) {
          LOGGER.info("Received status update for unknown task, marking task to be killed: {}",
              status.getTaskId().getValue());
          TaskKiller.killTask(status.getTaskId());
        } else {
          // Special case: Mesos can send TASK_LOST+REASON_RECONCILIATION as a response to a
          // prior kill request against a task that is unknown to Mesos. When this happens, we
          // don't want to repeat the kill, because that would create a Kill -> Status -> Kill
          // -> ... loop
          LOGGER.warn(
              "Received status update for unknown task, but task should not be killed again: {}",
              status.getTaskId().getValue());
        }
        break;
      case PROCESSED:
        // No-op
        break;
    }
  } catch (Throwable e) {
    logExceptionAndExit(e);
  }
}
 
Example #18
Source File: QuorumCall.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static <K> String mapToString(
    Map<K, ? extends Message> map) {
  StringBuilder sb = new StringBuilder();
  boolean first = true;
  for (Map.Entry<K, ? extends Message> e : map.entrySet()) {
    if (!first) {
      sb.append("\n");
    }
    first = false;
    sb.append(e.getKey()).append(": ")
      .append(TextFormat.shortDebugString(e.getValue()));
  }
  return sb.toString();
}
 
Example #19
Source File: ProtobufHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
		Assert.state(contentType != null, "No content type");
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
		message.writeTo(codedOutputStream);
		codedOutputStream.flush();
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
		outputMessage.getBody().flush();
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
		outputMessage.getBody().flush();
	}
}
 
Example #20
Source File: ProtobufHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
	}
	else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
		JSON_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
		XML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
		HTML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody());
	}
}
 
Example #21
Source File: GetContainerStatusesRequestPBImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
Example #22
Source File: ClientToAMTokenIdentifierForTest.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getNewProto());
}
 
Example #23
Source File: NMTokenIdentifierNewForTest.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(this.proto);
}
 
Example #24
Source File: NMTokenIdentifierNewForTest.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(this.proto);
}
 
Example #25
Source File: AddToClusterNodeLabelsResponsePBImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
Example #26
Source File: GetClusterNodeLabelsRequestPBImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
Example #27
Source File: ContainerFinishDataPBImpl.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
Example #28
Source File: AMRMTokenIdentifier.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return TextFormat.shortDebugString(getProto());
}
 
Example #29
Source File: SchedulerDriverFactory.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Creates and returns a new {@link SchedulerDriver} with the provided credential secret.
 *
 * @param scheduler        The Framework {@link Scheduler} implementation which should receive callbacks
 *                         from the {@link SchedulerDriver}
 * @param frameworkInfo    The {@link FrameworkInfo} which describes the framework implementation.
 *                         The 'principal' field MUST be populated and non-empty
 * @param masterUrl        The URL of the currently active Mesos Master, of the form "zk://host/mesos"
 * @param credentialSecret The secret to be included in the framework
 *                         {@link org.apache.mesos.Protos.Credential}, ignored if {@code null}/empty
 * @return A {@link SchedulerDriver} configured with the provided info
 * @throws IllegalArgumentException if {@link FrameworkInfo}.principal is unset or empty when
 *                                  authentication is needed
 */
public SchedulerDriver create(
    final Scheduler scheduler,
    final FrameworkInfo frameworkInfo,
    final String masterUrl,
    final SchedulerConfig schedulerConfig,
    final byte[] credentialSecret)
{
  Credential credential;
  if (credentialSecret != null && credentialSecret.length > 0) {
    // User has manually provided a Secret. Provide a Credential with Principal + Secret.
    // (note: we intentionally avoid logging the content of the credential secret, just in case)
    LOGGER.info("Creating secret authenticated MesosSchedulerDriver for "
            + "scheduler[{}], frameworkInfo[{}], masterUrl[{}], credentialSecret[{} bytes]",
        scheduler.getClass().getSimpleName(),
        TextFormat.shortDebugString(frameworkInfo),
        masterUrl,
        credentialSecret.length);
    credential = Credential.newBuilder()
        .setPrincipal(getPrincipal(frameworkInfo, "secret"))
        .setSecretBytes(ByteString.copyFrom(credentialSecret))
        .build();
  } else if (schedulerConfig.isSideChannelActive()) {
    // Sidechannel auth is enabled. Provide a Credential with only the Principal set.
    LOGGER.info("Creating sidechannel authenticated MesosSchedulerDriver for "
            // SUPPRESS CHECKSTYLE MultipleStringLiteralsCheck
            + "scheduler[{}], frameworkInfo[{}], masterUrl[{}]",
        scheduler.getClass().getSimpleName(),
        TextFormat.shortDebugString(frameworkInfo),
        masterUrl);
    credential = Credential.newBuilder()
        .setPrincipal(getPrincipal(frameworkInfo, "sidechannel"))
        .build();
  } else {
    // No auth. Provide no credential.
    LOGGER.info("Creating unauthenticated MesosSchedulerDriver for "
            + "scheduler[{}], frameworkInfo[{}], masterUrl[{}]",
        scheduler.getClass().getSimpleName(),
        TextFormat.shortDebugString(frameworkInfo),
        masterUrl);
    credential = null;
  }
  return createInternal(
      scheduler,
      frameworkInfo,
      masterUrl,
      credential,
      schedulerConfig.getMesosApiVersion());
}
 
Example #30
Source File: OfferResources.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
  return String.format("offer[%s]%nresources%s",
      TextFormat.shortDebugString(offer),
      resources.stream().map(r -> TextFormat.shortDebugString(r)).collect(Collectors.toList()));
}