You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streams.apache.org by sb...@apache.org on 2018/02/05 21:15:35 UTC

[streams] branch STREAMS-580 updated: decided it makes more sense to use class ancestry

This is an automated email from the ASF dual-hosted git repository.

sblackmon pushed a commit to branch STREAMS-580
in repository https://gitbox.apache.org/repos/asf/streams.git


The following commit(s) were added to refs/heads/STREAMS-580 by this push:
     new fcdd041  decided it makes more sense to use class ancestry
fcdd041 is described below

commit fcdd041a26e8cf9103e440da58ad41bf5c170d49
Author: Steve Blackmon @steveblackmon <sb...@apache.org>
AuthorDate: Mon Feb 5 15:15:27 2018 -0600

    decided it makes more sense to use class ancestry
    
    so now configuration classes will fall back to include properties associated with their ancestor classes, preferring closer ancestors
---
 .../streams/config/ComponentConfigurator.java      | 40 ++++++++++++++++------
 .../main/jsonschema/ComponentConfiguration.json    |  6 ++--
 .../ComponentConfigurationForTestingNumberOne.java |  7 ++++
 .../ComponentConfigurationForTestingNumberTwo.java |  7 ++++
 .../config/test/ComponentConfiguratorTest.java     |  8 ++---
 streams-config/src/test/resources/ancestry.conf    | 21 ++++++++++++
 streams-config/src/test/resources/multipath.conf   | 18 ----------
 7 files changed, 71 insertions(+), 36 deletions(-)

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
index 18f8519..e60f087 100644
--- a/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java
+++ b/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java
@@ -72,21 +72,19 @@ public class ComponentConfigurator<T extends Serializable> {
     Config rootConfig = StreamsConfigurator.getConfig();
 
     Config cascadeConfig = null;
-    String[] canonicalNameParts = StringUtils.split(configClass.getCanonicalName(), '.');
 
-    for( int partIndex = 1; partIndex <= canonicalNameParts.length; partIndex++) {
-      String[] partialPathParts = ArrayUtils.subarray(canonicalNameParts, 0, partIndex);
-      String partialPath = StringUtils.join(partialPathParts, '.');
+    List<Class> superclasses = getSuperClasses(configClass);
 
-      if( rootConfig.hasPath(partialPath) ) {
-        Config partialPathConfig = rootConfig.getConfig(partialPath);
-        if( cascadeConfig == null ) {
-          cascadeConfig = partialPathConfig;
+    for( Class superclass : superclasses) {
+      String superclassCanonicalName = superclass.getCanonicalName();
+      if( rootConfig.hasPath(superclassCanonicalName)) {
+        Config superclassConfig = rootConfig.getConfig(superclassCanonicalName);
+        if (cascadeConfig == null) {
+          cascadeConfig = superclassConfig;
         } else {
-          cascadeConfig = partialPathConfig.withFallback(cascadeConfig);
+          cascadeConfig = superclassConfig.withFallback(cascadeConfig);
         }
       }
-
     }
 
     if( rootConfig.hasPath(configClass.getSimpleName()) ) {
@@ -98,6 +96,15 @@ public class ComponentConfigurator<T extends Serializable> {
       }
     }
 
+    if( rootConfig.hasPath(configClass.getCanonicalName()) ) {
+      Config canonicalNameConfig = rootConfig.getConfig(configClass.getCanonicalName());
+      if( cascadeConfig == null ) {
+        cascadeConfig = canonicalNameConfig;
+      } else {
+        cascadeConfig = canonicalNameConfig.withFallback(cascadeConfig);
+      }
+    }
+
     try {
       pojoConfig = mapper.readValue(cascadeConfig.root().render(ConfigRenderOptions.concise()), configClass);
     } catch (Exception ex) {
@@ -146,4 +153,17 @@ public class ComponentConfigurator<T extends Serializable> {
   public T detectConfiguration(Config typesafeConfig, String subConfig) {
     return detectConfiguration( typesafeConfig.getConfig(subConfig));
   }
+
+  /*
+   * return class hierarchy in order from furthest to nearest ancestor
+   */
+  public static List<Class> getSuperClasses(Class clazz) {
+    List<Class> classList = new ArrayList<Class>();
+    Class superclass = clazz.getSuperclass();
+    while (superclass != null && !superclass.isInterface() && superclass != java.lang.Object.class) {
+      classList.add(0, superclass);
+      superclass = superclass.getSuperclass();
+    }
+    return classList;
+  }
 }
diff --git a/streams-config/src/main/jsonschema/ComponentConfiguration.json b/streams-config/src/main/jsonschema/ComponentConfiguration.json
index 4e015fe..9ed9add 100644
--- a/streams-config/src/main/jsonschema/ComponentConfiguration.json
+++ b/streams-config/src/main/jsonschema/ComponentConfiguration.json
@@ -12,15 +12,13 @@
             "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
diff --git a/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberOne.java b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberOne.java
new file mode 100644
index 0000000..fa073d7
--- /dev/null
+++ b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberOne.java
@@ -0,0 +1,7 @@
+package org.apache.streams.config.test;
+
+import org.apache.streams.config.ComponentConfiguration;
+
+public class ComponentConfigurationForTestingNumberOne extends ComponentConfiguration {
+
+}
diff --git a/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberTwo.java b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberTwo.java
new file mode 100644
index 0000000..ba98eee
--- /dev/null
+++ b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfigurationForTestingNumberTwo.java
@@ -0,0 +1,7 @@
+package org.apache.streams.config.test;
+
+import org.apache.streams.config.ComponentConfiguration;
+
+public class ComponentConfigurationForTestingNumberTwo extends ComponentConfigurationForTestingNumberOne {
+
+}
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
index c1c984e..1903652 100644
--- 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
@@ -40,7 +40,7 @@ import static org.hamcrest.core.IsEqual.equalTo;
 
 /**
  * Test for
- * @see org.apache.streams.config.ComponentConfigurator
+ * @see ComponentConfigurator
  */
 @RunWith(PowerMockRunner.class)
 @PrepareForTest(StreamsConfigurator.class)
@@ -195,13 +195,13 @@ public class ComponentConfiguratorTest {
   }
 
   @Test
-  public void testDetectConfigurationMultipath() throws Exception {
+  public void testDetectConfigurationAncestory() throws Exception {
 
-    Config testConfig = ConfigFactory.parseResourcesAnySyntax("multipath.conf");
+    Config testConfig = ConfigFactory.parseResourcesAnySyntax("ancestry.conf");
 
     StreamsConfigurator.setConfig(testConfig);
 
-    ComponentConfigurator<ComponentConfiguration> configurator = new ComponentConfigurator<>(ComponentConfiguration.class);
+    ComponentConfigurator<ComponentConfigurationForTestingNumberTwo> configurator = new ComponentConfigurator(ComponentConfigurationForTestingNumberTwo.class);
 
     ComponentConfiguration configuredPojo = configurator.detectConfiguration();
 
diff --git a/streams-config/src/test/resources/ancestry.conf b/streams-config/src/test/resources/ancestry.conf
new file mode 100644
index 0000000..d95176a
--- /dev/null
+++ b/streams-config/src/test/resources/ancestry.conf
@@ -0,0 +1,21 @@
+org.apache.streams.config.ComponentConfiguration = {
+  inClasses = [
+    "java.lang.Object"
+  ]
+  outClasses = [
+    "java.lang.Object"
+  ]
+}
+org.apache.streams.config.test.ComponentConfigurationForTestingNumberOne = {
+  inClasses = [
+    "java.lang.Integer"
+  ]
+  outClasses = [
+    "java.lang.Integer"
+  ]
+}
+org.apache.streams.config.test.ComponentConfigurationForTestingNumberTwo = {
+  outClasses = [
+    "java.lang.Float"
+  ]
+}
diff --git a/streams-config/src/test/resources/multipath.conf b/streams-config/src/test/resources/multipath.conf
deleted file mode 100644
index 68da7e7..0000000
--- a/streams-config/src/test/resources/multipath.conf
+++ /dev/null
@@ -1,18 +0,0 @@
-org = {
-  inClasses = [
-    "java.lang.Double"
-  ]
-  outClasses = [
-    "java.lang.Double"
-  ]
-}
-org.apache.streams = {
-  inClasses = [
-    "java.lang.Integer"
-  ]
-}
-org.apache.streams.config = {
-  outClasses = [
-    "java.lang.Float"
-  ]
-}

-- 
To stop receiving notification emails like this one, please contact
sblackmon@apache.org.