-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
DefineModelHandler.java
executable file
·128 lines (108 loc) · 4.44 KB
/
DefineModelHandler.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
119
120
121
122
123
124
125
126
127
128
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2022, 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.processor;
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.DefineModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.spi.PropertyDefiner;
import ch.qos.logback.core.util.OptionHelper;
/**
* Instantiate class for define property value. Get future property name and
* property definer class from attributes. Some property definer properties
* could be used. After defining put new property to context.
*
* @author Aleksey Didik
*/
public class DefineModelHandler extends ModelHandlerBase {
boolean inError;
PropertyDefiner definer;
String propertyName;
Scope scope;
public DefineModelHandler(Context context) {
super(context);
}
static public DefineModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
return new DefineModelHandler(context);
}
@Override
protected Class<DefineModel> getSupportedModelClass() {
return DefineModel.class;
}
@Override
public void handle(ModelInterpretationContext interpretationContext, Model model) throws ModelHandlerException {
definer = null;
inError = false;
propertyName = null;
DefineModel defineModel = (DefineModel) model;
propertyName = defineModel.getName();
String scopeStr = defineModel.getScopeStr();
scope = ActionUtil.stringToScope(scopeStr);
if (OptionHelper.isNullOrEmptyOrAllSpaces(propertyName)) {
addError("Missing property name for property definer. Near [" + model.getTag() + "] line "
+ model.getLineNumber());
inError = true;
}
// read property definer class name
String className = defineModel.getClassName();
if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
addError("Missing class name for property definer. Near [" + model.getTag() + "] line "
+ model.getLineNumber());
inError = true;
} else {
className = interpretationContext.getImport(className);
}
if (inError)
return;
// try to instantiate property definer
try {
addInfo("About to instantiate property definer of type [" + className + "]");
definer = (PropertyDefiner) OptionHelper.instantiateByClassName(className, PropertyDefiner.class, context);
definer.setContext(context);
interpretationContext.pushObject(definer);
} catch (Exception oops) {
inError = true;
addError("Could not create an PropertyDefiner of type [" + className + "].", oops);
throw new ModelHandlerException(oops);
}
}
/**
* Now property definer is initialized by all properties and we can put property
* value to context
*/
@Override
public void postHandle(ModelInterpretationContext interpretationContext, Model model) throws ModelHandlerException {
if (inError) {
return;
}
Object o = interpretationContext.peekObject();
if (o != definer) {
addWarn("The object at the of the stack is not the property definer for property named [" + propertyName
+ "] pushed earlier.");
} else {
interpretationContext.popObject();
if (definer instanceof LifeCycle)
((LifeCycle) definer).start();
// let's put defined property and value to context but only if it is
// not null
String propertyValue = definer.getPropertyValue();
if (propertyValue != null) {
addInfo("Setting property " + propertyName + "=" + propertyValue + " in scope " + scope);
ActionUtil.setProperty(interpretationContext, propertyName, propertyValue, scope);
}
}
}
}