use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class BaseGenerateSourcesMojo method executeInternal.
boolean executeInternal(IncludeExcludeFileSelector includeDirs, File outputDir, IncludeExcludeFileSelector inputSelector, String defaultInputIncludes, boolean testCompile) throws MojoExecutionException, MojoFailureException {
Set<File> inputFiles = ProvidenceInput.getInputFiles(project, inputSelector, defaultInputIncludes, print_debug, getLog());
if (inputFiles.isEmpty()) {
return false;
}
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
throw new MojoExecutionException("Unable to create target directory " + outputDir);
}
}
TreeSet<File> includes = new TreeSet<>();
File workingDir = new File(buildDir, testCompile ? "providence-test" : "providence");
File[] deleteFiles = workingDir.listFiles();
if (!workingDir.exists()) {
if (!workingDir.mkdirs()) {
throw new MojoExecutionException("Unable to create working directory " + workingDir);
}
} else if (deleteFiles != null) {
StreamSupport.<File>stream(Spliterators.spliterator(deleteFiles, Spliterator.DISTINCT | Spliterator.IMMUTABLE), false).forEach(File::delete);
}
Set<Artifact> resolvedArtifacts = new HashSet<>();
for (Dependency dep : dependencies) {
if (testCompile || !TEST.equalsIgnoreCase(dep.getScope())) {
resolveDependency(dep, includes, workingDir, resolvedArtifacts);
}
}
if (includeDirs != null) {
DirectoryScanner includeScanner = new DirectoryScanner();
includeScanner.setIncludes(includeDirs.getIncludes());
if (includeDirs.getExcludes() != null) {
includeScanner.setExcludes(includeDirs.getExcludes());
}
includeScanner.setBasedir(project.getBasedir());
includeScanner.scan();
for (String dir : includeScanner.getIncludedDirectories()) {
includes.add(new File(project.getBasedir(), dir));
}
for (String dir : includeScanner.getExcludedDirectories()) {
includes.remove(new File(project.getBasedir(), dir));
}
}
FileManager fileManager = new FileManager(outputDir);
ProgramParser parser = new ThriftProgramParser(require_field_id, require_enum_value, allow_language_reserved_names);
TypeLoader loader = new TypeLoader(includes, parser);
if (print_debug) {
inputFiles.stream().filter(Objects::nonNull).map(file -> {
try {
return file.getAbsoluteFile().getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}).sorted().forEach(f -> getLog().info("Compiling: " + f));
}
JavaOptions javaOptions = new JavaOptions();
javaOptions.android = android;
javaOptions.jackson = jackson;
javaOptions.rw_binary = rw_binary;
javaOptions.hazelcast_portable = hazelcast_portable;
javaOptions.generated_annotation_version = generated_annotation_version;
javaOptions.public_constructors = public_constructors;
GeneratorOptions generatorOptions = new GeneratorOptions();
generatorOptions.generator_program_name = "providence-maven-plugin";
generatorOptions.program_version = getVersionString();
GeneratorFactory factory = new JavaGeneratorFactory();
Generator generator = new JavaGenerator(fileManager, generatorOptions, javaOptions);
Path base = project.getBasedir().toPath().toAbsolutePath();
if (project.getParent() != null && project.getParent().getBasedir() != null) {
// Only replace with parent if parent is a parent directory of this.
Path parentBase = project.getParent().getBasedir().toPath().toAbsolutePath();
if (base.toString().startsWith(parentBase.toString())) {
base = parentBase;
}
}
for (File in : inputFiles) {
ProgramTypeRegistry registry;
try {
registry = loader.load(in);
} catch (SerializerException e) {
// ParseException is a SerializerException. And serialize exceptions can come from
// failing to make sense of constant definitions.
getLog().error(" ============ >> PROVIDENCE << ============");
getLog().error("");
Arrays.stream(e.asString().split("\r?\n")).forEach(l -> getLog().error(l));
getLog().error("");
getLog().error(" ============ << PROVIDENCE >> ============");
throw new MojoFailureException("Failed to parse thrift file: " + in.getName(), e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to read thrift file: " + in.getName(), e);
}
try {
if (skipIfMissingNamespace && registry.getProgram().getNamespaceForLanguage(factory.generatorName()) == null) {
getLog().warn("Skipping (no " + factory.generatorName() + " namespace) " + base.relativize(in.toPath()));
continue;
}
generator.generate(registry);
} catch (GeneratorException e) {
throw new MojoFailureException("Failed to generate program: " + registry.getProgram().getProgramName(), e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to write program file: " + registry.getProgram().getProgramName(), e);
}
}
try {
generator.generateGlobal(loader.getProgramRegistry(), inputFiles);
} catch (GeneratorException e) {
throw new MojoFailureException("Failed to generate global", e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to write global file", e);
}
return compileOutput;
}
use of net.morimekta.providence.serializer.SerializerException 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);
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method deserialize.
@Nonnull
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> Message deserialize(@Nonnull InputStream input, @Nonnull PMessageDescriptor<Message, Field> descriptor) throws IOException {
try {
TTransport transport = new TIOStreamTransport(input);
TProtocol protocol = protocolFactory.getProtocol(transport);
return readMessage(protocol, descriptor);
} catch (TTransportException e) {
throw new SerializerException(e, "Unable to serialize into transport protocol");
} catch (TException e) {
throw new SerializerException(e, "Transport exception in protocol");
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method readTypedValue.
private Object readTypedValue(byte tType, PDescriptor type, TProtocol protocol, boolean allowNull) throws TException, SerializerException {
if (tType != forType(type.getType())) {
throw new SerializerException("Expected type " + asString(forType(type.getType())) + " but found " + asString(tType));
}
switch(tType) {
case BinaryType.BOOL:
return protocol.readBool();
case BinaryType.BYTE:
return protocol.readByte();
case BinaryType.I16:
return protocol.readI16();
case BinaryType.I32:
if (PType.ENUM == type.getType()) {
PEnumDescriptor<?> et = (PEnumDescriptor<?>) type;
PEnumBuilder<?> eb = et.builder();
int value = protocol.readI32();
eb.setById(value);
if (!eb.valid() && !allowNull) {
throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName());
}
return eb.build();
} else {
return protocol.readI32();
}
case BinaryType.I64:
return protocol.readI64();
case BinaryType.DOUBLE:
return protocol.readDouble();
case BinaryType.STRING:
if (type == PPrimitive.BINARY) {
ByteBuffer buffer = protocol.readBinary();
return Binary.wrap(buffer.array());
}
return protocol.readString();
case BinaryType.STRUCT:
return readMessage(protocol, (PMessageDescriptor<?, ?>) type);
case BinaryType.LIST:
TList listInfo = protocol.readListBegin();
PList<Object> lDesc = (PList<Object>) type;
PDescriptor liDesc = lDesc.itemDescriptor();
PList.Builder<Object> list = lDesc.builder();
for (int i = 0; i < listInfo.size; ++i) {
list.add(readTypedValue(listInfo.elemType, liDesc, protocol, false));
}
protocol.readListEnd();
return list.build();
case BinaryType.SET:
TSet setInfo = protocol.readSetBegin();
PSet<Object> sDesc = (PSet<Object>) type;
PDescriptor siDesc = sDesc.itemDescriptor();
PSet.Builder<Object> set = sDesc.builder();
for (int i = 0; i < setInfo.size; ++i) {
set.add(readTypedValue(setInfo.elemType, siDesc, protocol, false));
}
protocol.readSetEnd();
return set.build();
case BinaryType.MAP:
TMap mapInfo = protocol.readMapBegin();
PMap<Object, Object> mDesc = (PMap<Object, Object>) type;
PDescriptor mkDesc = mDesc.keyDescriptor();
PDescriptor miDesc = mDesc.itemDescriptor();
PMap.Builder<Object, Object> map = mDesc.builder();
for (int i = 0; i < mapInfo.size; ++i) {
Object key = readTypedValue(mapInfo.keyType, mkDesc, protocol, false);
Object val = readTypedValue(mapInfo.valueType, miDesc, protocol, false);
map.put(key, val);
}
protocol.readMapEnd();
return map.build();
default:
throw new SerializerException("Unsupported protocol field type: " + tType);
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method writeMessage.
private void writeMessage(PMessage<?, ?> message, TTupleProtocol protocol) throws TException, SerializerException {
PMessageDescriptor<?, ?> descriptor = message.descriptor();
if (descriptor.getVariant() == PMessageVariant.UNION) {
if (((PUnion<?, ?>) message).unionFieldIsSet()) {
PField fld = ((PUnion<?, ?>) message).unionField();
protocol.writeI16((short) fld.getId());
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
} else {
throw new SerializerException("Unable to write " + descriptor.getQualifiedName() + " without set union field.");
}
} else {
PField[] fields = descriptor.getFields();
Arrays.sort(fields, Comparator.comparingInt(PField::getId));
int numOptionals = countOptionals(fields);
BitSet optionals = new BitSet();
if (numOptionals > 0) {
int optionalPos = 0;
for (PField fld : fields) {
if (fld.getRequirement() != PRequirement.REQUIRED) {
if (message.has(fld.getId())) {
optionals.set(optionalPos);
}
++optionalPos;
}
}
}
boolean shouldWriteOptionals = true;
int optionalPos = 0;
for (PField fld : fields) {
if (fld.getRequirement() == PRequirement.REQUIRED) {
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
} else {
// non-required field.
if (shouldWriteOptionals) {
protocol.writeBitSet(optionals, numOptionals);
shouldWriteOptionals = false;
}
if (optionals.get(optionalPos)) {
writeTypedValue(message.get(fld.getId()), fld.getDescriptor(), protocol);
}
++optionalPos;
}
}
}
}
Aggregations