Repository: incubator-brooklyn Updated Branches: refs/heads/master 6c72bd4c7 -> 1d1cfe713
Add BrooklynObject.config() - With separate implementations for Entity, Location and AbstractEntityAdjunct. - CatalogItemDo / CatalogItemDtoAbstract throw UnsupportedOperationException Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/4c233de5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/4c233de5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/4c233de5 Branch: refs/heads/master Commit: 4c233de5c43328e22c044c710cbc87c886c60e82 Parents: 6c72bd4 Author: Aled Sage <[email protected]> Authored: Mon Nov 10 19:05:53 2014 +0000 Committer: Aled Sage <[email protected]> Committed: Wed Feb 18 11:02:34 2015 +0000 ---------------------------------------------------------------------- .../java/brooklyn/basic/BrooklynObject.java | 55 +++- api/src/main/java/brooklyn/entity/Entity.java | 26 +- .../java/brooklyn/entity/basic/EntityLocal.java | 18 +- .../brooklyn/basic/BrooklynObjectInternal.java | 62 ++++ .../catalog/internal/CatalogItemDo.java | 8 + .../internal/CatalogItemDtoAbstract.java | 8 + .../brooklyn/entity/basic/AbstractEntity.java | 280 ++++++++++++++----- .../brooklyn/entity/basic/EntityInternal.java | 23 +- .../location/basic/AbstractLocation.java | 140 ++++++++-- .../policy/basic/AbstractEntityAdjunct.java | 113 +++++++- .../brooklyn/policy/basic/ConfigMapImpl.java | 7 + 11 files changed, 627 insertions(+), 113 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/api/src/main/java/brooklyn/basic/BrooklynObject.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/basic/BrooklynObject.java b/api/src/main/java/brooklyn/basic/BrooklynObject.java index 4698780..798d454 100644 --- a/api/src/main/java/brooklyn/basic/BrooklynObject.java +++ b/api/src/main/java/brooklyn/basic/BrooklynObject.java @@ -22,8 +22,12 @@ import java.util.Set; import javax.annotation.Nonnull; +import brooklyn.config.ConfigKey; +import brooklyn.config.ConfigKey.HasConfigKey; import brooklyn.entity.trait.Identifiable; +import brooklyn.management.Task; +import com.google.common.annotations.Beta; import com.google.common.collect.ImmutableMap; /** @@ -63,7 +67,9 @@ public interface BrooklynObject extends Identifiable { @Deprecated TagSupport getTagSupport(); - public static interface TagSupport { + ConfigurationSupport config(); + + public interface TagSupport { /** * @return An immutable copy of the set of tags on this entity. * Note {@link #containsTag(Object)} will be more efficient, @@ -80,4 +86,51 @@ public interface BrooklynObject extends Identifiable { boolean removeTag(@Nonnull Object tag); } + @Beta + public interface ConfigurationSupport { + + /** + * Gets the given configuration value for this entity, in the following order of preference: + * <ol> + * <li> value (including null) explicitly set on the entity + * <li> value (including null) explicitly set on an ancestor (inherited) + * <li> a default value (including null) on the best equivalent static key of the same name declared on the entity + * (where best equivalence is defined as preferring a config key which extends another, + * as computed in EntityDynamicType.getConfigKeys) + * <li> a default value (including null) on the key itself + * <li> null + * </ol> + */ + <T> T get(ConfigKey<T> key); + + /** + * @see {@link #getConfig(ConfigKey)} + */ + <T> T get(HasConfigKey<T> key); + + /** + * Sets the config to the given value. + */ + <T> T set(ConfigKey<T> key, T val); + + /** + * @see {@link #setConfig(HasConfigKey, Object)} + */ + <T> T set(HasConfigKey<T> key, T val); + + /** + * Sets the config to the value returned by the task. + * + * Returns immediately without blocking; subsequent calls to {@link #getConfig(ConfigKey)} + * will execute the task, and block until the task completes. + * + * @see {@link #setConfig(ConfigKey, Object)} + */ + <T> T set(ConfigKey<T> key, Task<T> val); + + /** + * @see {@link #setConfig(ConfigKey, Task)} + */ + <T> T set(HasConfigKey<T> key, Task<T> val); + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/api/src/main/java/brooklyn/entity/Entity.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/entity/Entity.java b/api/src/main/java/brooklyn/entity/Entity.java index 27b902d..0d45a35 100644 --- a/api/src/main/java/brooklyn/entity/Entity.java +++ b/api/src/main/java/brooklyn/entity/Entity.java @@ -194,24 +194,32 @@ public interface Entity extends BrooklynObject { <T> T getAttribute(AttributeSensor<T> sensor); /** - * Gets the given configuration value for this entity, in the following order of preference: - * <li> value (including null) explicitly set on the entity - * <li> value (including null) explicitly set on an ancestor (inherited) - * <li> a default value (including null) on the best equivalent static key of the same name declared on the entity - * (where best equivalence is defined as preferring a config key which extends another, - * as computed in EntityDynamicType.getConfigKeys) - * <li> a default value (including null) on the key itself - * <li> null + * Convenience for calling {@link ConfigurationSupport#getConfig(ConfigKey)}, + * via code like {@code config().get(key)}. */ <T> T getConfig(ConfigKey<T> key); - <T> T getConfig(HasConfigKey<T> key); + /** + * @see #getConfig(ConfigKey)} + */ + <T> T getConfig(HasConfigKey<T> key); /** * Returns the uncoerced value for this config key as set on this entity, if available, * not following any inheritance chains and not taking any default. + * + * @deprecated since 0.7.0; use {@code ((EntityInternal)entity).config().getRaw()} or + * {@code ((EntityInternal)entity).config().getLocalRaw()} */ + @Deprecated Maybe<Object> getConfigRaw(ConfigKey<?> key, boolean includeInherited); + + /** + * @see {@link #getConfigRaw(ConfigKey, boolean)}. + * + * @deprecated since 0.7.0 + */ + @Deprecated Maybe<Object> getConfigRaw(HasConfigKey<?> key, boolean includeInherited); /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/api/src/main/java/brooklyn/entity/basic/EntityLocal.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/entity/basic/EntityLocal.java b/api/src/main/java/brooklyn/entity/basic/EntityLocal.java index 772161c..266e572 100644 --- a/api/src/main/java/brooklyn/entity/basic/EntityLocal.java +++ b/api/src/main/java/brooklyn/entity/basic/EntityLocal.java @@ -60,11 +60,27 @@ public interface EntityLocal extends Entity, Configurable { void setDisplayName(String displayName); /** - * Must be called before the entity is managed. + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().set(key, val)} */ + @Deprecated <T> T setConfig(ConfigKey<T> key, T val); + + /** + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().set(key, val)} + */ + @Deprecated <T> T setConfig(ConfigKey<T> key, Task<T> val); + + /** + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().set(key, val)} + */ + @Deprecated <T> T setConfig(HasConfigKey<T> key, T val); + + /** + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().set(key, val)} + */ + @Deprecated <T> T setConfig(HasConfigKey<T> key, Task<T> val); /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/basic/BrooklynObjectInternal.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/basic/BrooklynObjectInternal.java b/core/src/main/java/brooklyn/basic/BrooklynObjectInternal.java index 26e75c9..9575246 100644 --- a/core/src/main/java/brooklyn/basic/BrooklynObjectInternal.java +++ b/core/src/main/java/brooklyn/basic/BrooklynObjectInternal.java @@ -19,7 +19,15 @@ package brooklyn.basic; import brooklyn.entity.rebind.RebindSupport; +import java.util.Map; + +import brooklyn.config.ConfigKey; +import brooklyn.config.ConfigKey.HasConfigKey; import brooklyn.entity.rebind.Rebindable; +import brooklyn.util.config.ConfigBag; +import brooklyn.util.guava.Maybe; + +import com.google.common.annotations.Beta; public interface BrooklynObjectInternal extends BrooklynObject, Rebindable { @@ -28,4 +36,58 @@ public interface BrooklynObjectInternal extends BrooklynObject, Rebindable { @SuppressWarnings("rawtypes") // subclasses typically apply stronger typing RebindSupport getRebindSupport(); + ConfigurationSupportInternal config(); + + @Beta + public interface ConfigurationSupportInternal extends BrooklynObject.ConfigurationSupport { + + /** + * Returns a read-only view of all the config key/value pairs on this entity, backed by a string-based map, + * including config names that did not match anything on this entity. + */ + @Beta + ConfigBag getBag(); + + /** + * Returns a read-only view of the local (i.e. not inherited) config key/value pairs on this entity, + * backed by a string-based map, including config names that did not match anything on this entity. + */ + @Beta + ConfigBag getLocalBag(); + + /** + * Returns the uncoerced value for this config key, if available, not taking any default. + * If there is no local value and there is an explicit inherited value, will return the inherited. + */ + @Beta + Maybe<Object> getRaw(ConfigKey<?> key); + + /** + * @see {@link #getConfigRaw(ConfigKey)} + */ + @Beta + Maybe<Object> getRaw(HasConfigKey<?> key); + + /** + * Returns the uncoerced value for this config key, if available, + * not following any inheritance chains and not taking any default. + */ + @Beta + Maybe<Object> getLocalRaw(ConfigKey<?> key); + + /** + * @see {@link #getLocalConfigRaw(ConfigKey)} + */ + @Beta + Maybe<Object> getLocalRaw(HasConfigKey<?> key); + + @Beta + void addToLocalBag(Map<String, ?> vals); + + @Beta + void refreshInheritedConfig(); + + @Beta + void refreshInheritedConfigOfChildren(); + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java index fdac692..494bfb0 100644 --- a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java +++ b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java @@ -46,6 +46,14 @@ public class CatalogItemDo<T,SpecT> implements CatalogItem<T,SpecT>, BrooklynObj public CatalogItem<T,SpecT> getDto() { return itemDto; } + + /** + * Config not supported for catalog item. See {@link #getPlanYaml()}. + */ + @Override + public ConfigurationSupportInternal config() { + throw new UnsupportedOperationException(); + } @Override public CatalogItemType getCatalogItemType() { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java index fde647b..cf23c26 100644 --- a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java +++ b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java @@ -62,6 +62,14 @@ public abstract class CatalogItemDtoAbstract<T, SpecT> extends AbstractBrooklynO private @SetFromFlag Collection<CatalogBundle> libraries; private @SetFromFlag Set<Object> tags = Sets.newLinkedHashSet(); + /** + * Config not supported for catalog item. See {@link #getPlanYaml()}. + */ + @Override + public ConfigurationSupportInternal config() { + throw new UnsupportedOperationException(); + } + @Override public String getId() { return getCatalogItemId(); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java index a004bc0..2fa1bf1 100644 --- a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java +++ b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java @@ -212,6 +212,8 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E protected final EntityManagementSupport managementSupport = new EntityManagementSupport(this); + private final BasicConfigurationSupport config = new BasicConfigurationSupport(); + /** * The config values of this entity. Updating this map should be done * via getConfig/setConfig. @@ -236,7 +238,7 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E protected final Map<String,Object> tempWorkings = Maps.newLinkedHashMap(); protected transient SubscriptionTracker _subscriptionTracker; - + public AbstractEntity() { this(Maps.newLinkedHashMap(), null); } @@ -407,21 +409,41 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E /** * Sets a config key value, and returns this Entity instance for use in fluent-API style coding. + * + * @deprecated since 0.7.0; see {@link #config()}, such as {@code config().set(key, value)} */ + @Deprecated public <T> AbstractEntity configure(ConfigKey<T> key, T value) { setConfig(key, value); return this; } + + /** + * @deprecated since 0.7.0; see {@link #config()}, such as {@code config().set(key, value)} + */ + @SuppressWarnings("unchecked") + @Deprecated public <T> AbstractEntity configure(ConfigKey<T> key, String value) { - setConfig((ConfigKey)key, value); + config().set((ConfigKey)key, value); return this; } + + /** + * @deprecated since 0.7.0; see {@link #config()}, such as {@code config().set(key, value)} + */ + @Deprecated public <T> AbstractEntity configure(HasConfigKey<T> key, T value) { - setConfig(key, value); + config().set(key, value); return this; } + + /** + * @deprecated since 0.7.0; see {@link #config()}, such as {@code config().set(key, value)} + */ + @SuppressWarnings("unchecked") + @Deprecated public <T> AbstractEntity configure(HasConfigKey<T> key, String value) { - setConfig((ConfigKey)key, value); + config().set((ConfigKey)key, value); return this; } @@ -472,7 +494,7 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E if (oldConfig.getLocalConfig().size() > 0) { configsInternal.setLocalConfig(oldConfig.getLocalConfig()); } - refreshInheritedConfig(); + config().refreshInheritedConfig(); attributesInternal = new AttributeMap(this, managementContext.getStorage().<Collection<String>, Object>getMap(getId()+"-attributes")); if (oldAttribs.asRawMap().size() > 0) { @@ -563,7 +585,7 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E parent.set(entity); entity.addChild(getProxyIfAvailable()); - refreshInheritedConfig(); + config().refreshInheritedConfig(); previouslyOwned = true; getApplication(); @@ -918,81 +940,211 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E } @Override + public Map<AttributeSensor, Object> getAllAttributes() { + Map<AttributeSensor, Object> result = Maps.newLinkedHashMap(); + Map<String, Object> attribs = attributesInternal.asMap(); + for (Map.Entry<String,Object> entry : attribs.entrySet()) { + AttributeSensor<?> attribKey = (AttributeSensor<?>) entityType.getSensor(entry.getKey()); + if (attribKey == null) { + // Most likely a race: e.g. persister thread calling getAllAttributes; writer thread + // has written attribute value and is in process of calling entityType.addSensorIfAbsent(attribute) + // Just use a synthetic AttributeSensor, rather than ignoring value. + // TODO If it's not a race, then don't log.warn every time! + LOG.warn("When retrieving all attributes of {}, no AttributeSensor for attribute {} (creating synthetic)", this, entry.getKey()); + attribKey = Sensors.newSensor(Object.class, entry.getKey()); + } + result.put(attribKey, entry.getValue()); + } + return result; + } + + + // -------- CONFIGURATION -------------- + + @Override + public ConfigurationSupportInternal config() { + return config; + } + + private class BasicConfigurationSupport implements ConfigurationSupportInternal { + + @Override + public <T> T get(ConfigKey<T> key) { + return configsInternal.getConfig(key); + } + + @Override + public <T> T get(HasConfigKey<T> key) { + return get(key.getConfigKey()); + } + + @Override + public <T> T set(ConfigKey<T> key, T val) { + return setConfigInternal(key, val); + } + + @Override + public <T> T set(HasConfigKey<T> key, T val) { + return set(key.getConfigKey(), val); + } + + @Override + public <T> T set(ConfigKey<T> key, Task<T> val) { + return setConfigInternal(key, val); + } + + @Override + public <T> T set(HasConfigKey<T> key, Task<T> val) { + return set(key.getConfigKey(), val); + } + + @Override + public ConfigBag getBag() { + return configsInternal.getAllConfigBag(); + } + + @Override + public ConfigBag getLocalBag() { + return configsInternal.getLocalConfigBag(); + } + + @Override + public Maybe<Object> getRaw(ConfigKey<?> key) { + return configsInternal.getConfigRaw(key, true); + } + + @Override + public Maybe<Object> getRaw(HasConfigKey<?> key) { + return getRaw(key.getConfigKey()); + } + + @Override + public Maybe<Object> getLocalRaw(ConfigKey<?> key) { + return configsInternal.getConfigRaw(key, false); + } + + @Override + public Maybe<Object> getLocalRaw(HasConfigKey<?> key) { + return getLocalRaw(key.getConfigKey()); + } + + @Override + public void addToLocalBag(Map<String, ?> vals) { + configsInternal.addToLocalBag(vals); + } + + @Override + public void refreshInheritedConfig() { + if (getParent() != null) { + configsInternal.setInheritedConfig(((EntityInternal)getParent()).getAllConfig(), ((EntityInternal)getParent()).config().getBag()); + } else { + configsInternal.clearInheritedConfig(); + } + + refreshInheritedConfigOfChildren(); + } + + @Override + public void refreshInheritedConfigOfChildren() { + for (Entity it : getChildren()) { + ((EntityInternal)it).config().refreshInheritedConfig(); + } + } + + @SuppressWarnings("unchecked") + private <T> T setConfigInternal(ConfigKey<T> key, Object val) { + if (!inConstruction && getManagementSupport().isDeployed()) { + // previously we threw, then warned, but it is still quite common; + // so long as callers don't expect miracles, it should be fine. + // i (Alex) think the way to be stricter about this (if that becomes needed) + // would be to introduce a 'mutable' field on config keys + LOG.debug("configuration being made to {} after deployment: {} = {}; change may not be visible in other contexts", + new Object[] { this, key, val }); + } + T result = (T) configsInternal.setConfig(key, val); + + getManagementSupport().getEntityChangeListener().onConfigChanged(key); + return result; + + } + } + + @Override public <T> T getConfig(ConfigKey<T> key) { - return configsInternal.getConfig(key); + return config().get(key); } @Override public <T> T getConfig(HasConfigKey<T> key) { - return configsInternal.getConfig(key); + return config().get(key); } @Override + @Deprecated public <T> T getConfig(HasConfigKey<T> key, T defaultValue) { return configsInternal.getConfig(key, defaultValue); } //don't use groovy defaults for defaultValue as that doesn't implement the contract; we need the above @Override + @Deprecated public <T> T getConfig(ConfigKey<T> key, T defaultValue) { return configsInternal.getConfig(key, defaultValue); } @Override + @Deprecated public Maybe<Object> getConfigRaw(ConfigKey<?> key, boolean includeInherited) { - return configsInternal.getConfigRaw(key, includeInherited); + return (includeInherited) ? config().getRaw(key) : config().getLocalRaw(key); } @Override + @Deprecated public Maybe<Object> getConfigRaw(HasConfigKey<?> key, boolean includeInherited) { - return getConfigRaw(key.getConfigKey(), includeInherited); - } - - @SuppressWarnings("unchecked") - private <T> T setConfigInternal(ConfigKey<T> key, Object val) { - if (!inConstruction && getManagementSupport().isDeployed()) { - // previously we threw, then warned, but it is still quite common; - // so long as callers don't expect miracles, it should be fine. - // i (Alex) think the way to be stricter about this (if that becomes needed) - // would be to introduce a 'mutable' field on config keys - LOG.debug("configuration being made to {} after deployment: {} = {}; change may not be visible in other contexts", - new Object[] { this, key, val }); - } - T result = (T) configsInternal.setConfig(key, val); - - getManagementSupport().getEntityChangeListener().onConfigChanged(key); - return result; - + return (includeInherited) ? config().getRaw(key) : config().getLocalRaw(key); } @Override + @Deprecated public <T> T setConfig(ConfigKey<T> key, T val) { - return setConfigInternal(key, val); + return config().set(key, val); } @Override + @Deprecated public <T> T setConfig(ConfigKey<T> key, Task<T> val) { - return setConfigInternal(key, val); + return config().set(key, val); } + /** + * @deprecated since 0.7.0; use {@code config().set(key, task)}, with {@link Task} instead of {@link DeferredSupplier} + */ + @Deprecated public <T> T setConfig(ConfigKey<T> key, DeferredSupplier val) { - return setConfigInternal(key, val); + return config.setConfigInternal(key, val); } @Override + @Deprecated public <T> T setConfig(HasConfigKey<T> key, T val) { - return setConfig(key.getConfigKey(), val); + return config().set(key, val); } @Override + @Deprecated public <T> T setConfig(HasConfigKey<T> key, Task<T> val) { - return (T) setConfig(key.getConfigKey(), val); + return (T) config().set(key, val); } + /** + * @deprecated since 0.7.0; use {@code config().set(key, task)}, with {@link Task} instead of {@link DeferredSupplier} + */ + @Deprecated public <T> T setConfig(HasConfigKey<T> key, DeferredSupplier val) { return setConfig(key.getConfigKey(), val); } + @SuppressWarnings("unchecked") public <T> T setConfigEvenIfOwned(ConfigKey<T> key, T val) { return (T) configsInternal.setConfig(key, val); } @@ -1001,71 +1153,69 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E return setConfigEvenIfOwned(key.getConfigKey(), val); } + /** + * @deprecated since 0.7.0; use {@code if (val != null) config().set(key, val)} + */ + @Deprecated + @SuppressWarnings({ "unchecked", "rawtypes" }) protected void setConfigIfValNonNull(ConfigKey key, Object val) { - if (val != null) setConfig(key, val); + if (val != null) config().set(key, val); } - + + /** + * @deprecated since 0.7.0; use {@code if (val != null) config().set(key, val)} + */ + @Deprecated + @SuppressWarnings({ "unchecked", "rawtypes" }) protected void setConfigIfValNonNull(HasConfigKey key, Object val) { - if (val != null) setConfig(key, val); + if (val != null) config().set(key, val); } + /** + * @deprecated since 0.7.0; see {@code config().refreshInheritedConfig()} + */ @Override + @Deprecated public void refreshInheritedConfig() { - if (getParent() != null) { - configsInternal.setInheritedConfig(((EntityInternal)getParent()).getAllConfig(), ((EntityInternal)getParent()).getAllConfigBag()); - } else { - configsInternal.clearInheritedConfig(); - } - - refreshInheritedConfigOfChildren(); + config().refreshInheritedConfig(); } + /** + * @deprecated since 0.7.0; see {@code config().refreshInheritedConfigOfChildren()} + */ + @Deprecated void refreshInheritedConfigOfChildren() { - for (Entity it : getChildren()) { - ((EntityInternal)it).refreshInheritedConfig(); - } + config().refreshInheritedConfigOfChildren(); } @Override + @Deprecated public EntityConfigMap getConfigMap() { return configsInternal; } @Override + @Deprecated public Map<ConfigKey<?>,Object> getAllConfig() { return configsInternal.getAllConfig(); } @Beta @Override + @Deprecated public ConfigBag getAllConfigBag() { - return configsInternal.getAllConfigBag(); + return config().getBag(); } @Beta @Override + @Deprecated public ConfigBag getLocalConfigBag() { - return configsInternal.getLocalConfigBag(); + return config().getLocalBag(); } - @Override - public Map<AttributeSensor, Object> getAllAttributes() { - Map<AttributeSensor, Object> result = Maps.newLinkedHashMap(); - Map<String, Object> attribs = attributesInternal.asMap(); - for (Map.Entry<String,Object> entry : attribs.entrySet()) { - AttributeSensor<?> attribKey = (AttributeSensor<?>) entityType.getSensor(entry.getKey()); - if (attribKey == null) { - // Most likely a race: e.g. persister thread calling getAllAttributes; writer thread - // has written attribute value and is in process of calling entityType.addSensorIfAbsent(attribute) - // Just use a synthetic AttributeSensor, rather than ignoring value. - // TODO If it's not a race, then don't log.warn every time! - LOG.warn("When retrieving all attributes of {}, no AttributeSensor for attribute {} (creating synthetic)", this, entry.getKey()); - attribKey = Sensors.newSensor(Object.class, entry.getKey()); - } - result.put(attribKey, entry.getValue()); - } - return result; - } + + // -------- SUBSCRIPTIONS -------------- /** @see EntityLocal#subscribe */ @Override http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/entity/basic/EntityInternal.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/entity/basic/EntityInternal.java b/core/src/main/java/brooklyn/entity/basic/EntityInternal.java index 56443a9..ba8df78 100644 --- a/core/src/main/java/brooklyn/entity/basic/EntityInternal.java +++ b/core/src/main/java/brooklyn/entity/basic/EntityInternal.java @@ -57,26 +57,37 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb */ <T> T setAttributeWithoutPublishing(AttributeSensor<T> sensor, T val); + /** + * @deprecated since 0.7.0; instead just use methods on {@link ConfigurationSupportInternal} returned by {@link #config()} + */ + @Deprecated EntityConfigMap getConfigMap(); /** * @return a read-only copy of all the config key/value pairs on this entity. + * + * @deprecated since 0.7.0; instead just use methods on {@link ConfigurationSupportInternal} returned by {@link #config()} */ + @Deprecated @Beta Map<ConfigKey<?>,Object> getAllConfig(); /** * Returns a read-only view of all the config key/value pairs on this entity, backed by a string-based map, * including config names that did not match anything on this entity. + * + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().getBag()} */ - @Beta + @Deprecated ConfigBag getAllConfigBag(); /** * Returns a read-only view of the local (i.e. not inherited) config key/value pairs on this entity, * backed by a string-based map, including config names that did not match anything on this entity. + * + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().getLocalBag()} */ - @Beta + @Deprecated ConfigBag getLocalConfigBag(); @Beta @@ -84,8 +95,12 @@ public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Reb @Beta void removeAttribute(AttributeSensor<?> attribute); - - @Beta + + /** + * + * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().refreshInheritedConfig()} + */ + @Deprecated void refreshInheritedConfig(); /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/location/basic/AbstractLocation.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/location/basic/AbstractLocation.java b/core/src/main/java/brooklyn/location/basic/AbstractLocation.java index 59bbfc7..fe592db 100644 --- a/core/src/main/java/brooklyn/location/basic/AbstractLocation.java +++ b/core/src/main/java/brooklyn/location/basic/AbstractLocation.java @@ -48,6 +48,7 @@ import brooklyn.location.Location; import brooklyn.location.LocationSpec; import brooklyn.location.geo.HasHostGeoInfo; import brooklyn.location.geo.HostGeoInfo; +import brooklyn.management.Task; import brooklyn.management.internal.LocalLocationManager; import brooklyn.management.internal.ManagementContextInternal; import brooklyn.mementos.LocationMemento; @@ -55,6 +56,7 @@ import brooklyn.util.collections.SetFromLiveMap; import brooklyn.util.config.ConfigBag; import brooklyn.util.flags.FlagUtils; import brooklyn.util.flags.TypeCoercions; +import brooklyn.util.guava.Maybe; import brooklyn.util.stream.Streams; import com.google.common.base.Objects; @@ -100,6 +102,8 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements private Reference<HostGeoInfo> hostGeoInfo = new BasicReference<HostGeoInfo>(); + private ConfigurationSupportInternal config = new BasicConfigurationSupport(); + private ConfigBag configBag = new ConfigBag(); private volatile boolean managed; @@ -109,7 +113,7 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements private final Map<Class<?>, Object> extensions = Maps.newConcurrentMap(); private final LocationDynamicType locationType; - + /** * Construct a new instance of an AbstractLocation. */ @@ -343,28 +347,121 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements } @Override - public <T> T getConfig(HasConfigKey<T> key) { - return getConfig(key.getConfigKey()); + public ConfigurationSupportInternal config() { + return config ; } - @Override - public <T> T getConfig(ConfigKey<T> key) { - if (hasConfig(key, false)) return getLocalConfigBag().get(key); - if (getParent()!=null && isInherited(key)) { - return getParent().getConfig(key); + private class BasicConfigurationSupport implements ConfigurationSupportInternal { + + @Override + public <T> T get(ConfigKey<T> key) { + if (hasConfig(key, false)) return getLocalBag().get(key); + if (getParent() != null && isInherited(key)) { + return getParent().getConfig(key); + } + + // In case this entity class has overridden the given key (e.g. to set default), then retrieve this entity's key + // TODO when locations become entities, the duplication of this compared to EntityConfigMap.getConfig will disappear. + @SuppressWarnings("unchecked") + ConfigKey<T> ownKey = (ConfigKey<T>) elvis(locationType.getConfigKey(key.getName()), key); + + return ownKey.getDefaultValue(); + } + + @Override + public <T> T get(HasConfigKey<T> key) { + return get(key.getConfigKey()); + } + + @Override + public <T> T set(ConfigKey<T> key, T val) { + T result = configBag.put(key, val); + onChanged(); + return result; + } + + @Override + public <T> T set(HasConfigKey<T> key, T val) { + return set(key.getConfigKey(), val); + } + + @Override + public <T> T set(ConfigKey<T> key, Task<T> val) { + // TODO Support for locations + throw new UnsupportedOperationException(); + } + + @Override + public <T> T set(HasConfigKey<T> key, Task<T> val) { + // TODO Support for locations + throw new UnsupportedOperationException(); + } + + @Override + public ConfigBag getBag() { + ConfigBag result = ConfigBag.newInstanceExtending(configBag, ImmutableMap.of()); + Location p = getParent(); + if (p!=null) result.putIfAbsent(((LocationInternal)p).getAllConfigBag().getAllConfig()); + return result; + } + + @Override + public ConfigBag getLocalBag() { + return configBag; + } + + @Override + public Maybe<Object> getRaw(ConfigKey<?> key) { + if (hasConfig(key, false)) return Maybe.of(getLocalBag().getStringKey(key.getName())); + if (getParent() != null) return ((AbstractLocation)getParent()).config().getRaw(key); + return Maybe.absent(); + } + + @Override + public Maybe<Object> getRaw(HasConfigKey<?> key) { + return getRaw(key.getConfigKey()); + } + + @Override + public Maybe<Object> getLocalRaw(ConfigKey<?> key) { + if (hasConfig(key, false)) return Maybe.of(getLocalBag().getStringKey(key.getName())); + return Maybe.absent(); + } + + @Override + public Maybe<Object> getLocalRaw(HasConfigKey<?> key) { + return getLocalRaw(key.getConfigKey()); + } + + @Override + public void addToLocalBag(Map<String, ?> vals) { + configBag.putAll(vals); } - // In case this entity class has overridden the given key (e.g. to set default), then retrieve this entity's key - // TODO when locations become entities, the duplication of this compared to EntityConfigMap.getConfig will disappear. - @SuppressWarnings("unchecked") - ConfigKey<T> ownKey = (ConfigKey<T>) elvis(locationType.getConfigKey(key.getName()), key); + @Override + public void refreshInheritedConfig() { + // no-op for location + } + + @Override + public void refreshInheritedConfigOfChildren() { + // no-op for location + } + } + + @Override + public <T> T getConfig(HasConfigKey<T> key) { + return config().get(key); + } - return ownKey.getDefaultValue(); + @Override + public <T> T getConfig(ConfigKey<T> key) { + return config().get(key); } @Override public boolean hasConfig(ConfigKey<?> key, boolean includeInherited) { - boolean locally = getLocalConfigBag().containsKey(key); + boolean locally = config().getLocalBag().containsKey(key); if (locally) return true; if (!includeInherited || !isInherited(key)) return false; if (getParent()!=null) return getParent().hasConfig(key, true); @@ -383,7 +480,7 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements @Override public Map<String,Object> getAllConfig(boolean includeInherited) { - ConfigBag bag = (includeInherited ? getAllConfigBag() : getLocalConfigBag()); + ConfigBag bag = (includeInherited ? config().getBag() : config().getLocalBag()); return bag.getAllConfig(); } @@ -393,15 +490,12 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements // here ConfigBag is used exclusively so // we have no information about what to include/exclude inheritance wise. // however few things use getAllConfigBag() - ConfigBag result = ConfigBag.newInstanceExtending(configBag, ImmutableMap.of()); - Location p = getParent(); - if (p!=null) result.putIfAbsent(((LocationInternal)p).getAllConfigBag().getAllConfig()); - return result; + return config().getBag(); } @Override public ConfigBag getLocalConfigBag() { - return configBag; + return config().getLocalBag(); } /** @@ -409,14 +503,12 @@ public abstract class AbstractLocation extends AbstractBrooklynObject implements * @since 0.6 */ public ConfigBag getRawLocalConfigBag() { - return getLocalConfigBag(); + return config().getLocalBag(); } @Override public <T> T setConfig(ConfigKey<T> key, T value) { - T result = configBag.put(key, value); - onChanged(); - return result; + return config().set(key, value); } /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java b/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java index a005e34..ba5ecd0 100644 --- a/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java +++ b/core/src/main/java/brooklyn/policy/basic/AbstractEntityAdjunct.java @@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory; import brooklyn.basic.AbstractBrooklynObject; import brooklyn.basic.BrooklynObjectInternal; import brooklyn.config.ConfigKey; +import brooklyn.config.ConfigKey.HasConfigKey; import brooklyn.config.ConfigMap; import brooklyn.enricher.basic.AbstractEnricher; import brooklyn.entity.Entity; @@ -49,12 +50,14 @@ import brooklyn.event.SensorEventListener; import brooklyn.management.ExecutionContext; import brooklyn.management.SubscriptionContext; import brooklyn.management.SubscriptionHandle; +import brooklyn.management.Task; import brooklyn.management.internal.SubscriptionTracker; import brooklyn.policy.EntityAdjunct; import brooklyn.util.config.ConfigBag; import brooklyn.util.flags.FlagUtils; import brooklyn.util.flags.SetFromFlag; import brooklyn.util.flags.TypeCoercions; +import brooklyn.util.guava.Maybe; import com.google.common.annotations.Beta; import com.google.common.base.Objects; @@ -75,6 +78,8 @@ public abstract class AbstractEntityAdjunct extends AbstractBrooklynObject imple protected transient ExecutionContext execution; + private final BasicConfigurationSupport config = new BasicConfigurationSupport(); + /** * The config values of this entity. Updating this map should be done * via getConfig/setConfig. @@ -168,8 +173,104 @@ public abstract class AbstractEntityAdjunct extends AbstractBrooklynObject imple return _legacyNoConstructionInit; } + @Override + public ConfigurationSupportInternal config() { + return config; + } + + private class BasicConfigurationSupport implements ConfigurationSupportInternal { + + @Override + public <T> T get(ConfigKey<T> key) { + return configsInternal.getConfig(key); + } + + @Override + public <T> T get(HasConfigKey<T> key) { + return get(key.getConfigKey()); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T set(ConfigKey<T> key, T val) { + if (entity != null && isRunning()) { + doReconfigureConfig(key, val); + } + T result = (T) configsInternal.setConfig(key, val); + onChanged(); + return result; + } + + @Override + public <T> T set(HasConfigKey<T> key, T val) { + return setConfig(key.getConfigKey(), val); + } + + @SuppressWarnings("unchecked") + @Override + public <T> T set(ConfigKey<T> key, Task<T> val) { + if (entity != null && isRunning()) { + // TODO Support for AbstractEntityAdjunct + throw new UnsupportedOperationException(); + } + T result = (T) configsInternal.setConfig(key, val); + onChanged(); + return result; + } + + @Override + public <T> T set(HasConfigKey<T> key, Task<T> val) { + return set(key.getConfigKey(), val); + } + + @Override + public ConfigBag getBag() { + return getLocalBag(); + } + + @Override + public ConfigBag getLocalBag() { + return ConfigBag.newInstance(configsInternal.getAllConfig()); + } + + @Override + public Maybe<Object> getRaw(ConfigKey<?> key) { + return configsInternal.getConfigRaw(key, true); + } + + @Override + public Maybe<Object> getRaw(HasConfigKey<?> key) { + return getRaw(key.getConfigKey()); + } + + @Override + public Maybe<Object> getLocalRaw(ConfigKey<?> key) { + return configsInternal.getConfigRaw(key, false); + } + + @Override + public Maybe<Object> getLocalRaw(HasConfigKey<?> key) { + return getLocalRaw(key.getConfigKey()); + } + + @Override + public void addToLocalBag(Map<String, ?> vals) { + configsInternal.addToLocalBag(vals); + } + + @Override + public void refreshInheritedConfig() { + // no-op for location + } + + @Override + public void refreshInheritedConfigOfChildren() { + // no-op for location + } + } + public <T> T getConfig(ConfigKey<T> key) { - return configsInternal.getConfig(key); + return config().get(key); } public Map<ConfigKey<?>, Object> getAllConfig() { @@ -177,18 +278,12 @@ public abstract class AbstractEntityAdjunct extends AbstractBrooklynObject imple } protected <K> K getRequiredConfig(ConfigKey<K> key) { - return checkNotNull(getConfig(key), key.getName()); + return checkNotNull(config().get(key), key.getName()); } - @SuppressWarnings("unchecked") @Override public <T> T setConfig(ConfigKey<T> key, T val) { - if (entity != null && isRunning()) { - doReconfigureConfig(key, val); - } - T result = (T) configsInternal.setConfig(key, val); - onChanged(); - return result; + return config().set(key, val); } // TODO make immutable http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4c233de5/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java b/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java index 686c4c2..9d62dda 100644 --- a/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java +++ b/core/src/main/java/brooklyn/policy/basic/ConfigMapImpl.java @@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory; import brooklyn.config.ConfigKey; import brooklyn.config.ConfigKey.HasConfigKey; +import brooklyn.entity.basic.ConfigKeys; import brooklyn.entity.basic.ConfigMapViewWithStringKeys; import brooklyn.entity.basic.Entities; import brooklyn.entity.basic.EntityInternal; @@ -160,6 +161,12 @@ public class ConfigMapImpl implements brooklyn.config.ConfigMap { return oldVal; } + public void addToLocalBag(Map<String, ?> vals) { + for (Map.Entry<String, ?> entry : vals.entrySet()) { + setConfig(ConfigKeys.newConfigKey(Object.class, entry.getKey()), entry.getValue()); + } + } + @Override public ConfigMapImpl submap(Predicate<ConfigKey<?>> filter) { ConfigMapImpl m = new ConfigMapImpl(adjunct);
