Switch to new Brooklyn package names. Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/c87c0ea6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/c87c0ea6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/c87c0ea6
Branch: refs/heads/master Commit: c87c0ea6ccd9089cc138ebdc922ee642216b7d5d Parents: 7474be1 Author: Alasdair Hodge <[email protected]> Authored: Tue Aug 25 11:57:16 2015 +0100 Committer: Alasdair Hodge <[email protected]> Committed: Tue Aug 25 11:58:54 2015 +0100 ---------------------------------------------------------------------- .../AbstractExternalConfigSupplier.java | 45 ------- .../config/external/ExternalConfigSupplier.java | 34 ------ .../external/InPlaceExternalConfigSupplier.java | 51 -------- .../PropertiesFileExternalConfigSupplier.java | 68 ----------- .../BasicExternalConfigSupplierRegistry.java | 120 ------------------- .../ExternalConfigSupplierRegistry.java | 45 ------- .../AbstractExternalConfigSupplier.java | 45 +++++++ .../config/external/ExternalConfigSupplier.java | 34 ++++++ .../external/InPlaceExternalConfigSupplier.java | 51 ++++++++ .../PropertiesFileExternalConfigSupplier.java | 68 +++++++++++ .../BasicExternalConfigSupplierRegistry.java | 119 ++++++++++++++++++ .../ExternalConfigSupplierRegistry.java | 45 +++++++ .../spi/dsl/methods/BrooklynDslCommon.java | 29 +---- 13 files changed, 366 insertions(+), 388 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/brooklyn/config/external/AbstractExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/config/external/AbstractExternalConfigSupplier.java b/core/src/main/java/brooklyn/config/external/AbstractExternalConfigSupplier.java deleted file mode 100644 index 7f0cec0..0000000 --- a/core/src/main/java/brooklyn/config/external/AbstractExternalConfigSupplier.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.config.external; - -import brooklyn.management.ManagementContext; - - -/** - * Default superclass for all {@link ExternalConfigSupplier} implementations. - */ -abstract public class AbstractExternalConfigSupplier implements ExternalConfigSupplier { - - private final ManagementContext managementContext; - private final String name; - - protected AbstractExternalConfigSupplier(ManagementContext managementContext, String name) { - this.managementContext = managementContext; - this.name = name; - } - - public ManagementContext getManagementContext() { - return managementContext; - } - - public String getName() { - return name; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/brooklyn/config/external/ExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/config/external/ExternalConfigSupplier.java b/core/src/main/java/brooklyn/config/external/ExternalConfigSupplier.java deleted file mode 100644 index 0df7917..0000000 --- a/core/src/main/java/brooklyn/config/external/ExternalConfigSupplier.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.config.external; - -import com.google.common.annotations.Beta; - -/** - * Provider of "externalised" entity configuration that is resolved at runtime. - * - * @since 0.8.0 - */ -@Beta -public interface ExternalConfigSupplier { - - String getName(); - String get(String key); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/brooklyn/config/external/InPlaceExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/config/external/InPlaceExternalConfigSupplier.java b/core/src/main/java/brooklyn/config/external/InPlaceExternalConfigSupplier.java deleted file mode 100644 index a5a3015..0000000 --- a/core/src/main/java/brooklyn/config/external/InPlaceExternalConfigSupplier.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.config.external; - -import java.util.Map; - -import brooklyn.management.ManagementContext; - - -/** - * Instances are populated via sub-keys specified directly in the <tt>brooklyn.properties</tt> file: - * - * <pre> - * brooklyn.external.foo = brooklyn.management.config.external.InPlaceConfigSupplier - * brooklyn.external.foo.key1 = value1 - * brooklyn.external.foo.key2 = value2 - * </pre> - * - * This will instantiate an <code>InPlaceExternalConfigSupplier</code> populated with values for <code>key1</code> - * and <code>key2</code>. Note that the <code>brooklyn.external.<name></code> prefix is stripped. - */ -public class InPlaceExternalConfigSupplier extends AbstractExternalConfigSupplier { - - private final Map<String, String> config; - - public InPlaceExternalConfigSupplier(ManagementContext managementContext, String name, Map<String, String> config) { - super(managementContext, name); - this.config = config; - } - - public String get(String key) { - return config.get(key); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/brooklyn/config/external/PropertiesFileExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/config/external/PropertiesFileExternalConfigSupplier.java b/core/src/main/java/brooklyn/config/external/PropertiesFileExternalConfigSupplier.java deleted file mode 100644 index 66d72e3..0000000 --- a/core/src/main/java/brooklyn/config/external/PropertiesFileExternalConfigSupplier.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.config.external; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Map; -import java.util.Properties; - -import brooklyn.management.ManagementContext; -import brooklyn.util.stream.Streams; - - -/** - * Instances are populated from a plain java properties file named in the passed <code>config</code> map - * under the <code>propertiesUrl</code> key: - * - * <pre> - * brooklyn.external.foo = brooklyn.management.config.external.PropertiesFileExternalConfigSupplier - * brooklyn.external.foo.propertiesUrl = http://brooklyn.example.com/config/foo.properties - * </pre> - */ -public class PropertiesFileExternalConfigSupplier extends AbstractExternalConfigSupplier { - - public static final String PROPERTIES_URL = "propertiesUrl"; - - private final Properties properties; - - public PropertiesFileExternalConfigSupplier(ManagementContext managementContext, String name, Map<String, String> config) throws IOException { - super(managementContext, name); - this.properties = loadProperties(config.get(PROPERTIES_URL)); - } - - public String get(String key) { - return properties.getProperty(key); - } - - private static Properties loadProperties(String propertiesUrl) throws IOException { - InputStream is = null; - try { - is = new URL(propertiesUrl).openStream(); - Properties p = new Properties(); - p.load(is); - return p; - - } finally { - Streams.closeQuietly(is); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/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 deleted file mode 100644 index 8d81d69..0000000 --- a/core/src/main/java/brooklyn/management/internal/BasicExternalConfigSupplierRegistry.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.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; - -/** - * 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(); - private final Object providersMapMutex = new Object(); - - public BasicExternalConfigSupplierRegistry(ManagementContext mgmt) { - updateFromBrooklynProperties(mgmt); - } - - @Override - public void addProvider(String name, ExternalConfigSupplier supplier) { - synchronized (providersMapMutex) { - if (providersByName.containsKey(name)) - throw new IllegalArgumentException("Provider already registered with name '" + name + "'"); - providersByName.put(name, supplier); - } - LOG.info("Added external config supplier named '" + name + "': " + supplier); - } - - @Override - public void removeProvider(String name) { - synchronized (providersMapMutex) { - ExternalConfigSupplier supplier = providersByName.remove(name); - LOG.info("Removed external config supplier named '" + name + "': " + supplier); - } - } - - @Override - public String getConfig(String providerName, String key) { - synchronized (providersMapMutex) { - ExternalConfigSupplier provider = providersByName.get(providerName); - if (provider == null) - throw new IllegalArgumentException("No provider found with name '" + providerName + "'"); - return provider.get(key); - } - } - - @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(ManagementContext.class, String.class, Map.class); - ExternalConfigSupplier configSupplier = constructor.newInstance(this, 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); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/brooklyn/management/internal/ExternalConfigSupplierRegistry.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/management/internal/ExternalConfigSupplierRegistry.java b/core/src/main/java/brooklyn/management/internal/ExternalConfigSupplierRegistry.java deleted file mode 100644 index 66eb127..0000000 --- a/core/src/main/java/brooklyn/management/internal/ExternalConfigSupplierRegistry.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 com.google.common.annotations.Beta; - -import brooklyn.config.external.ExternalConfigSupplier; - - -/** - * Permits a number of {@link ExternalConfigSupplier} instances to be registered, each with a unique name, for future - * (deferred) lookup of configuration values. - * - * @since 0.8.0 - */ -@Beta -public interface ExternalConfigSupplierRegistry { - - void addProvider(String name, ExternalConfigSupplier provider); - void removeProvider(String 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. - */ - public String getConfig(String providerName, String key); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/org/apache/brooklyn/core/config/external/AbstractExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/config/external/AbstractExternalConfigSupplier.java b/core/src/main/java/org/apache/brooklyn/core/config/external/AbstractExternalConfigSupplier.java new file mode 100644 index 0000000..18b25eb --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/config/external/AbstractExternalConfigSupplier.java @@ -0,0 +1,45 @@ +/* + * 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 org.apache.brooklyn.core.config.external; + +import org.apache.brooklyn.api.mgmt.ManagementContext; + + +/** + * Default superclass for all {@link ExternalConfigSupplier} implementations. + */ +abstract public class AbstractExternalConfigSupplier implements ExternalConfigSupplier { + + private final ManagementContext managementContext; + private final String name; + + protected AbstractExternalConfigSupplier(ManagementContext managementContext, String name) { + this.managementContext = managementContext; + this.name = name; + } + + public ManagementContext getManagementContext() { + return managementContext; + } + + public String getName() { + return name; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/org/apache/brooklyn/core/config/external/ExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/config/external/ExternalConfigSupplier.java b/core/src/main/java/org/apache/brooklyn/core/config/external/ExternalConfigSupplier.java new file mode 100644 index 0000000..c43f7ed --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/config/external/ExternalConfigSupplier.java @@ -0,0 +1,34 @@ +/* + * 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 org.apache.brooklyn.core.config.external; + +import com.google.common.annotations.Beta; + +/** + * Provider of "externalised" entity configuration that is resolved at runtime. + * + * @since 0.8.0 + */ +@Beta +public interface ExternalConfigSupplier { + + String getName(); + String get(String key); + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/org/apache/brooklyn/core/config/external/InPlaceExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/config/external/InPlaceExternalConfigSupplier.java b/core/src/main/java/org/apache/brooklyn/core/config/external/InPlaceExternalConfigSupplier.java new file mode 100644 index 0000000..a5445e9 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/config/external/InPlaceExternalConfigSupplier.java @@ -0,0 +1,51 @@ +/* + * 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 org.apache.brooklyn.core.config.external; + +import java.util.Map; + +import org.apache.brooklyn.api.mgmt.ManagementContext; + + +/** + * Instances are populated via sub-keys specified directly in the <tt>brooklyn.properties</tt> file: + * + * <pre> + * brooklyn.external.foo = brooklyn.management.config.external.InPlaceConfigSupplier + * brooklyn.external.foo.key1 = value1 + * brooklyn.external.foo.key2 = value2 + * </pre> + * + * This will instantiate an <code>InPlaceExternalConfigSupplier</code> populated with values for <code>key1</code> + * and <code>key2</code>. Note that the <code>brooklyn.external.<name></code> prefix is stripped. + */ +public class InPlaceExternalConfigSupplier extends AbstractExternalConfigSupplier { + + private final Map<String, String> config; + + public InPlaceExternalConfigSupplier(ManagementContext managementContext, String name, Map<String, String> config) { + super(managementContext, name); + this.config = config; + } + + public String get(String key) { + return config.get(key); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/org/apache/brooklyn/core/config/external/PropertiesFileExternalConfigSupplier.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/config/external/PropertiesFileExternalConfigSupplier.java b/core/src/main/java/org/apache/brooklyn/core/config/external/PropertiesFileExternalConfigSupplier.java new file mode 100644 index 0000000..f76cdf6 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/config/external/PropertiesFileExternalConfigSupplier.java @@ -0,0 +1,68 @@ +/* + * 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 org.apache.brooklyn.core.config.external; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Map; +import java.util.Properties; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.util.stream.Streams; + + +/** + * Instances are populated from a plain java properties file named in the passed <code>config</code> map + * under the <code>propertiesUrl</code> key: + * + * <pre> + * brooklyn.external.foo = brooklyn.management.config.external.PropertiesFileExternalConfigSupplier + * brooklyn.external.foo.propertiesUrl = http://brooklyn.example.com/config/foo.properties + * </pre> + */ +public class PropertiesFileExternalConfigSupplier extends AbstractExternalConfigSupplier { + + public static final String PROPERTIES_URL = "propertiesUrl"; + + private final Properties properties; + + public PropertiesFileExternalConfigSupplier(ManagementContext managementContext, String name, Map<String, String> config) throws IOException { + super(managementContext, name); + this.properties = loadProperties(config.get(PROPERTIES_URL)); + } + + public String get(String key) { + return properties.getProperty(key); + } + + private static Properties loadProperties(String propertiesUrl) throws IOException { + InputStream is = null; + try { + is = new URL(propertiesUrl).openStream(); + Properties p = new Properties(); + p.load(is); + return p; + + } finally { + Streams.closeQuietly(is); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/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 new file mode 100644 index 0000000..deebd93 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/BasicExternalConfigSupplierRegistry.java @@ -0,0 +1,119 @@ +/* + * 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 org.apache.brooklyn.core.mgmt.internal; + +import java.lang.reflect.Constructor; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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(); + private final Object providersMapMutex = new Object(); + + public BasicExternalConfigSupplierRegistry(ManagementContext mgmt) { + updateFromBrooklynProperties(mgmt); + } + + @Override + public void addProvider(String name, ExternalConfigSupplier supplier) { + synchronized (providersMapMutex) { + if (providersByName.containsKey(name)) + throw new IllegalArgumentException("Provider already registered with name '" + name + "'"); + providersByName.put(name, supplier); + } + LOG.info("Added external config supplier named '" + name + "': " + supplier); + } + + @Override + public void removeProvider(String name) { + synchronized (providersMapMutex) { + ExternalConfigSupplier supplier = providersByName.remove(name); + LOG.info("Removed external config supplier named '" + name + "': " + supplier); + } + } + + @Override + public String getConfig(String providerName, String key) { + synchronized (providersMapMutex) { + ExternalConfigSupplier provider = providersByName.get(providerName); + if (provider == null) + throw new IllegalArgumentException("No provider found with name '" + providerName + "'"); + return provider.get(key); + } + } + + @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(ManagementContext.class, String.class, Map.class); + ExternalConfigSupplier configSupplier = constructor.newInstance(this, 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); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistry.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistry.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistry.java new file mode 100644 index 0000000..81f0ee3 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistry.java @@ -0,0 +1,45 @@ +/* + * 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 org.apache.brooklyn.core.mgmt.internal; + +import org.apache.brooklyn.core.config.external.ExternalConfigSupplier; + +import com.google.common.annotations.Beta; + + +/** + * Permits a number of {@link ExternalConfigSupplier} instances to be registered, each with a unique name, for future + * (deferred) lookup of configuration values. + * + * @since 0.8.0 + */ +@Beta +public interface ExternalConfigSupplierRegistry { + + void addProvider(String name, ExternalConfigSupplier provider); + void removeProvider(String 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. + */ + public String getConfig(String providerName, String key); + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c87c0ea6/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java index 67c31f3..8f1483c 100644 --- a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java @@ -36,6 +36,8 @@ import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier; import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslUtils; import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent.Scope; import org.apache.brooklyn.core.entity.EntityDynamicType; +import org.apache.brooklyn.core.entity.EntityInternal; +import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; import org.apache.brooklyn.core.sensor.DependentConfiguration; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.core.config.ConfigBag; @@ -43,36 +45,13 @@ import org.apache.brooklyn.util.core.flags.ClassCoercionException; import org.apache.brooklyn.util.core.flags.FlagUtils; import org.apache.brooklyn.util.core.flags.TypeCoercions; import org.apache.brooklyn.util.core.task.DeferredSupplier; +import org.apache.brooklyn.util.core.task.Tasks; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.javalang.Reflections; -import org.apache.brooklyn.util.text.Strings; import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes; +import org.apache.brooklyn.util.text.Strings; import org.apache.commons.beanutils.BeanUtils; -import brooklyn.config.external.ExternalConfigSupplier; -import brooklyn.entity.Entity; -import brooklyn.entity.basic.EntityDynamicType; -import brooklyn.entity.basic.EntityInternal; -import brooklyn.entity.trait.Configurable; -import brooklyn.event.Sensor; -import brooklyn.event.basic.DependentConfiguration; -import brooklyn.management.Task; -import brooklyn.management.TaskAdaptable; -import brooklyn.management.TaskFactory; -import brooklyn.management.internal.ExternalConfigSupplierRegistry; -import brooklyn.management.internal.ManagementContextInternal; -import brooklyn.util.collections.MutableMap; -import brooklyn.util.config.ConfigBag; -import brooklyn.util.exceptions.Exceptions; -import brooklyn.util.flags.ClassCoercionException; -import brooklyn.util.flags.FlagUtils; -import brooklyn.util.flags.TypeCoercions; -import brooklyn.util.javalang.Reflections; -import brooklyn.util.task.DeferredSupplier; -import brooklyn.util.text.StringEscapes.JavaStringEscapes; -import brooklyn.util.text.Strings; -import brooklyn.util.task.Tasks; - import com.google.common.base.Function; import com.google.common.collect.Iterables; import com.google.common.collect.Lists;
