Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/340#discussion_r79990427
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
 ---
    @@ -107,4 +252,227 @@ public int size() {
         public boolean isEmpty() {
             return ownConfig.isEmpty();
         }
    +    
    +    protected ConfigInheritance getDefaultRuntimeInheritance() {
    +        return BasicConfigInheritance.OVERWRITE; 
    +    }
    +
    +    public <T> ReferenceWithError<ConfigValueAtContainer<TContainer,T>> 
getConfigAndContainer(ConfigKey<T> key) {
    +        return getConfigImpl(key, false);
    +    }
    +
    +    protected abstract TContainer getParentOfContainer(TContainer 
container);
    +    
    +    @Nullable protected final <T> ConfigKey<T> 
getKeyAtContainer(TContainer container, ConfigKey<T> queryKey) {
    +        if (container==null) return null;
    +        @SuppressWarnings("unchecked")
    +        ConfigKey<T> candidate = (ConfigKey<T>) 
getKeyAtContainerImpl(container, queryKey);
    +        return candidate;
    +    }
    +    
    +    @Nullable protected abstract <T> ConfigKey<?> 
getKeyAtContainerImpl(@Nonnull TContainer container, ConfigKey<T> queryKey);
    +
    +    protected Maybe<Object> getRawValueAtContainer(TContainer container, 
ConfigKey<? extends Object> configKey) {
    +        return 
((BrooklynObjectInternal)container).config().getInternalConfigMap().getConfigLocalRaw(configKey);
    +    }
    +    protected Maybe<Object> resolveRawValueFromContainer(TContainer 
container, ConfigKey<?> key, Object value) {
    +        Map<ConfigKey<?>, Object> oc = ((AbstractConfigMapImpl<?>) 
((BrooklynObjectInternal)container).config().getInternalConfigMap()).ownConfig;
    +        if (key instanceof ConfigKeySelfExtracting) {
    +            if (((ConfigKeySelfExtracting<?>)key).isSet(oc)) {
    +                Map<ConfigKey<?>, ?> ownCopy;
    +                synchronized (oc) {
    +                    // wasteful to make a copy to look up; maybe try once 
opportunistically?
    +                    ownCopy = MutableMap.copyOf(oc);
    +                }
    +                return Maybe.of((Object) ((ConfigKeySelfExtracting<?>) 
key).extractValue(ownCopy, getExecutionContext(container)) );
    +            } else {
    +                return Maybe.absent();
    +            }
    +        } else {
    +            // all our keys are self-extracting
    +            LOG.warn("Unexpected key type "+key+" ("+key.getClass()+") in 
"+bo+"; ignoring value");
    +            return Maybe.absent();
    +        }
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    protected <T> T coerce(Object value, Class<T> type) {
    +        if (type==null || value==null) return (T) value;
    +        return (T) TypeCoercions.coerce(value, type);
    +    }
    +    
    +    protected <T> ReferenceWithError<ConfigValueAtContainer<TContainer,T>> 
getConfigImpl(final ConfigKey<T> queryKey, final boolean raw) {
    +        // In case this entity class has overridden the given key (e.g. to 
set default), then retrieve this entity's key
    +        Function<TContainer, ConfigKey<T>> keyFn = new 
Function<TContainer, ConfigKey<T>>() {
    +            @Override public ConfigKey<T> apply(TContainer input) {
    +                // should return null if the key is not known, to indicate 
selected inheritance rules from base key should take effect
    +                return getKeyAtContainer(input, queryKey);
    +            }
    +        };
    +        ConfigKey<T> ownKey = keyFn.apply(getContainer());
    +        if (ownKey==null) ownKey = queryKey;
    +        @SuppressWarnings("unchecked")
    +        final Class<T> type = (Class<T>) ownKey.getType();
    +        
    +        // takes type of own key (or query key if own key not available)
    +        // takes default of own key if available and has default, else of 
query key
    +        
    +        Function<Maybe<Object>, Maybe<T>> coerceFn = new 
Function<Maybe<Object>, Maybe<T>>() {
    +            @SuppressWarnings("unchecked") @Override public Maybe<T> 
apply(Maybe<Object> input) {
    +                if (raw || input==null || input.isAbsent()) return 
(Maybe<T>)input;
    +                return Maybe.ofAllowingNull(coerce(input.get(), type));
    +            }
    +        };
    +        // prefer default and type of ownKey
    +        Maybe<T> defaultValue = raw ? Maybe.<T>absent() :
    +            ownKey.hasDefaultValue() ? 
coerceFn.apply(Maybe.of((Object)ownKey.getDefaultValue())) : 
    +            queryKey.hasDefaultValue() ? 
coerceFn.apply(Maybe.of((Object)queryKey.getDefaultValue())) :
    +                Maybe.<T>absent();
    +            
    +        if (ownKey instanceof ConfigKeySelfExtracting) {
    +            
    +            Function<TContainer, Maybe<Object>> lookupFn = new 
Function<TContainer, Maybe<Object>>() {
    +                @Override public Maybe<Object> apply(TContainer input) {
    +                    Maybe<Object> result = getRawValueAtContainer(input, 
queryKey);
    +                    if (!raw) result = resolveRawValueFromContainer(input, 
queryKey, result);
    +                    return result;
    +                }
    +            };
    +            Function<TContainer, TContainer> parentFn = new 
Function<TContainer, TContainer>() {
    +                @Override public TContainer apply(TContainer input) {
    +                    return getParentOfContainer(input);
    +                }
    +            };
    +            AncestorContainerAndKeyValueIterator<TContainer, T> ckvi = new 
AncestorContainerAndKeyValueIterator<TContainer,T>(
    +                getContainer(), keyFn, lookupFn, coerceFn, parentFn);
    +            
    +            return ConfigInheritances.resolveInheriting(
    +                getContainer(), ownKey, 
coerceFn.apply(lookupFn.apply(getContainer())), defaultValue, 
    +                ckvi, InheritanceContext.RUNTIME_MANAGEMENT, 
getDefaultRuntimeInheritance());
    +            
    +        } else {
    +            String message = "Config key "+ownKey+" of 
"+getBrooklynObject()+" is not a ConfigKeySelfExtracting; cannot retrieve 
value; returning default";
    +            LOG.warn(message);
    +            return ReferenceWithError.newInstanceThrowingError(new 
BasicConfigValueAtContainer<TContainer,T>(getContainer(), ownKey, null, false,
    +                    defaultValue),
    +                new IllegalStateException(message));
    +        }
    +    }
    +    
    +    public List<ConfigValueAtContainer<TContainer,?>> 
getConfigAllInheritedRaw(ConfigKey<?> queryKey) {
    --- End diff --
    
    What is this for? It looks like it is never used (it is just mentioned in 
some javadoc) and is never tested.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to