1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.model.processor;
15
16 import ch.qos.logback.classic.LoggerContext;
17 import ch.qos.logback.classic.model.ConfigurationModel;
18 import ch.qos.logback.core.Context;
19 import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
20 import ch.qos.logback.core.model.Model;
21 import ch.qos.logback.core.model.processor.ModelHandlerBase;
22 import ch.qos.logback.core.model.processor.ModelHandlerException;
23 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
24 import ch.qos.logback.core.status.OnConsoleStatusListener;
25 import ch.qos.logback.core.util.ContextUtil;
26 import ch.qos.logback.core.util.Duration;
27 import ch.qos.logback.core.util.OptionHelper;
28 import ch.qos.logback.core.util.StatusListenerConfigHelper;
29
30 import static ch.qos.logback.core.model.ModelConstants.DEBUG_SYSTEM_PROPERTY_KEY;
31 import static ch.qos.logback.core.model.ModelConstants.NULL_STR;
32 import static java.lang.Boolean.FALSE;
33
34
35
36
37
38
39
40
41
42
43
44 public class ConfigurationModelHandler extends ModelHandlerBase {
45
46 static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1);
47
48 protected Boolean scanning = null;
49
50 public ConfigurationModelHandler(Context context) {
51 super(context);
52 }
53
54 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
55 return new ConfigurationModelHandler(context);
56 }
57
58 protected Class<ConfigurationModel> getSupportedModelClass() {
59 return ConfigurationModel.class;
60 }
61
62 @Override
63 public void handle(ModelInterpretationContext mic, Model model) {
64
65 ConfigurationModel configurationModel = (ConfigurationModel) model;
66
67
68
69
70 String debugAttrib = OptionHelper.getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY, null);
71 if (debugAttrib == null) {
72 debugAttrib = mic.subst(configurationModel.getDebugStr());
73 }
74
75
76 if (!(OptionHelper.isNullOrEmptyOrAllSpaces(debugAttrib) || debugAttrib.equalsIgnoreCase(FALSE.toString())
77 || debugAttrib.equalsIgnoreCase(NULL_STR))) {
78 StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
79 }
80
81
82
83 this.scanning = scanAttrToBoolean(configurationModel);
84
85 mic.setTopScanBoolean(scanning);
86
87 printScanMessage(scanning);
88
89 if (scanning == Boolean.TRUE) {
90 ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(getContext());
91 ConfigurationWatchListUtil.setMainWatchURL(context, mic.getTopURL());
92 }
93
94 LoggerContext lc = (LoggerContext) context;
95 boolean packagingData = OptionHelper.toBoolean(mic.subst(configurationModel.getPackagingDataStr()),
96 LoggerContext.DEFAULT_PACKAGING_DATA);
97 lc.setPackagingDataEnabled(packagingData);
98
99 ContextUtil contextUtil = new ContextUtil(context);
100 contextUtil.addGroovyPackages(lc.getFrameworkPackages());
101
102
103 }
104
105 void printScanMessage(Boolean scanning) {
106 if (scanning == null) {
107 addInfo("Scan attribute not set or set to unrecognized value.");
108 return;
109 }
110 if (scanning) {
111 addInfo("Scan attribute set to true. Will scan for configuration file changes.");
112 } else {
113 addInfo("Scan attribute set to false.");
114 }
115 }
116
117
118 @Override
119 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
120
121
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 private Boolean scanAttrToBoolean(Model model) {
139 if(model instanceof ConfigurationModel) {
140 ConfigurationModel configurationModel = (ConfigurationModel) model;
141 String scanStr = configurationModel.getScanStr();
142 return OptionHelper.toBooleanObject(scanStr);
143 } else {
144 return null;
145 }
146
147 }
148 }