From a21c6b3593463c3a9144bd37ada7ca335e848ad8 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Tue, 27 Feb 2024 11:47:42 +0100 Subject: [PATCH] introduce ContextAwarePropertyContainer, corresponding refactorings Signed-off-by: Ceki Gulcu --- .../classic/sift/SiftingAppenderTest.java | 2 +- .../ch/qos/logback/core/model/ModelUtil.java | 54 +++++-------------- .../processor/InsertFromJNDIModelHandler.java | 4 +- .../processor/ModelInterpretationContext.java | 22 ++++---- .../model/processor/PropertyModelHandler.java | 3 +- .../core/model/util/PropertyModelUtil.java | 47 ++++++++++++++++ .../util/VariableSubstitutionsHelper.java | 6 ++- .../spi/ContextAwarePropertyContainer.java | 23 ++++++++ 8 files changed, 103 insertions(+), 58 deletions(-) create mode 100644 logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java index bc7a59f87a..f03b22b2c8 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java @@ -245,7 +245,7 @@ public void localPropertiesShouldBeVisible() throws JoranException { SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT"); StringListAppender listAppender = (StringListAppender) sa.getAppenderTracker().find(mdcVal); assertNotNull(listAppender); - + List strList = listAppender.strList; assertEquals(1, listAppender.strList.size()); assertEquals(prefix + msg, strList.get(0)); diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/ModelUtil.java b/logback-core/src/main/java/ch/qos/logback/core/model/ModelUtil.java index 318510487c..22970ddff5 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/ModelUtil.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/ModelUtil.java @@ -1,12 +1,18 @@ +/* + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2024, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ package ch.qos.logback.core.model; -import java.util.Properties; - -import ch.qos.logback.core.joran.action.ActionUtil.Scope; -import ch.qos.logback.core.model.processor.ModelInterpretationContext; -import ch.qos.logback.core.util.ContextUtil; -import ch.qos.logback.core.util.OptionHelper; - public class ModelUtil { @@ -15,39 +21,5 @@ static public void resetForReuse(Model model) { return; model.resetForReuse(); } - - /** - * Add all the properties found in the argument named 'props' to an - * InterpretationContext. - */ - static public void setProperty(ModelInterpretationContext mic, String key, String value, Scope scope) { - switch (scope) { - case LOCAL: - mic.addSubstitutionProperty(key, value); - break; - case CONTEXT: - mic.getContext().putProperty(key, value); - break; - case SYSTEM: - OptionHelper.setSystemProperty(mic, key, value); - } - } - /** - * Add all the properties found in the argument named 'props' to an - * InterpretationContext. - */ - static public void setProperties(ModelInterpretationContext ic, Properties props, Scope scope) { - switch (scope) { - case LOCAL: - ic.addSubstitutionProperties(props); - break; - case CONTEXT: - ContextUtil cu = new ContextUtil(ic.getContext()); - cu.addProperties(props); - break; - case SYSTEM: - OptionHelper.setSystemProperties(ic, props); - } - } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java index 446637030e..d82b0c7551 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java @@ -7,9 +7,9 @@ import ch.qos.logback.core.joran.action.ActionUtil.Scope; import ch.qos.logback.core.model.InsertFromJNDIModel; import ch.qos.logback.core.model.Model; +import ch.qos.logback.core.model.util.PropertyModelUtil; import ch.qos.logback.core.util.JNDIUtil; import ch.qos.logback.core.util.OptionHelper; -import ch.qos.logback.core.model.ModelUtil; public class InsertFromJNDIModelHandler extends ModelHandlerBase { @@ -61,7 +61,7 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand addError("[" + envEntryName + "] has null or empty value"); } else { addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope"); - ModelUtil.setProperty(mic, asKey, envEntryValue, scope); + PropertyModelUtil.setProperty(mic, asKey, envEntryValue, scope); } } catch (NamingException e) { addError("Failed to lookup JNDI env-entry [" + envEntryName + "]"); diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java index 30eadb6c01..5e52e8d523 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java @@ -30,9 +30,10 @@ import ch.qos.logback.core.model.util.VariableSubstitutionsHelper; import ch.qos.logback.core.spi.AppenderAttachable; import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; import ch.qos.logback.core.spi.PropertyContainer; -public class ModelInterpretationContext extends ContextAwareBase implements PropertyContainer { +public class ModelInterpretationContext extends ContextAwareBase implements ContextAwarePropertyContainer { Stack objectStack; Stack modelStack; @@ -152,13 +153,6 @@ public String subst(String ref) { return variableSubstitutionsHelper.subst(ref); } - /** - * Add a property to the properties of this execution context. If the property - * exists already, it is overwritten. - */ - public void addSubstitutionProperty(String key, String value) { - variableSubstitutionsHelper.addSubstitutionProperty(key, value); - } public DefaultNestedComponentRegistry getDefaultNestedComponentRegistry() { return defaultNestedComponentRegistry; @@ -209,6 +203,15 @@ public boolean isNamedDependeeStarted(String name) { // ========================================== object map + /** + * Add a property to the properties of this execution context. If the property + * exists already, it is overwritten. + */ + @Override + public void addSubstitutionProperty(String key, String value) { + variableSubstitutionsHelper.addSubstitutionProperty(key, value); + } + /** * If a key is found in propertiesMap then return it. Otherwise, delegate to the * context. @@ -222,7 +225,7 @@ public Map getCopyOfPropertyMap() { return variableSubstitutionsHelper.getCopyOfPropertyMap(); } - // imports + // imports =================================================================== /** * Add an import to the importMao @@ -262,5 +265,4 @@ public String getImport(String stem) { else return result; } - } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java index ab77ad7dcd..2877320568 100755 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java @@ -12,7 +12,6 @@ import ch.qos.logback.core.joran.action.ActionUtil.Scope; import ch.qos.logback.core.model.Model; import ch.qos.logback.core.model.ModelConstants; -import ch.qos.logback.core.model.ModelUtil; import ch.qos.logback.core.model.PropertyModel; import ch.qos.logback.core.model.util.PropertyModelUtil; import ch.qos.logback.core.util.Loader; @@ -81,7 +80,7 @@ public void handle(ModelInterpretationContext interpretationContext, Model model void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException { Properties props = new Properties(); props.load(istream); - ModelUtil.setProperties(mic, props, scope); + PropertyModelUtil.setProperties(mic, props, scope); } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java b/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java index 14ba246cfe..5c4328c882 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java @@ -14,10 +14,22 @@ package ch.qos.logback.core.model.util; +import ch.qos.logback.core.joran.action.ActionUtil; import ch.qos.logback.core.model.PropertyModel; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; +import ch.qos.logback.core.util.ContextUtil; import ch.qos.logback.core.util.OptionHelper; +import java.util.Properties; + +/** + * + * + * @since 1.5.1 + */ public class PropertyModelUtil { + + public static boolean checkFileAttributeSanity(PropertyModel propertyModel) { String file = propertyModel.getFile(); String name = propertyModel.getName(); @@ -46,4 +58,39 @@ public static boolean checkValueNameAttributesSanity(PropertyModel propertyModel return (!(OptionHelper.isNullOrEmptyOrAllSpaces(name) || OptionHelper.isNullOrEmptyOrAllSpaces(value)) && (OptionHelper.isNullOrEmptyOrAllSpaces(file) && OptionHelper.isNullOrEmptyOrAllSpaces(resource))); } + + /** + * Add all the properties found in the argument named 'props' to an + * InterpretationContext. + */ + static public void setProperty(ContextAwarePropertyContainer capc, String key, String value, ActionUtil.Scope scope) { + switch (scope) { + case LOCAL: + capc.addSubstitutionProperty(key, value); + break; + case CONTEXT: + capc.getContext().putProperty(key, value); + break; + case SYSTEM: + OptionHelper.setSystemProperty(capc, key, value); + } + } + + /** + * Add all the properties found in the argument named 'props' to an + * InterpretationContext. + */ + static public void setProperties(ContextAwarePropertyContainer capc, Properties props, ActionUtil.Scope scope) { + switch (scope) { + case LOCAL: + capc.addSubstitutionProperties(props); + break; + case CONTEXT: + ContextUtil cu = new ContextUtil(capc.getContext()); + cu.addProperties(props); + break; + case SYSTEM: + OptionHelper.setSystemProperties(capc, props); + } + } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java b/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java index dd0eef0536..cca9beb3e1 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java @@ -16,6 +16,7 @@ import ch.qos.logback.core.Context; import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; import ch.qos.logback.core.spi.PropertyContainer; import ch.qos.logback.core.spi.ScanException; import ch.qos.logback.core.util.OptionHelper; @@ -25,11 +26,11 @@ import java.util.Properties; /** - * Helper methods to deal with properties/ + * Helper methods to deal with properties. * * @since 1.5.1 */ -public class VariableSubstitutionsHelper extends ContextAwareBase implements PropertyContainer { +public class VariableSubstitutionsHelper extends ContextAwareBase implements ContextAwarePropertyContainer { protected Map propertiesMap; @@ -61,6 +62,7 @@ public String subst(String ref) { * Add a property to the properties of this execution context. If the property * exists already, it is overwritten. */ + @Override public void addSubstitutionProperty(String key, String value) { if (key == null || value == null) { return; diff --git a/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java b/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java new file mode 100644 index 0000000000..6590361561 --- /dev/null +++ b/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java @@ -0,0 +1,23 @@ +/* + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2024, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ + +package ch.qos.logback.core.spi; + +/** + * An interface extending both {@link PropertyContainer} and {@link ContextAware} + * + * @since 1.5.1 + */ +public interface ContextAwarePropertyContainer extends PropertyContainer, ContextAware { +}