brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [03/28] incubator-brooklyn git commit: Bootstrap external config suppliers from brooklyn properties.
Date Tue, 01 Sep 2015 15:16:26 GMT
Bootstrap external config suppliers from brooklyn properties.


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

Branch: refs/heads/master
Commit: b0b4cd8a758cb976521aff8497223c0a280ae02e
Parents: 36787a1
Author: Alasdair Hodge <github@alasdairhodge.co.uk>
Authored: Mon Jun 22 16:50:33 2015 +0100
Committer: Alasdair Hodge <github@alasdairhodge.co.uk>
Committed: Tue Aug 25 11:49:23 2015 +0100

----------------------------------------------------------------------
 .../BasicExternalConfigSupplierRegistry.java    | 53 +++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b0b4cd8a/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
b/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
index 605ea11..16f7ed5 100644
--- a/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
+++ b/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
@@ -18,9 +18,19 @@
  */
 package brooklyn.management.internal;
 
+import java.lang.reflect.Constructor;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import brooklyn.config.ConfigPredicates;
+import brooklyn.config.ConfigUtils;
 import brooklyn.config.external.ExternalConfigSupplier;
+import brooklyn.management.ManagementContext;
+import brooklyn.util.exceptions.Exceptions;
 
 import com.google.common.collect.Maps;
 
@@ -32,10 +42,13 @@ import com.google.common.collect.Maps;
  */
 public class BasicExternalConfigSupplierRegistry implements ExternalConfigSupplierRegistry
{
 
+    private static final Logger LOG = LoggerFactory.getLogger(BasicExternalConfigSupplierRegistry.class);
+
     private final Map<String, ExternalConfigSupplier> providersByName = Maps.newLinkedHashMap();
     private final Object providersMapMutex = new Object();
 
-    public BasicExternalConfigSupplierRegistry() {
+    public BasicExternalConfigSupplierRegistry(ManagementContext mgmt) {
+        updateFromBrooklynProperties(mgmt);
     }
 
     @Override
@@ -66,4 +79,42 @@ public class BasicExternalConfigSupplierRegistry implements ExternalConfigSuppli
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private void updateFromBrooklynProperties(ManagementContext mgmt) {
+        // form is:
+        //     brooklyn.external.<name> : fully.qualified.ClassName
+        //     brooklyn.external.<name>.<key> : <value>
+        //     brooklyn.external.<name>.<key> : <value>
+        //     brooklyn.external.<name>.<key> : <value>
+
+        String EXTERNAL_PROVIDER_PREFIX = "brooklyn.external.";
+        Map<String, Object> externalProviderProperties = mgmt.getConfig().submap(ConfigPredicates.startingWith(EXTERNAL_PROVIDER_PREFIX)).asMapWithStringKeys();
+        ClassLoader classloader = mgmt.getCatalogClassLoader();
+        List<Exception> exceptions = new LinkedList<Exception>();
+
+        for (String key : externalProviderProperties.keySet()) {
+            String strippedKey = key.substring(EXTERNAL_PROVIDER_PREFIX.length());
+            if (strippedKey.contains("."))
+                continue;
+
+            String name = strippedKey;
+            String providerClassname = (String) externalProviderProperties.get(key);
+            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(String.class,
Map.class);
+                ExternalConfigSupplier configSupplier = constructor.newInstance(name, config);
+                addProvider(name, configSupplier);
+
+            } catch (Exception e) {
+                LOG.error("Failed to instantiate external config supplier named '" + name
+ "': " + e, e);
+                exceptions.add(e);
+            }
+        }
+
+        if (!exceptions.isEmpty())
+            Exceptions.propagate(exceptions);
+    }
+
 }


Mime
View raw message