@Override public ConsumerFactory get() { if(typeDictionary == null) { // Thread safety: no need for volatile / synchronization - it doesn't hurt much if the TypeDictionary is created multiple times. ReflectionTypeDictionaryFactory dictionaryFactory = new ReflectionTypeDictionaryFactory(injector); typeDictionary = dictionaryFactory.createFromClasspath(); } return new ConsumerFactory(serviceProperties, typeDictionary, tracer, metricBuilderFactory); } }
public TypeDictionary createFromClasspath() { logger.info("Creating TypeDictionary using reflection from standard classpath."); return new TypeDictionary(populateHandlersFromClasspath(), populateParsersFromClasspath()); }
private <T extends com.google.protobuf.Message> ConsumerFactory consumerFactoryWithHandler(ServiceProperties serviceProperties, Class<T> messageType, MessageHandler<T> handler) { TypeDictionary typeDictionary = new TypeDictionary(); ReflectionTypeDictionaryFactory reflectionCruft = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(reflectionCruft.populateParsersFromClasspath()); typeDictionary.putHandler(MessageType.of(messageType), handler); ConsumerFactory consumerFactory = new ConsumerFactory(serviceProperties, typeDictionary, null, null); return consumerFactory; }
private <T extends com.google.protobuf.Message> ConsumerFactory consumerFactoryWithHandler(ServiceProperties serviceProperties, Class<T> messageType, MessageHandler<T> handler) { TypeDictionary typeDictionary = new TypeDictionary(); ReflectionTypeDictionaryFactory reflectionCruft = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(reflectionCruft.populateParsersFromClasspath()); typeDictionary.putHandler(MessageType.of(messageType), handler); ConsumerFactory consumerFactory = new ConsumerFactory(serviceProperties, typeDictionary, null, null); return consumerFactory; } }
ReflectionTypeDictionaryFactory typeDictionaryFactory = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(typeDictionaryFactory.populateParsersFromClasspath());
@Test public void testCreateFromClasspath() { // The ReflectionTypeDictionaryFactory is robust by design, so it does not throw any Exception. // Need to check the log output for the error test cases. // Dependency injection magic ServiceProperties serviceProperites = new ServiceProperties(); Module[] modules = new Module[1]; modules[0] = new TestInjectionModule(serviceProperites); Injector injector = Guice.createInjector(modules); ReflectionTypeDictionaryFactory rtdf = new ReflectionTypeDictionaryFactory(injector); TypeDictionary dictionary = rtdf.createFromClasspath(); MessageHandler<? extends com.google.protobuf.Message> handler = dictionary.messageHandlerFor(MessageType.of(TypeDictionaryTest.class)); assertNotNull(handler); MessageHandler<? extends com.google.protobuf.Message> unknownHandler = dictionary.messageHandlerFor(MessageType.of(TestMessageWithNoHandler.class)); assertNull(unknownHandler); Parser parser = dictionary.parserFor((MessageType.of(TypeDictionaryTest.class))); assertNotNull(parser); assertNull(dictionary.parserFor(new MessageType("UnknownType"))); }