1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.core.model.processor;
16
17 import ch.qos.logback.core.Context;
18 import ch.qos.logback.core.joran.GenericXMLConfigurator;
19 import ch.qos.logback.core.joran.event.SaxEvent;
20 import ch.qos.logback.core.joran.event.SaxEventRecorder;
21 import ch.qos.logback.core.joran.spi.JoranException;
22 import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
23 import ch.qos.logback.core.model.IncludeModel;
24 import ch.qos.logback.core.model.Model;
25 import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
26 import ch.qos.logback.core.spi.ErrorCodes;
27 import ch.qos.logback.core.util.Loader;
28 import ch.qos.logback.core.util.OptionHelper;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.MalformedURLException;
34 import java.net.URI;
35 import java.net.URL;
36 import java.util.List;
37 import java.util.function.Supplier;
38
39 import static ch.qos.logback.core.joran.JoranConstants.CONFIGURATION_TAG;
40 import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;
41
42
43
44
45 public class IncludeModelHandler extends ResourceHandlerBase {
46 boolean inError = false;
47
48 public IncludeModelHandler(Context context) {
49 super(context);
50 }
51
52 static public IncludeModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
53 return new IncludeModelHandler(context);
54 }
55
56 @Override
57 protected Class<IncludeModel> getSupportedModelClass() {
58 return IncludeModel.class;
59 }
60
61 @Override
62 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
63 IncludeModel includeModel = (IncludeModel) model;
64
65 URL topURL = mic.getTopURL();
66 Boolean topScan = mic.getTopScanBoolean();
67 Model modelFromIncludedFile = buildModelFromIncludedFile(mic, topURL, topScan, includeModel);
68 if (modelFromIncludedFile == null) {
69 warnIfRequired("Failed to build include model from included file");
70 return;
71 }
72 processModelFromIncludedFile(includeModel, modelFromIncludedFile);
73 }
74
75
76
77
78
79
80
81
82
83 public Model buildModelFromIncludedFile(ContextAwarePropertyContainer capc, URL topURL, Boolean topScan, IncludeModel includeModel) throws ModelHandlerException {
84
85 this.optional = OptionHelper.toBoolean(includeModel.getOptional(), false);
86
87 if (!checkAttributes(includeModel)) {
88 inError = true;
89 return null;
90 }
91
92
93 URL inputURL = getInputURL(capc, includeModel);
94 if (inputURL == null) {
95 inError = true;
96 return null;
97 }
98
99
100
101 updateConfigurationWatchList(inputURL, topURL, topScan);
102
103 InputStream in = openURL(inputURL);
104 if (in == null) {
105 inError = true;
106 return null;
107 }
108
109 addInfo("Including configuration file [" + inputURL + "]");
110
111 SaxEventRecorder recorder = null;
112
113 try {
114 recorder = populateSaxEventRecorder(in);
115
116 List<SaxEvent> saxEvents = recorder.getSaxEventList();
117 if (saxEvents.isEmpty()) {
118 addWarn("Empty sax event list");
119 return null;
120 }
121
122 Supplier<? extends GenericXMLConfigurator> jcSupplier = capc.getConfiguratorSupplier();
123 if (jcSupplier == null) {
124 addError("null configurator supplier. Abandoning inclusion of [" + attributeInUse + "]");
125 inError = true;
126 return null;
127 }
128
129 GenericXMLConfigurator genericXMLConfigurator = jcSupplier.get();
130 genericXMLConfigurator.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG);
131
132 Model modelFromIncludedFile = genericXMLConfigurator.buildModelFromSaxEventList(recorder.getSaxEventList());
133 return modelFromIncludedFile;
134 } catch (JoranException e) {
135 inError = true;
136 addError("Error processing XML data in [" + attributeInUse + "]", e);
137 return null;
138 }
139 }
140
141 private void processModelFromIncludedFile(IncludeModel includeModel, Model modelFromIncludedFile) {
142 includeModel.getSubModels().addAll(modelFromIncludedFile.getSubModels());
143 }
144
145 public SaxEventRecorder populateSaxEventRecorder(final InputStream inputStream) throws JoranException {
146 SaxEventRecorder recorder = new SaxEventRecorder(context);
147 recorder.recordEvents(inputStream);
148 return recorder;
149 }
150
151 private void updateConfigurationWatchList(URL inputURL, URL topURL, Boolean topScan) throws ModelHandlerException {
152 if(topScan == Boolean.TRUE) {
153 if(topURL != null) {
154 ConfigurationWatchListUtil.addToWatchList(context, inputURL);
155 } else {
156 addWarn("No top URL for XML configuration file. Will not add [" + inputURL + "] to watch list.");
157 }
158 }
159
160 }
161
162 }