brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Externalised configuration
Date Tue, 04 Aug 2015 23:44:49 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/770#discussion_r36256018
  
    --- Diff: core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java
---
    @@ -0,0 +1,108 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package brooklyn.management.internal;
    +
    +import java.lang.reflect.Constructor;
    +import java.util.Map;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import brooklyn.config.ConfigMap;
    +import brooklyn.config.ConfigPredicates;
    +import brooklyn.config.ConfigUtils;
    +import brooklyn.config.external.ExternalConfigSupplier;
    +import brooklyn.management.ManagementContext;
    +
    +import com.google.common.collect.Maps;
    +
    +/**
    + * Simple registry implementation.
    + *
    + * Permits a number of {@link ExternalConfigSupplier} instances to be registered, each
with a unique name, for future
    + * (deferred) lookup of configuration values.
    + */
    +public class BasicExternalConfigSupplierRegistry implements ExternalConfigSupplierRegistry
{
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(BasicExternalConfigSupplierRegistry.class);
    +
    +    private final Map<String, ExternalConfigSupplier> providersByName = Maps.newLinkedHashMap();
    +
    +    public BasicExternalConfigSupplierRegistry(ManagementContext mgmt) {
    +        updateFromBrooklynProperties(mgmt);
    +    }
    +
    +    synchronized public void addProvider(String name, ExternalConfigSupplier provider)
{
    +        if (providersByName.containsKey(name))
    +            throw new IllegalArgumentException("Provider already registered with name
'" + name + "'");
    +        providersByName.put(name, provider);
    +    }
    +
    +    synchronized public void removeProvider(String name) {
    +        providersByName.remove(name);
    +    }
    +
    +    /**
    +     * Searches the named {@link ExternalConfigSupplier} for the config value associated
with the specified key.
    +     * Quietly returns <code>null</code> if no config exists for the specified
key.
    +     * Throws {@link IllegalArgumentException} if no {@link ExternalConfigSupplier} exists
for the passed name.
    +     */
    +    synchronized public String getConfig(String providerName, String key) {
    +        ExternalConfigSupplier provider = providersByName.get(providerName);
    +        if (provider == null)
    +            throw new IllegalArgumentException("No provider found with name '" + providerName
+ "'");
    +        return provider.get(key);
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    synchronized 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.";
    +        ConfigMap externalProviderProperties = mgmt.getConfig().submap(ConfigPredicates.startingWith(EXTERNAL_PROVIDER_PREFIX));
    +        ClassLoader classloader = mgmt.getCatalogClassLoader();
    +
    +        for (String key : externalProviderProperties.asMapWithStringKeys().keySet())
{
    +            String strippedKey = key.substring(EXTERNAL_PROVIDER_PREFIX.length());
    +            if (strippedKey.contains("."))
    +                continue;
    +
    +            String name = strippedKey;
    +            String providerClassname = (String) externalProviderProperties.asMapWithStringKeys().get(key);
    +            Map<String, Object> config = ConfigUtils.filterForPrefixAndStrip(externalProviderProperties.asMapWithStringKeys(),
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(this,
name, config);
    +                addProvider(name, configSupplier);
    +                LOG.info("Added external config supplier named '" + name + "': " + configSupplier);
    +
    +            } catch (Exception e) {
    +                LOG.error("Failed to instantiate external config supplier named '" +
name + "': " + e, e);
    --- End diff --
    
    Should we just log and continue? Or should we propagate? Given these values came from
brooklyn.properties (rather than persisted state etc), then it feels reasonable to fail startup
of the management context.
    
    Thoughts?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message