brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #462: Inherit config default values
Date Fri, 06 Jan 2017 17:42:24 GMT
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/462#discussion_r94981371
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/config/BasicConfigInheritance.java
---
    @@ -28,51 +32,188 @@
     import org.apache.brooklyn.config.ConfigKey;
     import org.apache.brooklyn.config.ConfigValueAtContainer;
     import org.apache.brooklyn.util.collections.CollectionMerger;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
     import org.apache.brooklyn.util.exceptions.ReferenceWithError;
     import org.apache.brooklyn.util.guava.Maybe;
    +import org.apache.brooklyn.util.text.Strings;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
     
    +@SuppressWarnings("serial")
     public class BasicConfigInheritance implements ConfigInheritance {
     
    +    private static final Logger log = LoggerFactory.getLogger(BasicConfigInheritance.class);
    +    
         private static final long serialVersionUID = -5916548049057961051L;
     
         public static final String CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE = "deep_merge";
         public static final String CONFLICT_RESOLUTION_STRATEGY_OVERWRITE = "overwrite";
         
    +    public static abstract class DelegatingConfigInheritance implements ConfigInheritance
{
    +        protected abstract ConfigInheritance getDelegate();
    +        
    +        @Override @Deprecated public InheritanceMode isInherited(ConfigKey<?> key,
Object from, Object to) {
    +            return getDelegate().isInherited(key, from, to);
    +        }
    +        @Override public <TContainer, TValue> boolean isReinheritable(ConfigValueAtContainer<TContainer,
TValue> parent, ConfigInheritanceContext context) {
    +            return getDelegate().isReinheritable(parent, context);
    +        }
    +        @Override public <TContainer, TValue> boolean considerParent(ConfigValueAtContainer<TContainer,
TValue> local, ConfigValueAtContainer<TContainer, TValue> parent, ConfigInheritanceContext
context) {
    +            return getDelegate().considerParent(local, parent, context);
    +        }
    +        @Override
    +        public <TContainer, TValue> ReferenceWithError<ConfigValueAtContainer<TContainer,
TValue>> resolveWithParent(ConfigValueAtContainer<TContainer, TValue> local, ConfigValueAtContainer<TContainer,
TValue> resolvedParent, ConfigInheritanceContext context) {
    +            return getDelegate().resolveWithParent(local, resolvedParent, context);
    +        }
    +        @Override
    +        public boolean equals(Object obj) {
    +            return super.equals(obj) || getDelegate().equals(obj);
    +        }
    +        
    +        // standard deserialization method
    +        protected ConfigInheritance readResolve() {
    +            return returnEquivalentConstant(this);
    +        }
    +    }
    +
    +    private static ConfigInheritance returnEquivalentConstant(ConfigInheritance candidate)
{
    +        for (ConfigInheritance knownMode: Arrays.asList(
    +                NOT_REINHERITED, NOT_REINHERITED_ELSE_DEEP_MERGE, NEVER_INHERITED, OVERWRITE,
BasicConfigInheritance.DEEP_MERGE)) {
    +            if (candidate.equals(knownMode)) return knownMode;
    +        }
    +        if (candidate.equals(new BasicConfigInheritance(false, CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,
true, true))) {
    +            // ignore the ancestor flag for this mode
    +            return NEVER_INHERITED;
    +        }
    +        return candidate;
    +    }
    +    
    +    /*
    +     * use of delegate is so that stateless classes can be defined to make the serialization
nice,
    +     * both the name and hiding the implementation detail (also making it easier for
that detail to change);
    +     * with aliased type names the field names here could even be aliases for the type
names.
    +     * (we could alternatively have an "alias-for-final-instance" mode in serialization,
    +     * to optimize where we know that a java instance is final.)   
    +     */
    +    
    +    private static class NotReinherited extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false, true); 
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that a config key value should not be passed down from a container
where it is defined.
          * Unlike {@link #NEVER_INHERITED} these values can be passed down if set as anonymous
keys at a container
          * (ie the container does not expect it) to a container which does expect it, but
it will not be passed down further. 
          * If the inheritor also defines a value the parent's value is ignored irrespective

          * (as in {@link #OVERWRITE}; see {@link #NOT_REINHERITED_ELSE_DEEP_MERGE} if merging
is desired). */
    -    public static BasicConfigInheritance NOT_REINHERITED = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,false);
    +    public static ConfigInheritance NOT_REINHERITED = new NotReinherited();
    +    
    +    private static class NotReinheritedElseDeepMerge extends DelegatingConfigInheritance
{
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE, false, true); 
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** As {@link #NOT_REINHERITED} but in cases where a value is inherited because a
parent did not recognize it,
          * if the inheritor also defines a value the two values should be merged. */
    -    public static BasicConfigInheritance NOT_REINHERITED_ELSE_DEEP_MERGE = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE,false);
    -    /** Indicates that a key's value should never be inherited, even if defined on a
container that does not know the key.
    -     * Most usages will prefer {@link #NOT_REINHERITED}. */
    -    public static BasicConfigInheritance NEVER_INHERITED = new BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,true);
    +    public static ConfigInheritance NOT_REINHERITED_ELSE_DEEP_MERGE = new NotReinheritedElseDeepMerge();
    +    
    +    private static class NeverInherited extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, true, false);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
    +    /** Indicates that a key's value should never be inherited, even if inherited from
a value set on a container that does not know the key.
    +     * (Most usages will prefer {@link #NOT_REINHERITED}.) */
    +    public static ConfigInheritance NEVER_INHERITED = new NeverInherited();
    +    
    +    private static class Overwrite extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(true,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false, true);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that if a key has a value at both an ancestor and a descendant, the
