Search in sources :

Example 11 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class TTupleProtocolSerializer method serialize.

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
        protocol.writeMessageBegin(tm);
        writeMessage(call.getMessage(), protocol);
        protocol.writeMessageEnd();
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TMessage(org.apache.thrift.protocol.TMessage) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol)

Example 12 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class Config method run.

public void run(String... args) {
    ConfigOptions op = new ConfigOptions(tty);
    try {
        ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
        try {
            cli.parse(args);
            if (op.showHelp()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available Commands:");
                System.out.println();
                op.getCommandSet().printUsage(System.out);
                return;
            } else if (op.showVersion()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                return;
            }
            cli.validate();
            op.execute();
            return;
        } catch (ArgumentException e) {
            System.err.println("Invalid argument: " + e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            if (op.verbose()) {
                e.printStackTrace();
            }
        } catch (ParseException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (TokenizerException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serialization error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (IOException | RuntimeException e) {
            System.out.flush();
            System.err.println("IO Error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        System.out.flush();
        System.err.println("Unhandled exception: " + e.toString());
        if (op.verbose()) {
            System.err.println();
            e.printStackTrace();
        }
    }
    exit(1);
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) SerializerException(net.morimekta.providence.serializer.SerializerException) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 13 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class BinaryFormatUtils method readFieldValue.

/**
 * Read a field value from stream.
 *
 * @param in        The stream to consume.
 * @param fieldInfo The field info about the content.
 * @param fieldType The type to generate content for.
 * @param strict    If the field should be read strictly.
 * @return The field value, or null if no type.
 * @throws IOException If unable to read from stream or invalid field type.
 */
public static Object readFieldValue(BigEndianBinaryReader in, FieldInfo fieldInfo, PDescriptor fieldType, boolean strict) throws IOException {
    if (fieldType != null && forType(fieldType.getType()) != fieldInfo.type) {
        throw new SerializerException("Wrong field type for id=%d: expected %s, got %s", fieldInfo.id, asString(forType(fieldType.getType())), asString(fieldInfo.getType()));
    }
    switch(fieldInfo.type) {
        case BinaryType.VOID:
            return Boolean.TRUE;
        case BinaryType.BOOL:
            return in.expectByte() != 0;
        case BinaryType.BYTE:
            return in.expectByte();
        case BinaryType.I16:
            return in.expectShort();
        case BinaryType.I32:
            int val = in.expectInt();
            if (fieldType != null && fieldType instanceof PEnumDescriptor) {
                @SuppressWarnings("unchecked") PEnumBuilder builder = ((PEnumDescriptor<?>) fieldType).builder();
                builder.setById(val);
                return builder.build();
            } else {
                return val;
            }
        case BinaryType.I64:
            return in.expectLong();
        case BinaryType.DOUBLE:
            return in.expectDouble();
        case BinaryType.STRING:
            int len = in.expectUInt32();
            byte[] data = in.expectBytes(len);
            if (fieldType != null && fieldType.getType() == PType.STRING) {
                return new String(data, StandardCharsets.UTF_8);
            } else {
                return Binary.wrap(data);
            }
        case BinaryType.STRUCT:
            {
                if (fieldType == null) {
                    consumeMessage(in);
                    return null;
                }
                return readMessage(in, (PMessageDescriptor<?, ?>) fieldType, strict);
            }
        case BinaryType.MAP:
            {
                final byte keyT = in.expectByte();
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor keyType = null;
                PDescriptor valueType = null;
                PMap.Builder<Object, Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PMap<Object, Object> mapType = (PMap<Object, Object>) fieldType;
                    keyType = mapType.keyDescriptor();
                    valueType = mapType.itemDescriptor();
                    out = mapType.builder();
                } else {
                    out = new PMap.DefaultBuilder<>();
                }
                FieldInfo keyInfo = new FieldInfo(1, keyT);
                FieldInfo itemInfo = new FieldInfo(2, itemT);
                for (int i = 0; i < size; ++i) {
                    Object key = readFieldValue(in, keyInfo, keyType, strict);
                    Object value = readFieldValue(in, itemInfo, valueType, strict);
                    if (key != null && value != null) {
                        out.put(key, value);
                    } else if (strict) {
                        if (key == null) {
                            throw new SerializerException("Null key in map");
                        } else {
                            throw new SerializerException("Null value in map");
                        }
                    }
                }
                return out.build();
            }
        case BinaryType.SET:
            {
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor entryType = null;
                PSet.Builder<Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PSet<Object> setType = (PSet<Object>) fieldType;
                    entryType = setType.itemDescriptor();
                    out = setType.builder();
                } else {
                    out = new PSet.DefaultBuilder<>();
                }
                FieldInfo itemInfo = new FieldInfo(0, itemT);
                for (int i = 0; i < size; ++i) {
                    Object value = readFieldValue(in, itemInfo, entryType, strict);
                    if (value != null) {
                        out.add(value);
                    } else if (strict) {
                        throw new SerializerException("Null value in set");
                    }
                }
                return out.build();
            }
        case BinaryType.LIST:
            {
                final byte itemT = in.expectByte();
                final int size = in.expectUInt32();
                PDescriptor entryType = null;
                PList.Builder<Object> out;
                if (fieldType != null) {
                    @SuppressWarnings("unchecked") PList<Object> listType = (PList<Object>) fieldType;
                    entryType = listType.itemDescriptor();
                    out = listType.builder();
                } else {
                    out = new PList.DefaultBuilder<>();
                }
                FieldInfo itemInfo = new FieldInfo(0, itemT);
                for (int i = 0; i < size; ++i) {
                    Object value = readFieldValue(in, itemInfo, entryType, strict);
                    if (value != null) {
                        out.add(value);
                    } else if (strict) {
                        throw new SerializerException("Null value in list");
                    }
                }
                return out.build();
            }
        default:
            throw new SerializerException("unknown data type: " + fieldInfo.getType());
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) PEnumBuilder(net.morimekta.providence.PEnumBuilder) PMessageBuilder(net.morimekta.providence.PMessageBuilder) PMap(net.morimekta.providence.descriptor.PMap) BinaryType.asString(net.morimekta.providence.serializer.binary.BinaryType.asString) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) SerializerException(net.morimekta.providence.serializer.SerializerException) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PEnumBuilder(net.morimekta.providence.PEnumBuilder) PSet(net.morimekta.providence.descriptor.PSet) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor)

Example 14 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class BinaryFormatUtils method readMessage.

/**
 * Read message from reader.
 *
 * @param input The input reader.
 * @param descriptor The message descriptor.
 * @param strict If the message should be read in strict mode.
 * @param <Message> The message type.
 * @param <Field> The field type.
 * @return The read and parsed message.
 * @throws IOException If read failed.
 */
public static <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(BigEndianBinaryReader input, PMessageDescriptor<Message, Field> descriptor, boolean strict) throws IOException {
    PMessageBuilder<Message, Field> builder = descriptor.builder();
    if (builder instanceof BinaryReader) {
        ((BinaryReader) builder).readBinary(input, strict);
    } else {
        FieldInfo fieldInfo = readFieldInfo(input);
        while (fieldInfo != null) {
            PField field = descriptor.findFieldById(fieldInfo.getId());
            if (field != null) {
                Object value = readFieldValue(input, fieldInfo, field.getDescriptor(), strict);
                builder.set(field.getId(), value);
            } else {
                readFieldValue(input, fieldInfo, null, false);
            }
            fieldInfo = readFieldInfo(input);
        }
        if (strict) {
            try {
                builder.validate();
            } catch (IllegalStateException e) {
                throw new SerializerException(e, e.getMessage());
            }
        }
    }
    return builder.build();
}
Also used : PField(net.morimekta.providence.descriptor.PField) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) BigEndianBinaryReader(net.morimekta.util.io.BigEndianBinaryReader) SerializerException(net.morimekta.providence.serializer.SerializerException)

Example 15 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class MessageCollectors method toFile.

/**
 * write stream of messages to file.
 *
 * @param file The file to write.
 * @param serializer The serializer to use.
 * @param <Message> The message type.
 * @param <Field> The field type.
 * @return The collector.
 */
@Nonnull
public static <Message extends PMessage<Message, Field>, Field extends PField> Collector<Message, OutputStream, Integer> toFile(File file, Serializer serializer) {
    final AtomicInteger result = new AtomicInteger(0);
    return Collector.of(Suppliers.memoize(() -> {
        // Delay file creation until the write starts.
        try {
            return new BufferedOutputStream(new FileOutputStream(file));
        } catch (IOException e) {
            throw new UncheckedIOException("Unable to open " + file.getName(), e);
        }
    }), (outputStream, t) -> {
        try {
            synchronized (result) {
                result.addAndGet(serializer.serialize(outputStream, t));
                if (!serializer.binaryProtocol()) {
                    result.addAndGet(maybeWriteBytes(outputStream, MessageStreams.READABLE_ENTRY_SEP));
                }
            }
        } catch (SerializerException e) {
            throw new UncheckedIOException("Bad data", e);
        } catch (IOException e) {
            throw new UncheckedIOException("Unable to write to " + file.getName(), e);
        }
    }, (a, b) -> a, (outputStream) -> {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            throw new UncheckedIOException("Unable to close " + file.getName(), e);
        }
        return result.getAndSet(0);
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BufferedOutputStream(java.io.BufferedOutputStream) SerializerException(net.morimekta.providence.serializer.SerializerException) Nonnull(javax.annotation.Nonnull)

Aggregations

SerializerException (net.morimekta.providence.serializer.SerializerException)26 TException (org.apache.thrift.TException)8 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 TTransport (org.apache.thrift.transport.TTransport)8 IOException (java.io.IOException)7 PServiceCall (net.morimekta.providence.PServiceCall)6 File (java.io.File)5 UncheckedIOException (java.io.UncheckedIOException)5 PMessage (net.morimekta.providence.PMessage)5 PField (net.morimekta.providence.descriptor.PField)5 TMessage (org.apache.thrift.protocol.TMessage)5 Nonnull (javax.annotation.Nonnull)4 ArgumentException (net.morimekta.console.args.ArgumentException)4 PApplicationException (net.morimekta.providence.PApplicationException)4 Test (org.junit.Test)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ArgumentParser (net.morimekta.console.args.ArgumentParser)3 CountingOutputStream (net.morimekta.util.io.CountingOutputStream)3 TProtocol (org.apache.thrift.protocol.TProtocol)3 TTupleProtocol (org.apache.thrift.protocol.TTupleProtocol)3