/
PropertyModelHandler.java
executable file
·118 lines (98 loc) · 4.97 KB
/
PropertyModelHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ch.qos.logback.core.model.processor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.action.ActionUtil;
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.ModelUtil;
import ch.qos.logback.core.model.PropertyModel;
import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;
public class PropertyModelHandler extends ModelHandlerBase {
public static final String INVALID_ATTRIBUTES = "In <property> element, either the \"file\" attribute alone, or "
+ "the \"resource\" element alone, or both the \"name\" and \"value\" attributes must be set.";
public PropertyModelHandler(Context context) {
super(context);
}
static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
return new PropertyModelHandler(context);
}
@Override
protected Class<PropertyModel> getSupportedModelClass() {
return PropertyModel.class;
}
@Override
public void handle(ModelInterpretationContext interpretationContext, Model model) {
PropertyModel propertyModel = (PropertyModel) model;
Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr());
if (checkFileAttributeSanity(propertyModel)) {
String file = propertyModel.getFile();
file = interpretationContext.subst(file);
try (FileInputStream istream = new FileInputStream(file)) {
loadAndSetProperties(interpretationContext, istream, scope);
} catch (FileNotFoundException e) {
addError("Could not find properties file [" + file + "].");
} catch (IOException|IllegalArgumentException e1) {
addError("Could not read properties file [" + file + "].", e1);
}
} else if (checkResourceAttributeSanity(propertyModel)) {
String resource = propertyModel.getResource();
resource = interpretationContext.subst(resource);
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
if (resourceURL == null) {
addError("Could not find resource [" + resource + "].");
} else {
try ( InputStream istream = resourceURL.openStream();) {
loadAndSetProperties(interpretationContext, istream, scope);
} catch (IOException e) {
addError("Could not read resource file [" + resource + "].", e);
}
}
} else if (checkValueNameAttributesSanity(propertyModel)) {
// earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no
// need to do this. See RegularEscapeUtil.__UNUSED__basicEscape
String value = propertyModel.getValue();
// now remove both leading and trailing spaces
value = value.trim();
value = interpretationContext.subst(value);
ActionUtil.setProperty(interpretationContext, propertyModel.getName(), value, scope);
} else {
addError(INVALID_ATTRIBUTES);
}
}
void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException {
Properties props = new Properties();
props.load(istream);
ModelUtil.setProperties(mic, props, scope);
}
boolean checkFileAttributeSanity(PropertyModel propertyModel) {
String file = propertyModel.getFile();
String name = propertyModel.getName();
String value = propertyModel.getValue();
String resource = propertyModel.getResource();
return !(OptionHelper.isNullOrEmpty(file)) && (OptionHelper.isNullOrEmpty(name)
&& OptionHelper.isNullOrEmpty(value) && OptionHelper.isNullOrEmpty(resource));
}
boolean checkResourceAttributeSanity(PropertyModel propertyModel) {
String file = propertyModel.getFile();
String name = propertyModel.getName();
String value = propertyModel.getValue();
String resource = propertyModel.getResource();
return !(OptionHelper.isNullOrEmpty(resource)) && (OptionHelper.isNullOrEmpty(name)
&& OptionHelper.isNullOrEmpty(value) && OptionHelper.isNullOrEmpty(file));
}
boolean checkValueNameAttributesSanity(PropertyModel propertyModel) {
String file = propertyModel.getFile();
String name = propertyModel.getName();
String value = propertyModel.getValue();
String resource = propertyModel.getResource();
return (!(OptionHelper.isNullOrEmpty(name) || OptionHelper.isNullOrEmpty(value))
&& (OptionHelper.isNullOrEmpty(file) && OptionHelper.isNullOrEmpty(resource)));
}
}