descendant and his descendants
          * will prefer the value at the descendant. */
    -    public static BasicConfigInheritance OVERWRITE = new BasicConfigInheritance(true,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,false);
    +    public static ConfigInheritance OVERWRITE = new Overwrite();
    +    
    +    private static class DeepMerge extends DelegatingConfigInheritance {
    +        final static BasicConfigInheritance DELEGATE = new BasicConfigInheritance(true,
CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE, false, true);
    +        @Override protected ConfigInheritance getDelegate() { return DELEGATE; }
    +    }
         /** Indicates that if a key has a value at both an ancestor and a descendant, the
descendant and his descendants
    -     * should attempt to merge the values. If the values are not mergable behaviour is
undefined
    +     * should attempt to merge the values. If the values are not mergeable behaviour
is undefined
          * (and often the descendant's value will simply overwrite). */
    -    public static BasicConfigInheritance DEEP_MERGE = new BasicConfigInheritance(true,CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE,false);
    +    public static ConfigInheritance DEEP_MERGE = new DeepMerge();
    +
    +    // support conversion from these legacy fields
    +    @SuppressWarnings("deprecation")
    +    private static void registerReplacements() {
    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.DEEP_MERGE, DEEP_MERGE);

    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.ALWAYS, OVERWRITE);

    +        ConfigInheritance.Legacy.registerReplacement(ConfigInheritance.NONE, NOT_REINHERITED);

    +    }
    +    static { registerReplacements(); }
         
    -    /** reinheritable? true/false; if false, children/descendants/inheritors will never
see it; default true */
    +    /** whether a value on a key defined locally should be inheritable by descendants.
    +     * if false at a point where a key is defined, 
    +     * children/descendants/inheritors will not be able to see its value, whether explicit
or default.
    +     * default true:  things are normally reinherited.
    +     * <p>
    +     * note that this only takes effect where a key is defined locally.
    +     * if a key is not defined at an ancestor, a descendant setting this value false
will not prevent it 
    +     * from inheriting values from ancestors.
    +     * <p> 
    +     * typical use case for setting this is false is where a key is consumed and descendants
should not
    +     * "reconsume" it.  for example setting files to install on a VM need only be applied
once,
    +     * and if it has <b>runtime management</b> hierarchy descendants which
also understand that field they
    +     * should not install the same files. 
    +     * (there is normally no reason to set this false in the context of <b>type
hierarchy</b> inheritance because
    +     * an ancestor type cannot "consume" a value.) */
         protected final boolean isReinherited;
    -    /** conflict-resolution-strategy? in {@link BasicConfigInheritance} supported values
are
    +    
    +    /** a symbol indicating a conflict-resolution-strategy understood by the implementation.
    +     * in {@link BasicConfigInheritance} supported values are
          * {@link #CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE} and {@link #CONFLICT_RESOLUTION_STRATEGY_OVERWRITE}.
    -     * subclasses may pass null if they provide a custom implementaton of {@link #resolveWithParentCustomStrategy(ConfigValueAtContainer,
ConfigValueAtContainer, org.apache.brooklyn.config.ConfigInheritance.ConfigInheritanceContext)}
*/
    +     * subclasses may pass null or a different string if they provide a custom implementaton

    --- End diff --
    
    typo `implementaton`


---
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