Skip to content

Commit 8f3a304

Browse files
committed
more fine tuning of propertyModelHandler and co
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent a21c6b3 commit 8f3a304

File tree

11 files changed

+251
-174
lines changed

11 files changed

+251
-174
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java

+24-17
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,37 @@
1717
import ch.qos.logback.classic.Level;
1818
import ch.qos.logback.classic.Logger;
1919
import ch.qos.logback.classic.LoggerContext;
20+
import ch.qos.logback.classic.util.LevelUtil;
21+
import ch.qos.logback.core.Context;
22+
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
2023
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
2124
import ch.qos.logback.core.spi.ContextAwareBase;
22-
import ch.qos.logback.core.spi.PropertyContainer;
23-
import ch.qos.logback.core.spi.ScanException;
25+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
2426
import ch.qos.logback.core.status.OnConsoleStatusListener;
2527
import ch.qos.logback.core.util.OptionHelper;
2628
import ch.qos.logback.core.util.StatusListenerConfigHelper;
2729
import ch.qos.logback.core.util.StringUtil;
2830

29-
import java.util.HashMap;
3031
import java.util.Map;
3132

32-
public class TylerConfiguratorBase extends ContextAwareBase implements PropertyContainer {
33+
public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {
3334

34-
public static final String SET_CONTEXT_NAME = "setContextName";
35+
public static final String SET_CONTEXT_METHOD_NAME = "setContext";
36+
public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName";
3537
public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger";
38+
public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper";
39+
public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper";
3640

37-
VariableSubstitutionsHelper variableSubstitutionsHelper;
41+
// initialized via #setContext
42+
protected VariableSubstitutionsHelper variableSubstitutionsHelper;
43+
// context set in #setContext
44+
protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
3845

39-
private Logger setupLogger(String loggerName, Level level, String levelString, Boolean additivity) {
46+
protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) {
4047
LoggerContext loggerContext = (LoggerContext) context;
4148
Logger logger = loggerContext.getLogger(loggerName);
4249
if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) {
50+
Level level = LevelUtil.levelStringToLevel(levelString);
4351
logger.setLevel(level);
4452
}
4553
if (additivity != null) {
@@ -48,6 +56,13 @@ private Logger setupLogger(String loggerName, Level level, String levelString, B
4856
return logger;
4957
}
5058

59+
@Override
60+
public void setContext(Context context) {
61+
super.setContext(context);
62+
variableSubstitutionsHelper = new VariableSubstitutionsHelper(context);
63+
propertyModelHandlerHelper.setContext(context);
64+
}
65+
5166
protected void setContextName(String name) {
5267
if(StringUtil.isNullOrEmpty(name)) {
5368
addError("Cannot set context name to null or empty string");
@@ -72,17 +87,9 @@ protected void addOnConsoleStatusListener() {
7287
* @param ref
7388
* @return
7489
*/
90+
@Override
7591
public String subst(String ref) {
76-
if (ref == null) {
77-
return null;
78-
}
79-
80-
try {
81-
return OptionHelper.substVars(ref, this, context);
82-
} catch (ScanException | IllegalArgumentException e) {
83-
addError("Problem while parsing [" + ref + "]", e);
84-
return ref;
85-
}
92+
return variableSubstitutionsHelper.subst(ref);
8693
}
8794

8895
@Override

logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import ch.qos.logback.core.joran.action.ActionUtil;
1919
import ch.qos.logback.core.model.ModelConstants;
2020
import ch.qos.logback.core.model.PropertyModel;
21-
import ch.qos.logback.core.model.util.PropertyModelUtil;
21+
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
2222
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
2323
import ch.qos.logback.core.spi.ContextAwareBase;
2424
import ch.qos.logback.core.util.ContextUtil;
@@ -47,7 +47,7 @@ public class VariableModelHelper extends ContextAwareBase {
4747
void updateProperties(PropertyModel propertyModel) {
4848

4949
ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr());
50-
if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) {
50+
if (PropertyModelHandlerHelper.checkFileAttributeSanity(propertyModel)) {
5151
String file = propertyModel.getFile();
5252
file = tylerConfiguratorBase.subst(file);
5353
try (FileInputStream istream = new FileInputStream(file)) {
@@ -58,7 +58,7 @@ void updateProperties(PropertyModel propertyModel) {
5858
// is badly malformed, i.e a binary.
5959
addError("Could not read properties file [" + file + "].", e1);
6060
}
61-
} else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) {
61+
} else if (PropertyModelHandlerHelper.checkResourceAttributeSanity(propertyModel)) {
6262
String resource = propertyModel.getResource();
6363
resource = tylerConfiguratorBase.subst(resource);
6464
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
@@ -71,7 +71,7 @@ void updateProperties(PropertyModel propertyModel) {
7171
addError("Could not read resource file [" + resource + "].", e);
7272
}
7373
}
74-
} else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) {
74+
} else if (PropertyModelHandlerHelper.checkValueNameAttributesSanity(propertyModel)) {
7575
// earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no
7676
// need to do this. See RegularEscapeUtil.__UNUSED__basicEscape
7777
String value = propertyModel.getValue();

logback-core/src/main/java/ch/qos/logback/core/joran/action/ActionUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package ch.qos.logback.core.joran.action;
1515

1616
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
17+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
1718
import ch.qos.logback.core.util.OptionHelper;
1819

1920
public class ActionUtil {
@@ -50,7 +51,7 @@ static public Scope stringToScope(String scopeStr) {
5051
// }
5152
// }
5253

53-
static public void setProperty(ModelInterpretationContext ic, String key, String value, Scope scope) {
54+
static public void setProperty(ContextAwarePropertyContainer ic, String key, String value, Scope scope) {
5455
switch (scope) {
5556
case LOCAL:
5657
ic.addSubstitutionProperty(key, value);

logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected Model buildCurrentModel(SaxEventInterpretationContext interpretationCo
4444
PropertyModel propertyModel = new PropertyModel();
4545
propertyModel.setName(attributes.getValue(NAME_ATTRIBUTE));
4646
propertyModel.setValue(attributes.getValue(VALUE_ATTRIBUTE));
47-
propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE));
4847
propertyModel.setFile(attributes.getValue(FILE_ATTRIBUTE));
4948
propertyModel.setResource(attributes.getValue(RESOURCE_ATTRIBUTE));
49+
propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE));
5050
return propertyModel;
5151
}
5252

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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;
10+
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
1111
import ch.qos.logback.core.util.JNDIUtil;
1212
import ch.qos.logback.core.util.OptionHelper;
1313

@@ -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-
PropertyModelUtil.setProperty(mic, asKey, envEntryValue, scope);
64+
PropertyModelHandlerHelper.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/PropertyModelHandler.java

+5-49
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.net.URL;
8-
import java.util.Properties;
98

109
import ch.qos.logback.core.Context;
1110
import ch.qos.logback.core.joran.action.ActionUtil;
1211
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
1312
import ch.qos.logback.core.model.Model;
1413
import ch.qos.logback.core.model.ModelConstants;
1514
import ch.qos.logback.core.model.PropertyModel;
16-
import ch.qos.logback.core.model.util.PropertyModelUtil;
15+
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
1716
import ch.qos.logback.core.util.Loader;
1817

1918
public class PropertyModelHandler extends ModelHandlerBase {
@@ -32,55 +31,12 @@ protected Class<PropertyModel> getSupportedModelClass() {
3231
}
3332

3433
@Override
35-
public void handle(ModelInterpretationContext interpretationContext, Model model) {
34+
public void handle(ModelInterpretationContext mic, Model model) {
3635

3736
PropertyModel propertyModel = (PropertyModel) model;
38-
39-
Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr());
40-
41-
if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) {
42-
String file = propertyModel.getFile();
43-
file = interpretationContext.subst(file);
44-
try (FileInputStream istream = new FileInputStream(file)) {
45-
loadAndSetProperties(interpretationContext, istream, scope);
46-
} catch (FileNotFoundException e) {
47-
addError("Could not find properties file [" + file + "].");
48-
} catch (IOException|IllegalArgumentException e1) { // IllegalArgumentException is thrown in case the file
49-
// is badly malformed, i.e a binary.
50-
addError("Could not read properties file [" + file + "].", e1);
51-
}
52-
} else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) {
53-
String resource = propertyModel.getResource();
54-
resource = interpretationContext.subst(resource);
55-
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
56-
if (resourceURL == null) {
57-
addError("Could not find resource [" + resource + "].");
58-
} else {
59-
try ( InputStream istream = resourceURL.openStream();) {
60-
loadAndSetProperties(interpretationContext, istream, scope);
61-
} catch (IOException e) {
62-
addError("Could not read resource file [" + resource + "].", e);
63-
}
64-
}
65-
} else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) {
66-
// earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no
67-
// need to do this. See RegularEscapeUtil.__UNUSED__basicEscape
68-
String value = propertyModel.getValue();
69-
70-
// now remove both leading and trailing spaces
71-
value = value.trim();
72-
value = interpretationContext.subst(value);
73-
ActionUtil.setProperty(interpretationContext, propertyModel.getName(), value, scope);
74-
75-
} else {
76-
addError(ModelConstants.INVALID_ATTRIBUTES);
77-
}
78-
}
79-
80-
void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException {
81-
Properties props = new Properties();
82-
props.load(istream);
83-
PropertyModelUtil.setProperties(mic, props, scope);
37+
PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
38+
propertyModelHandlerHelper.setContext(context);
39+
propertyModelHandlerHelper.handlePropertyModel(mic, propertyModel);
8440
}
8541

8642
}

0 commit comments

Comments
 (0)