brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [2/6] incubator-brooklyn git commit: Support ExternalConfigSupplier without constructor map arg
Date Mon, 14 Sep 2015 16:47:59 GMT
Support ExternalConfigSupplier without constructor map arg

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/306c07c2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/306c07c2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/306c07c2

Branch: refs/heads/master
Commit: 306c07c2fd4711d118615d72cc7440acf145a081
Parents: b0d321c
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon Sep 14 16:15:38 2015 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Mon Sep 14 16:42:44 2015 +0100

----------------------------------------------------------------------
 .../BasicExternalConfigSupplierRegistry.java    | 16 ++++--
 .../camp/brooklyn/ExternalConfigYamlTest.java   | 59 ++++++++++++++++++++
 2 files changed, 70 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/306c07c2/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java
index 4765cda..6b261db 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java
@@ -18,7 +18,6 @@
  */
 package org.apache.brooklyn.core.mgmt.internal;
 
-import java.lang.reflect.Constructor;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -28,9 +27,11 @@ import org.apache.brooklyn.core.config.ConfigPredicates;
 import org.apache.brooklyn.core.config.ConfigUtils;
 import org.apache.brooklyn.core.config.external.ExternalConfigSupplier;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.javalang.Reflections;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
 
 /**
@@ -101,10 +102,15 @@ public class BasicExternalConfigSupplierRegistry implements ExternalConfigSuppli
             Map<String, Object> config = ConfigUtils.filterForPrefixAndStrip(externalProviderProperties,
key + ".");
 
             try {
-                Class<? extends ExternalConfigSupplier> providerClass = (Class<?
extends ExternalConfigSupplier>) classloader.loadClass(providerClassname);
-                Constructor<? extends ExternalConfigSupplier> constructor = providerClass.getConstructor(ManagementContext.class,
String.class, Map.class);
-                ExternalConfigSupplier configSupplier = constructor.newInstance(mgmt, name,
config);
-                addProvider(name, configSupplier);
+                Optional<ExternalConfigSupplier> configSupplier = Reflections.invokeConstructorWithArgs(classloader,
providerClassname, mgmt, name, config);
+                if (!configSupplier.isPresent()) {
+                    configSupplier = Reflections.invokeConstructorWithArgs(classloader, providerClassname,
mgmt, name);
+                }
+                if (!configSupplier.isPresent()) {
+                    throw new IllegalStateException("No matching constructor found in "+providerClassname);
+                }
+                
+                addProvider(name, configSupplier.get());
 
             } catch (Exception e) {
                 LOG.error("Failed to instantiate external config supplier named '" + name
+ "': " + e, e);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/306c07c2/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java
b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java
index 274056c..7f46861 100644
--- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java
+++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java
@@ -28,6 +28,7 @@ import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.config.external.AbstractExternalConfigSupplier;
+import org.apache.brooklyn.core.config.external.ExternalConfigSupplier;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
 import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
@@ -48,6 +49,7 @@ public class ExternalConfigYamlTest extends AbstractYamlTest {
         BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
         props.put("brooklyn.external.myprovider", MyExternalConfigSupplier.class.getName());
         props.put("brooklyn.external.myprovider.mykey", "myval");
+        props.put("brooklyn.external.myproviderWithoutMapArg", MyExternalConfigSupplierWithoutMapArg.class.getName());
         
         return LocalManagementContextForTests.builder(true)
                 .useProperties(props)
@@ -71,6 +73,22 @@ public class ExternalConfigYamlTest extends AbstractYamlTest {
     }
     
     @Test
+    public void testExternalisedConfigFromSupplierWithoutMapArg() throws Exception {
+        ConfigKey<String> MY_CONFIG_KEY = ConfigKeys.newStringConfigKey("my.config.key");
+        
+        String yaml = Joiner.on("\n").join(
+            "services:",
+            "- serviceType: org.apache.brooklyn.core.test.entity.TestApplication",
+            "  brooklyn.config:",
+            "    my.config.key: $brooklyn:external(\"myproviderWithoutMapArg\", \"mykey\")");
+        
+        TestApplication app = (TestApplication) createAndStartApplication(new StringReader(yaml));
+        waitForApplicationTasks(app);
+
+        assertEquals(app.getConfig(MY_CONFIG_KEY), "myHardcodedVal");
+    }
+
+    @Test
     public void testWhenExternalisedConfigSupplierDoesNotExist() throws Exception {
         BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
         props.put("brooklyn.external.myprovider", "wrong.classname.DoesNotExist");
@@ -87,6 +105,24 @@ public class ExternalConfigYamlTest extends AbstractYamlTest {
         }
     }
     
+    @Test
+    public void testWhenExternalisedConfigSupplierDoesNotHavingRightConstructor() throws
Exception {
+        BrooklynProperties props = BrooklynProperties.Factory.newEmpty();
+        props.put("brooklyn.external.myprovider", MyExternalConfigSupplierWithWrongConstructor.class.getName());
+        
+        try {
+            LocalManagementContext mgmt2 = LocalManagementContextForTests.builder(true)
+                    .useProperties(props)
+                    .build();
+            mgmt2.terminate();
+            fail();
+        } catch (Exception e) {
+            if (!e.toString().contains("No matching constructor")) {
+                throw e;
+            }
+        }
+    }
+    
     @Override
     protected Logger getLogger() {
         return log;
@@ -104,4 +140,27 @@ public class ExternalConfigYamlTest extends AbstractYamlTest {
             return conf.get(key);
         }
     }
+    
+    public static class MyExternalConfigSupplierWithoutMapArg extends AbstractExternalConfigSupplier
{
+        public MyExternalConfigSupplierWithoutMapArg(ManagementContext mgmt, String name)
{
+            super(mgmt, name);
+        }
+        
+        @Override public String get(String key) {
+            return key.equals("mykey") ? "myHardcodedVal" : null;
+        }
+    }
+    
+    public static class MyExternalConfigSupplierWithWrongConstructor implements ExternalConfigSupplier
{
+        public MyExternalConfigSupplierWithWrongConstructor(double d) {
+        }
+        
+        @Override public String getName() {
+            return "myname";
+        }
+        
+        @Override public String get(String key) {
+            return null;
+        }
+    }
 }


Mime
View raw message