Repository: incubator-streams Updated Branches: refs/heads/master 9b89c0898 -> d0b5a0a6b
refresh streams-config support detection of StreamsConfiguration support detection of any component configuration test using base ComponentConfiguration Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/13024d00 Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/13024d00 Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/13024d00 Branch: refs/heads/master Commit: 13024d00309b9576ff5cb6fe49beccbd33062228 Parents: bfa9466 Author: sblackmon <[email protected]> Authored: Tue Nov 11 17:44:59 2014 -0600 Committer: sblackmon <[email protected]> Committed: Tue Nov 11 17:44:59 2014 -0600 ---------------------------------------------------------------------- streams-config/pom.xml | 2 +- .../streams/config/ComponentConfigurator.java | 47 +++++++++++++++++++ .../streams/config/StreamsConfigurator.java | 28 ++++++++++++ .../main/jsonschema/ComponentConfiguration.json | 23 ++++++++++ .../main/jsonschema/StreamsConfiguration.json | 5 +- .../config/test/ComponentConfiguratorTest.java | 37 +++++++++++++++ .../config/test/StreamsConfiguratorTest.java | 48 ++++++++++++++++++++ .../src/test/resources/componentTest.conf | 11 +++++ 8 files changed, 198 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/pom.xml ---------------------------------------------------------------------- diff --git a/streams-config/pom.xml b/streams-config/pom.xml index aa8d59b..63a6857 100644 --- a/streams-config/pom.xml +++ b/streams-config/pom.xml @@ -104,7 +104,7 @@ <addCompileSourceRoot>true</addCompileSourceRoot> <generateBuilders>true</generateBuilders> <sourcePaths> - <sourcePath>src/main/jsonschema/StreamsConfiguration.json</sourcePath> + <sourcePath>src/main/jsonschema</sourcePath> </sourcePaths> <outputDirectory>target/generated-sources/jsonschema2pojo</outputDirectory> <targetPackage>org.apache.streams.config</targetPackage> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java ---------------------------------------------------------------------- diff --git a/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java b/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java new file mode 100644 index 0000000..0056a4a --- /dev/null +++ b/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java @@ -0,0 +1,47 @@ +package org.apache.streams.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.reflect.TypeToken; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigRenderOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; + +/** + * Created by sblackmon on 11/11/14. + */ +public class ComponentConfigurator<T extends Serializable> { + + private Class<T> configClass; + public ComponentConfigurator(Class<T> configClass) { + this.configClass = configClass; + } + + private final static Logger LOGGER = LoggerFactory.getLogger(ComponentConfigurator.class); + + private final static ObjectMapper mapper = new ObjectMapper(); + + public T detectConfiguration(Config typesafeConfig) { + + T pojoConfig = null; + + try { + pojoConfig = mapper.readValue(typesafeConfig.root().render(ConfigRenderOptions.concise()), configClass); + } catch (Exception e) { + e.printStackTrace(); + LOGGER.warn("Could not parse:", typesafeConfig); + } + + return pojoConfig; + } + + public T detectConfiguration(String subConfig) { + return detectConfiguration( StreamsConfigurator.config.getString(subConfig)); + } + + public T detectConfiguration(Config typesafeConfig, String subConfig) { + return detectConfiguration( typesafeConfig.getString(subConfig)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java ---------------------------------------------------------------------- diff --git a/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java b/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java index e35dd5d..34e3844 100644 --- a/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java +++ b/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java @@ -18,8 +18,15 @@ package org.apache.streams.config; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.reflect.TypeToken; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigRenderOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; /** * Created with IntelliJ IDEA. @@ -30,9 +37,30 @@ import com.typesafe.config.ConfigFactory; */ public class StreamsConfigurator { + private final static Logger LOGGER = LoggerFactory.getLogger(ComponentConfigurator.class); + + private final static ObjectMapper mapper = new ObjectMapper(); + /* Pull all configuration files from the classpath, system properties, and environment variables */ public static Config config = ConfigFactory.load(); + public static StreamsConfiguration detectConfiguration() { + return detectConfiguration(config); + } + + public static StreamsConfiguration detectConfiguration(Config typesafeConfig) { + + StreamsConfiguration pojoConfig = null; + + try { + pojoConfig = mapper.readValue(typesafeConfig.root().render(ConfigRenderOptions.concise()), StreamsConfiguration.class); + } catch (Exception e) { + e.printStackTrace(); + LOGGER.warn("Could not parse:", typesafeConfig); + } + + return pojoConfig; + } } http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/main/jsonschema/ComponentConfiguration.json ---------------------------------------------------------------------- diff --git a/streams-config/src/main/jsonschema/ComponentConfiguration.json b/streams-config/src/main/jsonschema/ComponentConfiguration.json new file mode 100644 index 0000000..dcb9cde --- /dev/null +++ b/streams-config/src/main/jsonschema/ComponentConfiguration.json @@ -0,0 +1,23 @@ +{ + "type": "object", + "$schema": "http://json-schema.org/draft-03/schema", + "id": "#", + "javaType" : "org.apache.streams.config.ComponentConfiguration", + "javaInterfaces": ["java.io.Serializable"], + "properties": { + "inClasses": { + "type" : "array", + "items" : { + "type" : "string" + }, + "default": ["java.lang.String"] + }, + "outClasses": { + "type" : "array", + "items" : { + "type" : "string" + }, + "default": ["java.lang.String"] + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/main/jsonschema/StreamsConfiguration.json ---------------------------------------------------------------------- diff --git a/streams-config/src/main/jsonschema/StreamsConfiguration.json b/streams-config/src/main/jsonschema/StreamsConfiguration.json index 9a23130..fabf378 100644 --- a/streams-config/src/main/jsonschema/StreamsConfiguration.json +++ b/streams-config/src/main/jsonschema/StreamsConfiguration.json @@ -2,11 +2,12 @@ "type": "object", "$schema": "http://json-schema.org/draft-03/schema", "id": "#", - "javaType" : "org.apache.streams.StreamsConfiguration", + "javaType" : "org.apache.streams.config.StreamsConfiguration", "javaInterfaces": ["java.io.Serializable"], "properties": { "pipeline": { - "type" : "string" + "type" : "string", + "default": "No-name Stream" } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java ---------------------------------------------------------------------- diff --git a/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java new file mode 100644 index 0000000..e0acd6a --- /dev/null +++ b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java @@ -0,0 +1,37 @@ +package org.apache.streams.config.test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import junit.framework.Assert; +import org.apache.streams.config.ComponentConfiguration; +import org.apache.streams.config.ComponentConfigurator; +import org.apache.streams.config.StreamsConfigurator; +import org.junit.Test; + +/** + * Created by sblackmon on 10/20/14. + */ +public class ComponentConfiguratorTest { + + private final static ObjectMapper mapper = new ObjectMapper(); + + @Test + public void testDetectConfiguration() throws Exception { + + Config config = ConfigFactory.load("componentTest"); + + ComponentConfigurator<ComponentConfiguration> configurator = new ComponentConfigurator<>(ComponentConfiguration.class); + + ComponentConfiguration defaultPojo = configurator.detectConfiguration(config.getConfig("defaultComponent")); + + assert(defaultPojo != null); + + ComponentConfiguration configuredPojo = configurator.detectConfiguration(config.getConfig("configuredComponent")); + + assert(configuredPojo != null); + + Assert.assertEquals(configuredPojo,defaultPojo); + + } +} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/test/java/org/apache/streams/config/test/StreamsConfiguratorTest.java ---------------------------------------------------------------------- diff --git a/streams-config/src/test/java/org/apache/streams/config/test/StreamsConfiguratorTest.java b/streams-config/src/test/java/org/apache/streams/config/test/StreamsConfiguratorTest.java new file mode 100644 index 0000000..6f610c3 --- /dev/null +++ b/streams-config/src/test/java/org/apache/streams/config/test/StreamsConfiguratorTest.java @@ -0,0 +1,48 @@ +package org.apache.streams.config.test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import org.apache.streams.config.ComponentConfiguration; +import org.apache.streams.config.StreamsConfiguration; +import org.apache.streams.config.StreamsConfigurator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Serializable; +import java.util.Map; +import java.util.Scanner; + +/** + * Created by sblackmon on 10/20/14. + */ +public class StreamsConfiguratorTest { + + private final static ObjectMapper mapper = new ObjectMapper(); + + @Test + public void testDetectConfiguration() throws Exception { + + Config config = ConfigFactory.load(); + + Config detected = StreamsConfigurator.config; + + StreamsConfiguration defaultPojo = StreamsConfigurator.detectConfiguration(); + + assert(defaultPojo != null); + + StreamsConfiguration configuredPojo = StreamsConfigurator.detectConfiguration(StreamsConfigurator.config); + + assert(configuredPojo != null); + + junit.framework.Assert.assertEquals(configuredPojo, defaultPojo); + + } +} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/13024d00/streams-config/src/test/resources/componentTest.conf ---------------------------------------------------------------------- diff --git a/streams-config/src/test/resources/componentTest.conf b/streams-config/src/test/resources/componentTest.conf new file mode 100644 index 0000000..52859b3 --- /dev/null +++ b/streams-config/src/test/resources/componentTest.conf @@ -0,0 +1,11 @@ +defaultComponent { + +} +configuredComponent { + inClasses = [ + java.lang.String + ] + outClasses = [ + java.lang.String + ] +} \ No newline at end of file
