Skip to content

Commit

Permalink
introduce ContextAwarePropertyContainer, corresponding refactorings
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Feb 27, 2024
1 parent 26b9106 commit a21c6b3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void localPropertiesShouldBeVisible() throws JoranException {
SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT");
StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) sa.getAppenderTracker().find(mdcVal);
assertNotNull(listAppender);

List<String> strList = listAppender.strList;
assertEquals(1, listAppender.strList.size());
assertEquals(prefix + msg, strList.get(0));
Expand Down
54 changes: 13 additions & 41 deletions logback-core/src/main/java/ch/qos/logback/core/model/ModelUtil.java
Original file line number Diff line number Diff line change
@@ -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 {


Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> objectStack;
Stack<Model> modelStack;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -222,7 +225,7 @@ public Map<String, String> getCopyOfPropertyMap() {
return variableSubstitutionsHelper.getCopyOfPropertyMap();
}

// imports
// imports ===================================================================

/**
* Add an import to the importMao
Expand Down Expand Up @@ -262,5 +265,4 @@ public String getImport(String stem) {
else
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> propertiesMap;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}

0 comments on commit a21c6b3

Please sign in to comment.