public static void registerMarshallers(SerializationContext ctx) throws IOException, DescriptorParserException { ctx.registerProtoFiles(FileDescriptorSource.fromResources(PROTOBUF_RES)); ctx.registerMarshaller(new UserMarshaller()); ctx.registerMarshaller(new GenderMarshaller()); ctx.registerMarshaller(new AddressMarshaller()); ctx.registerMarshaller(new AccountMarshaller()); ctx.registerMarshaller(new LimitsMarshaller()); ctx.registerMarshaller(new TransactionMarshaller()); } }
protected ProtoTypeMetadata scanAnnotations(Class<?> javaType) { ProtoTypeMetadata protoTypeMetadata = metadataByClass.get(javaType); if (protoTypeMetadata != null) { // already seen return protoTypeMetadata; } if (serializationContext.canMarshall(javaType)) { // this is a known type, defined in another schema file that we'll need to import BaseMarshaller<?> m = serializationContext.getMarshaller(javaType); protoTypeMetadata = new ProtoTypeMetadata(m); imports.add(serializationContext.getDescriptorByName(m.getTypeName()).getFileDescriptor().getName()); } else if (javaType.isEnum()) { protoTypeMetadata = new ProtoEnumTypeMetadata((Class<? extends Enum>) javaType); } else { protoTypeMetadata = new ProtoMessageTypeMetadata(this, javaType); } defineType(protoTypeMetadata); return protoTypeMetadata; }
private FileDescriptor parseSchema() throws DescriptorParserException, IOException { FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString( schemaName, protoSchema ); Configuration config = Configuration.builder().build(); SerializationContext serCtx = ProtobufUtil.newSerializationContext( config ); serCtx.registerProtoFiles( fileDescriptorSource ); return serCtx.getFileDescriptors().get( schemaName ); } }
private void registerProtoFiles(OgmProtoStreamMarshaller marshaller, SchemaDefinitions sd) { try { marshaller.getSerializationContext().registerProtoFiles( sd.asFileDescriptorSource() ); } catch (DescriptorParserException | IOException e) { throw log.errorAtProtobufParsing( e ); } }
public void testProtobufFieldIndexingMetadata() throws Exception { SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("user_definition.proto", PROTO_DEFINITIONS)); ProtobufFieldIndexingMetadata userIndexedFieldProvider = new ProtobufFieldIndexingMetadata(serCtx.getMessageDescriptor("User")); ProtobufFieldIndexingMetadata addressIndexedFieldProvider = new ProtobufFieldIndexingMetadata(serCtx.getMessageDescriptor("User.Address")); // try top level attributes assertTrue(userIndexedFieldProvider.isIndexed(new String[]{"name"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"surname"})); assertTrue(addressIndexedFieldProvider.isIndexed(new String[]{"street"})); assertFalse(addressIndexedFieldProvider.isIndexed(new String[]{"postCode"})); // try nested attributes assertTrue(userIndexedFieldProvider.isIndexed(new String[]{"indexedAddresses", "street"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"indexedAddresses", "postCode"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"unindexedAddresses", "street"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"unindexedAddresses", "postCode"})); }
public static void registerSequenceMarshaller(SequenceTableDefinition std, OgmProtoStreamMarshaller marshaller) { SequenceIdMarshaller idM = new SequenceIdMarshaller( std ); marshaller.getSerializationContext().registerMarshaller( idM ); } }
@Override public Descriptor getEntityMetadata(String typeName) { return serializationContext.getMessageDescriptor(typeName); }
public InfinispanConnectionImpl(RemoteCacheManager manager, RemoteCacheManager scriptManager, String cacheName, SerializationContext ctx, InfinispanConnectionFactory icf, String cacheTemplate) throws ResourceException { this.cacheManager = manager; this.cacheName = cacheName; this.ctx = ctx; this.ctx.registerMarshallerProvider(this.marshallerProvider); this.icf = icf; try { this.defaultCache = this.cacheManager.getCache(this.cacheName); } catch (Throwable t) { throw new ResourceException(t); } this.scriptManager = scriptManager; this.cacheTemplate = cacheTemplate; }
@Override public boolean isMarshallable(Object o) { // our marshaller can handle all of these primitive/scalar types as well even if we do not // have a per-type marshaller defined in our SerializationContext return o instanceof String || o instanceof Long || o instanceof Integer || o instanceof Double || o instanceof Float || o instanceof Boolean || o instanceof byte[] || o instanceof Byte || o instanceof Short || o instanceof Character || o instanceof java.util.Date || o instanceof java.time.Instant || getSerializationContext().canMarshall(o.getClass()); }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Annotation 'IndexedField' cannot be used together with 'Field' on field test.User.age") public void testIndexedFieldAndField() throws Exception { String testProto = "package test;\n" + "/* @Indexed */ message User {\n" + " /* @IndexedField(index=false, store=false) @Field(store=Store.NO, index=Index.NO) */ " + " required int32 age = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test.proto", testProto)); }
public static void registerEntityMarshaller(MainOgmCoDec delegate, OgmProtoStreamMarshaller marshaller) throws DescriptorParserException { IdMessageMarshaller idM = new IdMessageMarshaller( delegate ); PayloadMessageMarshaller valueM = new PayloadMessageMarshaller( delegate ); marshaller.getSerializationContext().registerMarshaller( idM ); marshaller.getSerializationContext().registerMarshaller( valueM ); }
public ProtobufMatcher(SerializationContext serializationContext, IndexedFieldProvider<Descriptor> indexedFieldProvider) { super(new ProtobufPropertyHelper(serializationContext, indexedFieldProvider)); this.serializationContext = serializationContext; this.wrappedMessageDescriptor = serializationContext.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME); }
/** * Registers a marshaller from a dynamic entity. * @param kieMarshallerSupplier The {@link KieMarshallerSupplier for the entity} */ void registerMarshaller(final KieMarshallerSupplier kieMarshallerSupplier) { supplierByClass.put(kieMarshallerSupplier.getJavaClass(), kieMarshallerSupplier); getSerializationContext().registerMarshallerProvider(new SerializationContext.MarshallerProvider() { @Override public BaseMarshaller<?> getMarshaller(String typeName) { Class<?> classForType = classByType.get(typeName); if (classForType != null && classForType.equals(kieMarshallerSupplier.getJavaClass())) { return kieMarshallerSupplier.getMarshallerForType(typeName); } return null; } @Override public BaseMarshaller<?> getMarshaller(Class<?> javaClass) { if (kieMarshallerSupplier.getJavaClass().isAssignableFrom(javaClass)) { return kieMarshallerSupplier.getMarshallerForType(type.get()); } else { return null; } } }); }
/** * Registers proto files and marshallers. * * @param ctx the serialization context * @throws org.infinispan.protostream.DescriptorParserException if a proto definition file fails to parse correctly * @throws IOException if proto file registration fails */ public static void registerMarshallers(SerializationContext ctx) throws IOException { FileDescriptorSource fileDescriptorSource = new FileDescriptorSource(); fileDescriptorSource.addProtoFile(QUERY_PROTO_RES, MarshallerRegistration.class.getResourceAsStream(QUERY_PROTO_RES)); fileDescriptorSource.addProtoFile(MESSAGE_PROTO_RES, MarshallerRegistration.class.getResourceAsStream(MESSAGE_PROTO_RES)); ctx.registerProtoFiles(fileDescriptorSource); ctx.registerMarshaller(new QueryRequest.NamedParameter.Marshaller()); ctx.registerMarshaller(new QueryRequest.Marshaller()); ctx.registerMarshaller(new QueryResponse.Marshaller()); ctx.registerMarshaller(new FilterResult.Marshaller()); ctx.registerMarshaller(new ContinuousQueryResult.ResultType.Marshaller()); ctx.registerMarshaller(new ContinuousQueryResult.Marshaller()); } }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Annotation 'IndexedField' cannot be used together with 'Analyzer' on field test.User2.name") public void testIndexedFieldAndAnalyzer() throws Exception { String testProto = "package test;\n" + "/* @Indexed */" + "message User2 {\n" + " /* @IndexedField(index=false, store=false) @Analyzer(definition=\"standard\") */" + " required string name = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test1.proto", testProto)); }
/** * Registers a protobuf file * @param fileName The name of the file. * @param contents The contents of the file. * @param dynamicEntityClass The dynamic entity class. * the types in the protobuf. * @throws IOException in case the registration fails. */ void registerSchema(String fileName, String contents, Class<?> dynamicEntityClass) throws IOException { getSerializationContext().registerProtoFiles(FileDescriptorSource.fromString(fileName, contents)); getSerializationContext().getFileDescriptors().entrySet().stream() .filter(p -> p.getKey().equals(fileName)) .flatMap(fd -> fd.getValue().getMessageTypes().stream()) .map(AnnotatedDescriptorImpl::getFullName) .forEach(t -> classByType.put(t, dynamicEntityClass)); }
private void generateMarshallers() throws Exception { MarshallerCodeGenerator marshallerCodeGenerator = new MarshallerCodeGenerator(packageName, getClassPool()); for (Class<?> c : metadataByClass.keySet()) { ProtoTypeMetadata ptm = metadataByClass.get(c); if (ptm instanceof ProtoMessageTypeMetadata) { RawProtobufMarshaller<?> marshaller = marshallerCodeGenerator.generateMessageMarshaller((ProtoMessageTypeMetadata) ptm); ptm.setMarshaller(marshaller); serializationContext.registerMarshaller(marshaller); } else if (ptm instanceof ProtoEnumTypeMetadata) { EnumMarshaller<?> marshaller = marshallerCodeGenerator.generateEnumMarshaller((ProtoEnumTypeMetadata) ptm); ptm.setMarshaller(marshaller); serializationContext.registerMarshaller(marshaller); } } }
@Override public void onEnd() { if (payloadStarted) { processMissingFields(); } else { payloadStarted = true; if (payload != null) { if (entityTypeName == null) { throw new IllegalStateException("Descriptor name is missing"); } payloadMessageDescriptor = serializationContext.getMessageDescriptor(entityTypeName); messageContext = new MessageContext<>(null, null, payloadMessageDescriptor); } } }
/** * Registers a marshaller from a dynamic entity. * @param kieMarshallerSupplier The {@link KieMarshallerSupplier for the entity} */ void registerMarshaller(final KieMarshallerSupplier kieMarshallerSupplier) { supplierByClass.put(kieMarshallerSupplier.getJavaClass(), kieMarshallerSupplier); getSerializationContext().registerMarshallerProvider(new SerializationContext.MarshallerProvider() { @Override public BaseMarshaller<?> getMarshaller(String typeName) { Class<?> classForType = classByType.get(typeName); if (classForType != null && classForType.equals(kieMarshallerSupplier.getJavaClass())) { return kieMarshallerSupplier.getMarshallerForType(typeName); } return null; } @Override public BaseMarshaller<?> getMarshaller(Class<?> javaClass) { if (kieMarshallerSupplier.getJavaClass().isAssignableFrom(javaClass)) { return kieMarshallerSupplier.getMarshallerForType(type.get()); } else { return null; } } }); }
@Override public void init(SerializationContext serializationContext) throws IOException { serializationContext.registerProtoFiles(FileDescriptorSource.fromResources(ServerCtxInitializer.class.getClassLoader(), "/sample_bank_account/bank.proto")); serializationContext.registerMarshaller(new UserMarshaller()); serializationContext.registerMarshaller(new GenderMarshaller()); } }