/** * 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)); }
public static FileDescriptorSource fromString(String name, String protoSource) { return new FileDescriptorSource().addProtoFile(name, protoSource); }
public static FileDescriptorSource fromResources(ClassLoader userClassLoader, String... classPathResources) throws IOException { return new FileDescriptorSource().addProtoFiles(userClassLoader, classPathResources); }
public FileDescriptorSource addProtoFiles(ClassLoader userClassLoader, String... classpathResources) throws IOException { for (String classpathResource : classpathResources) { if (classpathResource == null) { throw new IllegalArgumentException("classpathResource cannot be null"); } // enforce absolute resource path String absPath = classpathResource.startsWith("/") ? classpathResource : "/" + classpathResource; InputStream resourceAsStream = getResourceAsStream(userClassLoader, absPath); if (resourceAsStream == null) { throw new IOException("Resource not found in class path : " + classpathResource); } // discard the leading slash String path = classpathResource.startsWith("/") ? classpathResource.substring(1) : classpathResource; addProtoFile(path, resourceAsStream); } return this; }
public static SerializationContext newSerializationContext(Configuration configuration) { SerializationContextImpl serializationContext = new SerializationContextImpl(configuration); try { serializationContext.registerProtoFiles(FileDescriptorSource.fromResources(WRAPPING_DEFINITIONS_RES)); } catch (IOException | DescriptorParserException e) { throw new RuntimeException("Failed to initialize serialization context", e); } serializationContext.registerMarshaller(new WrappedMessage.Marshaller()); return serializationContext; }
public FileDescriptorSource addProtoFiles(String... classpathResources) throws IOException { return addProtoFiles(null, classpathResources); }
@Override public void registerProtoFiles(FileDescriptorSource source) throws DescriptorParserException { if (log.isDebugEnabled()) { log.debugf("Registering proto files : %s", source.getFileDescriptors().keySet()); } Map<String, FileDescriptor> fileDescriptorMap = parser.parse(source); writeLock.lock(); try { // unregister all types from the files that are being overwritten for (String fileName : fileDescriptorMap.keySet()) { FileDescriptor oldFileDescriptor = fileDescriptors.get(fileName); if (oldFileDescriptor != null) { unregisterFileDescriptorTypes(oldFileDescriptor); } } fileDescriptors.putAll(fileDescriptorMap); // resolve imports and types for all files ResolutionContext resolutionContext = new ResolutionContext(source.getProgressCallback(), fileDescriptors, genericDescriptors, typeIds, enumValueDescriptors); resolutionContext.resolve(); } finally { writeLock.unlock(); } }
@Override public Map<String, FileDescriptor> parse(FileDescriptorSource fileDescriptorSource) throws DescriptorParserException { Map<String, char[]> input = fileDescriptorSource.getFileDescriptors(); Map<String, FileDescriptor> fileDescriptorMap = new LinkedHashMap<>(input.size()); for (Map.Entry<String, char[]> entry : input.entrySet()) { String fileName = entry.getKey(); try { ProtoFile protoFile = ProtoParser.parse(fileName, new CharArrayReader(entry.getValue())); checkUniqueFileOptions(protoFile); FileDescriptor fileDescriptor = PROTOFILE_MAPPER.map(protoFile); fileDescriptor.setConfiguration(configuration); fileDescriptorMap.put(fileName, fileDescriptor); } catch (DescriptorParserException e) { reportParsingError(fileDescriptorSource, fileDescriptorMap, fileName, e); } catch (IOException | RuntimeException e) { reportParsingError(fileDescriptorSource, fileDescriptorMap, fileName, new DescriptorParserException(e)); } } return fileDescriptorMap; }
@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()); } }
/** * 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)); }
public FileDescriptorSource asFileDescriptorSource() throws IOException { FileDescriptorSource source = new FileDescriptorSource(); StringReader stringReader = new StringReader( generateProtoschema() ); source.addProtoFile( "ogm-generated", stringReader ); return source; }
public static FileDescriptorSource fromResources(String... classPathResources) throws IOException { return new FileDescriptorSource().addProtoFiles(classPathResources); }
public CustomProtoStreamMarshaller() throws IOException { SerializationContext serCtx = getSerializationContext(); serCtx.registerProtoFiles(FileDescriptorSource.fromResources(CustomProtoStreamMarshaller.class.getClassLoader(), "/sample_bank_account/bank.proto")); serCtx.registerMarshaller(new UserMarshaller()); serCtx.registerMarshaller(new GenderMarshaller()); } }
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 ); } }
/** * 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()); } }
public InfinispanSessionLegacyConverter (String cacheName) throws Exception { //legacy serialization _legacyManager = new RemoteCacheManager(); _legacyCache = _legacyManager.getCache(cacheName); //new protobuf based String host = System.getProperty("host", "127.0.0.1"); _verbose = Boolean.getBoolean("verbose"); Properties properties = new Properties(); ConfigurationBuilder clientBuilder = new ConfigurationBuilder(); clientBuilder.withProperties(properties).addServer().host(host).marshaller(new ProtoStreamMarshaller()); _protoManager = new RemoteCacheManager(clientBuilder.build()); FileDescriptorSource fds = new FileDescriptorSource(); fds.addProtoFiles("/session.proto"); SerializationContext serCtx = ProtoStreamMarshaller.getSerializationContext(_protoManager); serCtx.registerProtoFiles(fds); serCtx.registerMarshaller(new SessionDataMarshaller()); _protoCache = _protoManager.getCache(cacheName); }
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()); } }
@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)); }
String file = schemas.getProperty(schema); try (FileInputStream in = new FileInputStream(file)) { ctx.registerProtoFiles(new FileDescriptorSource().addProtoFile(schema, in));
@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)); }