Github user aledsage commented on a diff in the pull request: https://github.com/apache/brooklyn-server/pull/462#discussion_r100509692 --- 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 --- End diff -- @geomacy I think this PR is big enough already, and taking too long to get merged. I agree we should look at places we declare that "behaviour is undefined" - some of those should be forbidden (i.e. fail-fast, until we decide to actually support it), others should have clearly defined semantics, and only rarely is "undefined" a reasonable behaviour.
--- 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. ---