All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.dl.client.tool.ContractsRegistration Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
package com.scalar.dl.client.tool;

import com.moandjiezana.toml.Toml;
import com.scalar.dl.client.config.ClientConfig;
import com.scalar.dl.client.exception.ClientException;
import com.scalar.dl.client.service.ClientService;
import com.scalar.dl.client.service.ClientServiceFactory;
import com.scalar.dl.ledger.service.StatusCode;
import java.io.File;
import java.io.StringReader;
import java.util.Optional;
import java.util.concurrent.Callable;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
import picocli.CommandLine;
import picocli.CommandLine.Command;

@Command(name = "register-contracts", description = "Register specified contracts.")
public class ContractsRegistration implements Callable {

  @CommandLine.Option(
      names = {"--properties", "--config"},
      required = true,
      paramLabel = "PROPERTIES_FILE",
      description = "A configuration file in properties format.")
  private String properties;

  @CommandLine.Option(
      names = {"--contracts-file"},
      required = true,
      paramLabel = "CONTRACTS_FILE",
      description = "A file including contracts to register in TOML format.")
  private String contractsFile;

  @CommandLine.Option(
      names = "--ignore-registered",
      description = "Ignore an already registered contract.")
  private boolean ignoreRegistered;

  @CommandLine.Option(
      names = {"-h", "--help"},
      usageHelp = true,
      description = "display the help message.")
  boolean helpRequested;

  public static void main(String[] args) {
    int exitCode = new CommandLine(new ContractsRegistration()).execute(args);
    System.exit(exitCode);
  }

  @Override
  public Integer call() throws Exception {
    ClientServiceFactory factory = new ClientServiceFactory(new ClientConfig(new File(properties)));

    try (ClientService service = factory.getClientService()) {
      new Toml()
          .read(new File(contractsFile))
          .getTables("contracts")
          .forEach(
              each -> {
                Optional properties =
                    each.contains("properties")
                        ? Optional.ofNullable(
                            Json.createReader(new StringReader(each.getString("properties")))
                                .readObject())
                        : Optional.empty();
                try {
                  service.registerContract(
                      each.getString("contract-id"),
                      each.getString("contract-binary-name"),
                      each.getString("contract-class-file"),
                      properties);
                } catch (ClientException e) {
                  if (!ignoreRegistered
                      || e.getStatusCode() != StatusCode.CONTRACT_ALREADY_REGISTERED) {
                    throw e;
                  }
                }
              });
      Common.printOutput(JsonValue.NULL);
      return 0;
    } catch (ClientException e) {
      Common.printError(e);
      return 1;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy