Search in sources :

Example 1 with ArgumentException

use of net.morimekta.console.args.ArgumentException in project providence by morimekta.

the class TestRunner method main.

public static void main(String... args) throws InterruptedException, TException, IOException {
    System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "off");
    ArgumentParser parser = new ArgumentParser("it-serialization", "SNAPSHOT", "Serialization speed Integration test");
    TestOptions options = new TestOptions();
    try {
        parser.add(new Flag("--help", "h?", "Show this help message", options.help::set));
        parser.add(new Flag("--no_progress", "s", "No progress output", options.no_progress::set));
        parser.add(new Option("--runs", "R", "RUNS", "Number of runs to do", i32(options.runs::set), "100"));
        parser.add(new Option("--generate", "g", "NUM", "Number of messages to generate", i32(options.generate::set), "10"));
        parser.add(new Option("--fill_rate", "r", "FRACT", "Fill rate, 0.0 - 1.0 of fields in generated classes", dbl(options.generator::setFillRate), "1.0"));
        parser.add(new Option("--load", null, "FILE", "File to load data from. Required test to match content", file(options.file::set)));
        parser.add(new Option("--format", "f", "FMT", "Which format to test", oneOf(Format.class, options.format::set), "all"));
        parser.add(new Argument("test", "Which test to run", oneOf(TestOptions.Test.class, options.test::set), "all"));
        parser.parse(args);
        if (options.help.get()) {
            System.out.println("Usage: " + parser.getSingleLineUsage());
            System.out.println();
            parser.printUsage(System.out);
            System.exit(0);
        }
        parser.validate();
        if (options.test.get() == null || options.test.get() == TestOptions.Test.containers) {
            new TestRunner<>(net.morimekta.test.providence.serialization.containers.ManyContainers.kDescriptor, net.morimekta.test.thrift.serialization.containers.ManyContainers::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.fields) {
            new TestRunner<>(net.morimekta.test.providence.serialization.messages.ManyFields.kDescriptor, net.morimekta.test.thrift.serialization.messages.ManyFields::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.r_fields) {
            new TestRunner<>(net.morimekta.test.providence.serialization.messages.ManyRequiredFields.kDescriptor, net.morimekta.test.thrift.serialization.messages.ManyRequiredFields::new, options).run();
        }
        if (options.test.get() == null || options.test.get() == TestOptions.Test.deep) {
            new TestRunner<>(net.morimekta.test.providence.serialization.deep.DeepStructure.kDescriptor, net.morimekta.test.thrift.serialization.deep.DeepStructure::new, options).run();
        }
        System.out.println();
        System.exit(0);
    } catch (ArgumentException e) {
        System.err.println("Argument: " + e.getMessage());
        System.err.println();
        System.err.println("Usage: " + parser.getSingleLineUsage());
        System.err.println();
        parser.printUsage(System.err);
    } catch (TException e) {
        e.printStackTrace();
        System.err.println("TException" + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.exit(1);
}
Also used : TException(org.apache.thrift.TException) Argument(net.morimekta.console.args.Argument) ArgumentParser(net.morimekta.console.args.ArgumentParser) Flag(net.morimekta.console.args.Flag) TException(org.apache.thrift.TException) IOException(java.io.IOException) ArgumentException(net.morimekta.console.args.ArgumentException) Parser.file(net.morimekta.console.util.Parser.file) Option(net.morimekta.console.args.Option) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 2 with ArgumentException

use of net.morimekta.console.args.ArgumentException in project providence by morimekta.

the class ConvertOptions method getServiceDefinition.

public PService getServiceDefinition() throws ParseException, IOException {
    ProgramRegistry registry = getProgramRegistry();
    PService srv = registry.getService(type, null);
    if (srv == null) {
        String programName = type.substring(0, type.lastIndexOf("."));
        programName = programName.replaceAll("[-.]", "_");
        Map<String, File> includeMap = FormatUtils.getIncludeMap(getRc(), includes);
        String filePath = includeMap.get(programName).toString();
        CProgram document = registry.registryForPath(filePath).getProgram();
        Set<String> services = new TreeSet<>(document.getServices().stream().map(s -> s.getQualifiedName()).collect(Collectors.toSet()));
        throw new ArgumentException("Unknown service %s in %s.\n" + "Found %s", type, ReflectionUtils.programNameFromPath(type), services.size() == 0 ? "none" : Strings.join(", ", services));
    }
    return srv;
}
Also used : ProgramRegistry(net.morimekta.providence.reflect.util.ProgramRegistry) TreeSet(java.util.TreeSet) CProgram(net.morimekta.providence.reflect.contained.CProgram) ArgumentException(net.morimekta.console.args.ArgumentException) File(java.io.File) PService(net.morimekta.providence.descriptor.PService)

Example 3 with ArgumentException

use of net.morimekta.console.args.ArgumentException in project providence by morimekta.

the class GeneratorOptions method getGenerator.

public Generator getGenerator(TypeLoader loader) throws ArgumentException, GeneratorException, IOException {
    net.morimekta.providence.generator.GeneratorOptions generatorOptions = new net.morimekta.providence.generator.GeneratorOptions();
    generatorOptions.generator_program_name = "pvdgen";
    generatorOptions.program_version = Utils.getVersionString();
    try {
        return gen.factory.createGenerator(getFileManager(), generatorOptions, gen.options);
    } catch (GeneratorException e) {
        throw new ArgumentException(e, e.getMessage());
    }
}
Also used : ArgumentException(net.morimekta.console.args.ArgumentException) GeneratorException(net.morimekta.providence.generator.GeneratorException)

Example 4 with ArgumentException

use of net.morimekta.console.args.ArgumentException in project providence by morimekta.

the class GeneratorSpecParser method parse.

@Override
public GeneratorSpec parse(String spec) {
    Map<String, GeneratorFactory> factoryMap = generatorFactoryMap.get();
    ArrayList<String> options = new ArrayList<>();
    String[] gen = spec.split("[:]");
    if (gen.length > 2) {
        throw new ArgumentException("Invalid generator spec, only one ':' allowed: " + spec);
    }
    GeneratorFactory factory = factoryMap.get(gen[0].toLowerCase());
    if (factory == null) {
        throw new ArgumentException("Unknown output language " + gen[0]);
    }
    if (gen.length == 2) {
        Collections.addAll(options, gen[1].split("[,]"));
    }
    return new GeneratorSpec(factory, options);
}
Also used : GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) ArrayList(java.util.ArrayList) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 5 with ArgumentException

use of net.morimekta.console.args.ArgumentException in project providence by morimekta.

the class RPC method run.

@SuppressWarnings("unchecked")
void run(String... args) {
    try {
        ArgumentParser cli = options.getArgumentParser("pvdrpc", "Providence RPC Tool");
        try {
            cli.parse(args);
            if (options.showHelp()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                System.out.println("Example code to run:");
                System.out.println("$ cat call.json | pvdrpc -I thrift/ -s cal.Calculator http://localhost:8080/service");
                System.out.println("$ pvdrpc -i binary,file:my.data -f json_protocol -I thrift/ -s cal.Calculator http://localhost:8080/service");
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available formats are:");
                for (Format format : Format.values()) {
                    System.out.println(String.format(" - %-20s : %s", format.name(), format.desc));
                }
                return;
            } else if (options.showVersion()) {
                System.out.println("Providence RPC Tool - " + getVersionString());
                return;
            }
            cli.validate();
            MessageReader in = options.getInput();
            MessageWriter out = options.getOutput();
            PService service = options.getDefinition();
            PServiceCallHandler handler = options.getHandler();
            PServiceCall call = in.read(service);
            in.close();
            PServiceCall resp = handler.handleCall(call, service);
            out.write(resp);
            out.separator();
            out.close();
            if (options.out.base64mime && options.out.file == null) {
                System.out.println();
            }
            return;
        } catch (ConnectException e) {
            System.out.flush();
            System.err.format("Unable to connect to %s: %s%n", options.endpoint, e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (HttpResponseException e) {
            System.out.flush();
            System.err.println("Received " + e.getStatusCode() + " " + e.getStatusMessage());
            System.err.println(" - from: " + options.endpoint);
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (ArgumentException e) {
            System.out.flush();
            System.err.println(e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            System.err.println();
            System.err.println("Run $ pvdrpc --help # for available options.");
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serializer error: " + e.asString());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (UncheckedIOException | IOException e) {
            System.out.flush();
            System.err.println("I/O error: " + e.getMessage());
            if (options.verbose()) {
                System.out.flush();
                e.printStackTrace();
            }
        } catch (IllegalArgumentException e) {
            System.out.flush();
            System.err.println("Internal Error: " + e.getMessage());
            if (options.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (RuntimeException | IOException e) {
        System.out.flush();
        System.err.println("Unchecked exception: " + e.getMessage());
        if (options.verbose()) {
            System.out.flush();
            e.printStackTrace();
        }
    }
    System.err.flush();
    exit(1);
}
Also used : MessageReader(net.morimekta.providence.mio.MessageReader) HttpResponseException(com.google.api.client.http.HttpResponseException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) Format(net.morimekta.providence.tools.common.formats.Format) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) PServiceCall(net.morimekta.providence.PServiceCall) MessageWriter(net.morimekta.providence.mio.MessageWriter) ArgumentException(net.morimekta.console.args.ArgumentException) PService(net.morimekta.providence.descriptor.PService) ConnectException(java.net.ConnectException)

Aggregations

ArgumentException (net.morimekta.console.args.ArgumentException)17 File (java.io.File)9 IOException (java.io.IOException)9 ArgumentParser (net.morimekta.console.args.ArgumentParser)6 PService (net.morimekta.providence.descriptor.PService)5 TreeSet (java.util.TreeSet)4 TypeLoader (net.morimekta.providence.reflect.TypeLoader)4 CProgram (net.morimekta.providence.reflect.contained.CProgram)4 SerializerException (net.morimekta.providence.serializer.SerializerException)4 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)3 MessageReader (net.morimekta.providence.mio.MessageReader)3 MessageWriter (net.morimekta.providence.mio.MessageWriter)3 ParseException (net.morimekta.providence.reflect.parser.ParseException)3 Format (net.morimekta.providence.tools.common.formats.Format)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 UncheckedIOException (java.io.UncheckedIOException)2 InetSocketAddress (java.net.InetSocketAddress)2