Search in sources :

Example 6 with SerializationContextImpl

use of org.infinispan.protostream.impl.SerializationContextImpl in project kogito-apps by kiegroup.

the class ProtoDomainModelProducerTest method getTestFileDescriptor.

static FileDescriptor getTestFileDescriptor() {
    String content = TestUtils.getTestFileContent();
    SerializationContext ctx = new SerializationContextImpl(Configuration.builder().build());
    ctx.registerProtoFiles(FileDescriptorSource.fromString(DOMAIN_MODEL_PROTO_NAME, content));
    return ctx.getFileDescriptors().get(DOMAIN_MODEL_PROTO_NAME);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl)

Example 7 with SerializationContextImpl

use of org.infinispan.protostream.impl.SerializationContextImpl in project kogito-apps by kiegroup.

the class ProtoIndexParserTest method createFileDescriptor.

private FileDescriptor createFileDescriptor() {
    SerializationContext ctx = new SerializationContextImpl(configureBuilder().build());
    String content = getTestFileContent();
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test", content);
    ctx.registerProtoFiles(fileDescriptorSource);
    return ctx.getFileDescriptors().get("test");
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource)

Example 8 with SerializationContextImpl

use of org.infinispan.protostream.impl.SerializationContextImpl in project protostream by infinispan.

the class ProtobufUtil method write.

private static <A> void write(ImmutableSerializationContext ctx, TagWriterImpl out, A t) throws IOException {
    if (t == null) {
        throw new IllegalArgumentException("Object to marshall cannot be null");
    }
    BaseMarshallerDelegate marshallerDelegate = ((SerializationContextImpl) ctx).getMarshallerDelegate(t);
    marshallerDelegate.marshall(out, null, t);
    out.flush();
}
Also used : SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) BaseMarshallerDelegate(org.infinispan.protostream.impl.BaseMarshallerDelegate)

Example 9 with SerializationContextImpl

use of org.infinispan.protostream.impl.SerializationContextImpl in project protostream by infinispan.

the class WrappedMessage method readContainer.

private static Object readContainer(ImmutableSerializationContext ctx, TagReader in, int tag) throws IOException {
    int containerSize = -1;
    String containerTypeName = null;
    Integer containerTypeId = null;
    byte[] containerMessage = null;
    int fieldCount = 0;
    while (tag != 0) {
        switch(tag) {
            case WRAPPED_CONTAINER_SIZE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                containerSize = in.readInt32();
                break;
            case WRAPPED_CONTAINER_TYPE_NAME << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    containerTypeName = in.readString();
                    break;
                }
            case WRAPPED_CONTAINER_TYPE_ID << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    containerTypeId = mapTypeIdIn(in.readInt32(), ctx);
                    break;
                }
            case WRAPPED_CONTAINER_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                containerMessage = in.readByteArray();
                break;
            default:
                throw new IllegalStateException("Unexpected tag : " + tag + " (Field number : " + WireType.getTagFieldNumber(tag) + ", Wire type : " + WireType.getTagWireType(tag) + ")");
        }
        if (++fieldCount == 3) {
            break;
        }
        tag = in.readTag();
    }
    if (fieldCount != 3 || containerSize < 0 || containerMessage == null || containerTypeId == null && containerTypeName == null || containerTypeId != null && containerTypeName != null) {
        throw new IOException("Invalid WrappedMessage encoding.");
    }
    if (containerTypeId != null) {
        containerTypeName = ctx.getDescriptorByTypeId(containerTypeId).getFullName();
    }
    BaseMarshallerDelegate<?> marshallerDelegate = ((SerializationContextImpl) ctx).getMarshallerDelegate(containerTypeName);
    BaseMarshaller<?> containerMarshaller = marshallerDelegate.getMarshaller();
    if (!(containerMarshaller instanceof ElementContainerAdapter)) {
        throw new IllegalStateException("The unmarshaller is not a container adapter : " + containerMarshaller.getJavaClass().getName());
    }
    TagReaderImpl nestedInput = TagReaderImpl.newNestedInstance((ProtobufTagMarshaller.ReadContext) in, containerMessage);
    // pass the size to the marshaller of the container object
    nestedInput.setParam(CONTAINER_SIZE_CONTEXT_PARAM, containerSize);
    Object container = marshallerDelegate.unmarshall(nestedInput, null);
    if (container == null) {
        throw new IllegalStateException("The unmarshalled container must not be null");
    }
    containerMessage = null;
    nestedInput = null;
    if (containerMarshaller instanceof IterableElementContainerAdapter) {
        IterableElementContainerAdapter adapter = (IterableElementContainerAdapter) containerMarshaller;
        for (int i = 0; i < containerSize; i++) {
            Object e = readMessage(ctx, in, true);
            adapter.appendElement(container, e);
        }
    } else if (containerMarshaller instanceof IndexedElementContainerAdapter) {
        IndexedElementContainerAdapter adapter = (IndexedElementContainerAdapter) containerMarshaller;
        for (int i = 0; i < containerSize; i++) {
            Object e = readMessage(ctx, in, true);
            adapter.setElement(container, i, e);
        }
    } else {
        throw new IllegalStateException("Unknown container adapter kind : " + containerMarshaller.getJavaClass().getName());
    }
    return container;
}
Also used : IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) TagReaderImpl(org.infinispan.protostream.impl.TagReaderImpl) IOException(java.io.IOException) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) ElementContainerAdapter(org.infinispan.protostream.containers.ElementContainerAdapter)

Example 10 with SerializationContextImpl

use of org.infinispan.protostream.impl.SerializationContextImpl in project kogito-runtimes by kiegroup.

the class ProtostreamProtobufAdapterTypeProvider method build.

// we transform protostream to protobuf descriptors
private List<com.google.protobuf.Descriptors.FileDescriptor> build() throws IOException, DescriptorValidationException {
    SerializationContextImpl context = buildSerializationContext();
    List<com.google.protobuf.Descriptors.FileDescriptor> protos = new ArrayList<>();
    com.google.protobuf.Descriptors.FileDescriptor[] dependencies;
    // make sure kogito-types is processed first or else will be missing as dependency for the application types
    List<FileDescriptor> descriptorsSorted = sortFds(context.getFileDescriptors().values());
    for (FileDescriptor entry : descriptorsSorted) {
        dependencies = protos.toArray(new com.google.protobuf.Descriptors.FileDescriptor[protos.size()]);
        protos.add(Descriptors.FileDescriptor.buildFrom(buildEnumTypes(entry), dependencies));
        dependencies = protos.toArray(new com.google.protobuf.Descriptors.FileDescriptor[protos.size()]);
        protos.add(Descriptors.FileDescriptor.buildFrom(buildMessageTypes(entry), dependencies));
    }
    return protos;
}
Also used : SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) ArrayList(java.util.ArrayList) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Aggregations

SerializationContextImpl (org.infinispan.protostream.impl.SerializationContextImpl)11 SerializationContext (org.infinispan.protostream.SerializationContext)5 IOException (java.io.IOException)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 ArrayList (java.util.ArrayList)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 PUBLIC (com.github.javaparser.ast.Modifier.Keyword.PUBLIC)1 NodeList (com.github.javaparser.ast.NodeList)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 EQUALS (com.github.javaparser.ast.expr.BinaryExpr.Operator.EQUALS)1 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1