Skip to content

Commit a21c6b3

Browse files
committed
introduce ContextAwarePropertyContainer, corresponding refactorings
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 26b9106 commit a21c6b3

File tree

8 files changed

+103
-58
lines changed

8 files changed

+103
-58
lines changed

logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void localPropertiesShouldBeVisible() throws JoranException {
245245
SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT");
246246
StringListAppender<ILoggingEvent> listAppender = (StringListAppender<ILoggingEvent>) sa.getAppenderTracker().find(mdcVal);
247247
assertNotNull(listAppender);
248-
248+
249249
List<String> strList = listAppender.strList;
250250
assertEquals(1, listAppender.strList.size());
251251
assertEquals(prefix + msg, strList.get(0));
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
114
package ch.qos.logback.core.model;
215

3-
import java.util.Properties;
4-
5-
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
6-
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
7-
import ch.qos.logback.core.util.ContextUtil;
8-
import ch.qos.logback.core.util.OptionHelper;
9-
1016
public class ModelUtil {
1117

1218

@@ -15,39 +21,5 @@ static public void resetForReuse(Model model) {
1521
return;
1622
model.resetForReuse();
1723
}
18-
19-
/**
20-
* Add all the properties found in the argument named 'props' to an
21-
* InterpretationContext.
22-
*/
23-
static public void setProperty(ModelInterpretationContext mic, String key, String value, Scope scope) {
24-
switch (scope) {
25-
case LOCAL:
26-
mic.addSubstitutionProperty(key, value);
27-
break;
28-
case CONTEXT:
29-
mic.getContext().putProperty(key, value);
30-
break;
31-
case SYSTEM:
32-
OptionHelper.setSystemProperty(mic, key, value);
33-
}
34-
}
3524

36-
/**
37-
* Add all the properties found in the argument named 'props' to an
38-
* InterpretationContext.
39-
*/
40-
static public void setProperties(ModelInterpretationContext ic, Properties props, Scope scope) {
41-
switch (scope) {
42-
case LOCAL:
43-
ic.addSubstitutionProperties(props);
44-
break;
45-
case CONTEXT:
46-
ContextUtil cu = new ContextUtil(ic.getContext());
47-
cu.addProperties(props);
48-
break;
49-
case SYSTEM:
50-
OptionHelper.setSystemProperties(ic, props);
51-
}
52-
}
5325
}

logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
88
import ch.qos.logback.core.model.InsertFromJNDIModel;
99
import ch.qos.logback.core.model.Model;
10+
import ch.qos.logback.core.model.util.PropertyModelUtil;
1011
import ch.qos.logback.core.util.JNDIUtil;
1112
import ch.qos.logback.core.util.OptionHelper;
12-
import ch.qos.logback.core.model.ModelUtil;
1313

1414
public class InsertFromJNDIModelHandler extends ModelHandlerBase {
1515

@@ -61,7 +61,7 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
6161
addError("[" + envEntryName + "] has null or empty value");
6262
} else {
6363
addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
64-
ModelUtil.setProperty(mic, asKey, envEntryValue, scope);
64+
PropertyModelUtil.setProperty(mic, asKey, envEntryValue, scope);
6565
}
6666
} catch (NamingException e) {
6767
addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");

logback-core/src/main/java/ch/qos/logback/core/model/processor/ModelInterpretationContext.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
3131
import ch.qos.logback.core.spi.AppenderAttachable;
3232
import ch.qos.logback.core.spi.ContextAwareBase;
33+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
3334
import ch.qos.logback.core.spi.PropertyContainer;
3435

35-
public class ModelInterpretationContext extends ContextAwareBase implements PropertyContainer {
36+
public class ModelInterpretationContext extends ContextAwareBase implements ContextAwarePropertyContainer {
3637

3738
Stack<Object> objectStack;
3839
Stack<Model> modelStack;
@@ -152,13 +153,6 @@ public String subst(String ref) {
152153
return variableSubstitutionsHelper.subst(ref);
153154
}
154155

155-
/**
156-
* Add a property to the properties of this execution context. If the property
157-
* exists already, it is overwritten.
158-
*/
159-
public void addSubstitutionProperty(String key, String value) {
160-
variableSubstitutionsHelper.addSubstitutionProperty(key, value);
161-
}
162156

163157
public DefaultNestedComponentRegistry getDefaultNestedComponentRegistry() {
164158
return defaultNestedComponentRegistry;
@@ -209,6 +203,15 @@ public boolean isNamedDependeeStarted(String name) {
209203

210204
// ========================================== object map
211205

206+
/**
207+
* Add a property to the properties of this execution context. If the property
208+
* exists already, it is overwritten.
209+
*/
210+
@Override
211+
public void addSubstitutionProperty(String key, String value) {
212+
variableSubstitutionsHelper.addSubstitutionProperty(key, value);
213+
}
214+
212215
/**
213216
* If a key is found in propertiesMap then return it. Otherwise, delegate to the
214217
* context.
@@ -222,7 +225,7 @@ public Map<String, String> getCopyOfPropertyMap() {
222225
return variableSubstitutionsHelper.getCopyOfPropertyMap();
223226
}
224227

225-
// imports
228+
// imports ===================================================================
226229

227230
/**
228231
* Add an import to the importMao
@@ -262,5 +265,4 @@ public String getImport(String stem) {
262265
else
263266
return result;
264267
}
265-
266268
}

logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
1313
import ch.qos.logback.core.model.Model;
1414
import ch.qos.logback.core.model.ModelConstants;
15-
import ch.qos.logback.core.model.ModelUtil;
1615
import ch.qos.logback.core.model.PropertyModel;
1716
import ch.qos.logback.core.model.util.PropertyModelUtil;
1817
import ch.qos.logback.core.util.Loader;
@@ -81,7 +80,7 @@ public void handle(ModelInterpretationContext interpretationContext, Model model
8180
void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException {
8281
Properties props = new Properties();
8382
props.load(istream);
84-
ModelUtil.setProperties(mic, props, scope);
83+
PropertyModelUtil.setProperties(mic, props, scope);
8584
}
8685

8786
}

logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java

+47
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@
1414

1515
package ch.qos.logback.core.model.util;
1616

17+
import ch.qos.logback.core.joran.action.ActionUtil;
1718
import ch.qos.logback.core.model.PropertyModel;
19+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
20+
import ch.qos.logback.core.util.ContextUtil;
1821
import ch.qos.logback.core.util.OptionHelper;
1922

23+
import java.util.Properties;
24+
25+
/**
26+
*
27+
*
28+
* @since 1.5.1
29+
*/
2030
public class PropertyModelUtil {
31+
32+
2133
public static boolean checkFileAttributeSanity(PropertyModel propertyModel) {
2234
String file = propertyModel.getFile();
2335
String name = propertyModel.getName();
@@ -46,4 +58,39 @@ public static boolean checkValueNameAttributesSanity(PropertyModel propertyModel
4658
return (!(OptionHelper.isNullOrEmptyOrAllSpaces(name) || OptionHelper.isNullOrEmptyOrAllSpaces(value))
4759
&& (OptionHelper.isNullOrEmptyOrAllSpaces(file) && OptionHelper.isNullOrEmptyOrAllSpaces(resource)));
4860
}
61+
62+
/**
63+
* Add all the properties found in the argument named 'props' to an
64+
* InterpretationContext.
65+
*/
66+
static public void setProperty(ContextAwarePropertyContainer capc, String key, String value, ActionUtil.Scope scope) {
67+
switch (scope) {
68+
case LOCAL:
69+
capc.addSubstitutionProperty(key, value);
70+
break;
71+
case CONTEXT:
72+
capc.getContext().putProperty(key, value);
73+
break;
74+
case SYSTEM:
75+
OptionHelper.setSystemProperty(capc, key, value);
76+
}
77+
}
78+
79+
/**
80+
* Add all the properties found in the argument named 'props' to an
81+
* InterpretationContext.
82+
*/
83+
static public void setProperties(ContextAwarePropertyContainer capc, Properties props, ActionUtil.Scope scope) {
84+
switch (scope) {
85+
case LOCAL:
86+
capc.addSubstitutionProperties(props);
87+
break;
88+
case CONTEXT:
89+
ContextUtil cu = new ContextUtil(capc.getContext());
90+
cu.addProperties(props);
91+
break;
92+
case SYSTEM:
93+
OptionHelper.setSystemProperties(capc, props);
94+
}
95+
}
4996
}

logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import ch.qos.logback.core.Context;
1818
import ch.qos.logback.core.spi.ContextAwareBase;
19+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
1920
import ch.qos.logback.core.spi.PropertyContainer;
2021
import ch.qos.logback.core.spi.ScanException;
2122
import ch.qos.logback.core.util.OptionHelper;
@@ -25,11 +26,11 @@
2526
import java.util.Properties;
2627

2728
/**
28-
* Helper methods to deal with properties/
29+
* Helper methods to deal with properties.
2930
*
3031
* @since 1.5.1
3132
*/
32-
public class VariableSubstitutionsHelper extends ContextAwareBase implements PropertyContainer {
33+
public class VariableSubstitutionsHelper extends ContextAwareBase implements ContextAwarePropertyContainer {
3334

3435
protected Map<String, String> propertiesMap;
3536

@@ -61,6 +62,7 @@ public String subst(String ref) {
6162
* Add a property to the properties of this execution context. If the property
6263
* exists already, it is overwritten.
6364
*/
65+
@Override
6466
public void addSubstitutionProperty(String key, String value) {
6567
if (key == null || value == null) {
6668
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.core.spi;
16+
17+
/**
18+
* An interface extending both {@link PropertyContainer} and {@link ContextAware}
19+
*
20+
* @since 1.5.1
21+
*/
22+
public interface ContextAwarePropertyContainer extends PropertyContainer, ContextAware {
23+
}

0 commit comments

Comments
 (0)