Search in sources :

Example 1 with FileDescriptorSource

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

the class ProtostreamProducerTest method kogitoTypesDescriptor.

@Test
void kogitoTypesDescriptor() {
    try {
        FileDescriptorSource fileDescriptorSource = protostreamProducer.kogitoTypesDescriptor();
        assertTrue(fileDescriptorSource.getFileDescriptors().containsKey(KOGITO_INDEX_PROTO));
        assertTrue(fileDescriptorSource.getFileDescriptors().containsKey(KOGITO_TYPES_PROTO));
    } catch (IOException e) {
        fail("Failed with IOException", e);
    }
}
Also used : IOException(java.io.IOException) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.jupiter.api.Test)

Example 2 with FileDescriptorSource

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

the class ProtoIndexParserTest method testConfigureBuilderWithInvalidFile.

@Test
void testConfigureBuilderWithInvalidFile() {
    SerializationContext ctx = new SerializationContextImpl(configureBuilder().build());
    FileDescriptorSource invalidFileDescriptorSource = FileDescriptorSource.fromString("invalid", "invalid");
    try {
        ctx.registerProtoFiles(invalidFileDescriptorSource);
        fail("Failed to process invalid proto file");
    } catch (DescriptorParserException ex) {
    // Successfully throw exception
    }
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.jupiter.api.Test)

Example 3 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project infinispan by infinispan.

the class ProtobufMetadataManagerInterceptor method visitPutMapCommand.

@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) {
    if (!ctx.isOriginLocal()) {
        return invokeNext(ctx, command);
    }
    final Map<Object, Object> map = command.getMap();
    FileDescriptorSource source = new FileDescriptorSource();
    for (Object key : map.keySet()) {
        final Object value = map.get(key);
        if (!(key instanceof String)) {
            throw log.keyMustBeString(key.getClass());
        }
        if (!(value instanceof String)) {
            throw log.valueMustBeString(value.getClass());
        }
        if (shouldIntercept(key)) {
            if (!((String) key).endsWith(PROTO_KEY_SUFFIX)) {
                throw log.keyMustBeStringEndingWithProto(key);
            }
            source.addProtoFile((String) key, (String) value);
        }
    }
    // lock global errors key
    VisitableCommand cmd = commandsFactory.buildLockControlCommand(ERRORS_KEY_SUFFIX, command.getFlagsBitSet(), null);
    InvocationStage stage = invoker.running().invokeStage(ctx, cmd);
    return makeStage(asyncInvokeNext(ctx, command, stage)).thenApply(ctx, command, (rCtx, rCommand, rv) -> {
        long flagsBitSet = copyFlags(rCommand);
        ProgressCallback progressCallback = null;
        if (rCtx.isOriginLocal()) {
            progressCallback = new ProgressCallback();
            source.withProgressCallback(progressCallback);
        } else {
            source.withProgressCallback(EMPTY_CALLBACK);
        }
        registerFileDescriptorSource(source, source.getFiles().keySet().toString());
        if (progressCallback != null) {
            List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
            return updateSchemaErrorsIterator(rCtx, flagsBitSet, errorUpdates.iterator());
        }
        return InvocationStage.completedNullStage();
    });
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) InvocationStage(org.infinispan.interceptors.InvocationStage) SyncInvocationStage(org.infinispan.interceptors.SyncInvocationStage) KeyValuePair(org.infinispan.util.KeyValuePair) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource)

Example 4 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class ProtoSchemaBuilder method main.

public static void main(String[] args) throws Exception {
    CommandLine cmd = parseCommandLine(args);
    if (cmd == null) {
        return;
    }
    String packageName = cmd.getOptionValue(PACKAGE_LONG_OPT);
    Configuration config = Configuration.builder().build();
    SerializationContext ctx = ProtobufUtil.newSerializationContext(config);
    Properties schemas = cmd.getOptionProperties(SCHEMA_LONG_OPT);
    if (schemas != null) {
        for (String schema : schemas.stringPropertyNames()) {
            String file = schemas.getProperty(schema);
            try (FileInputStream in = new FileInputStream(file)) {
                ctx.registerProtoFiles(new FileDescriptorSource().addProtoFile(schema, in));
            }
        }
    }
    String[] marshallers = cmd.getOptionValues(MARSHALLER_LONG_OPT);
    if (marshallers != null) {
        for (String marshallerClass : marshallers) {
            BaseMarshaller<?> bm = (BaseMarshaller<?>) Class.forName(marshallerClass).newInstance();
            ctx.registerMarshaller(bm);
        }
    }
    File file = cmd.hasOption(FILE_LONG_OPT) ? new File(cmd.getOptionValue(FILE_LONG_OPT)) : null;
    String fileName = file == null ? DEFAULT_GENERATED_SCHEMA_NAME : file.getName();
    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder().fileName(fileName).packageName(packageName);
    for (String className : cmd.getArgs()) {
        protoSchemaBuilder.addClass(Class.forName(className));
    }
    String schemaFile = protoSchemaBuilder.build(ctx);
    if (file != null) {
        try (PrintStream out = new PrintStream(new FileOutputStream(file))) {
            out.print(schemaFile);
            out.flush();
        }
    } else {
        System.out.print(schemaFile);
    }
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) PrintStream(java.io.PrintStream) Configuration(org.infinispan.protostream.config.Configuration) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) FileOutputStream(java.io.FileOutputStream) BaseMarshaller(org.infinispan.protostream.BaseMarshaller) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) File(java.io.File)

Example 5 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testNotImportedInSamePackage.

@Test
public void testNotImportedInSamePackage() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("Failed to resolve type of field \"test.M2.b\". Type not found : M1");
    String file1 = "package test;\n" + "message M1 {\n" + "  required string a = 1;\n" + "}";
    String file2 = "package test;\n" + "message M2 {\n" + "  required M1 b = 2;\n" + "}";
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
    fileDescriptorSource.addProtoFile("file1.proto", file1);
    fileDescriptorSource.addProtoFile("file2.proto", file2);
    parseAndResolve(fileDescriptorSource);
}
Also used : FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Aggregations

FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)74 Test (org.junit.Test)61 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)24 SerializationContext (org.infinispan.protostream.SerializationContext)23 Configuration (org.infinispan.protostream.config.Configuration)13 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)9 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)8 HashMap (java.util.HashMap)7 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)7 Descriptor (org.infinispan.protostream.descriptors.Descriptor)7 List (java.util.List)6 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)5 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)5 TagReader (org.infinispan.protostream.TagReader)4 SerializationContextImpl (org.infinispan.protostream.impl.SerializationContextImpl)4 Map (java.util.Map)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3