1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.model.processor;
16
17 import ch.qos.logback.classic.joran.PropertiesConfigurator;
18 import ch.qos.logback.classic.model.ConfigurationModel;
19 import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
20 import ch.qos.logback.core.Context;
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.Model;
24 import ch.qos.logback.core.model.ResourceModel;
25 import ch.qos.logback.core.model.processor.ModelHandlerException;
26 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
27 import ch.qos.logback.core.model.processor.ResourceHandlerBase;
28 import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
29 import ch.qos.logback.core.util.OptionHelper;
30
31 import java.io.InputStream;
32 import java.net.URL;
33
34 public class PropertiesConfiguratorModelHandler extends ResourceHandlerBase {
35 boolean inError = false;
36
37 static final boolean CREATE_CWL_IF_NOT_ALREADY_CREATED = true;
38
39
40
41
42
43
44
45
46
47
48
49 public static final String LOCAL_SCAN_REQUESTED_KEY = "PROPERTIES_CONFIGURATOR_LOCAL_SCAN_REQUESTED";
50
51 public PropertiesConfiguratorModelHandler(Context context) {
52 super(context);
53 }
54
55 static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
56 return new PropertiesConfiguratorModelHandler(context);
57 }
58
59 @Override
60 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
61
62 Boolean topScanBoolean = mic.getTopScanBoolean();
63 detachedHandle(mic, model, topScanBoolean);
64 }
65
66
67
68
69
70
71
72
73
74
75
76 public void detachedHandle(ContextAwarePropertyContainer capc, Model model, Boolean topScanBoolean) throws ModelHandlerException {
77
78 PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
79
80 this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
81
82 if (!checkAttributes(propertyConfiguratorModel)) {
83 inError = true;
84 return;
85 }
86
87 URL inputURL = getInputURL(capc, propertyConfiguratorModel);
88 if (inputURL == null) {
89 inError = true;
90 return;
91 }
92
93 String substitutedScanStr = capc.subst(propertyConfiguratorModel.getScanStr());
94 Boolean localScan = OptionHelper.toLopsidedBooleanObject(substitutedScanStr, Boolean.TRUE);
95
96
97 boolean watch = (localScan != null) ? localScan : (topScanBoolean == Boolean.TRUE);
98
99 if (watch) {
100 if (topScanBoolean != Boolean.TRUE) {
101
102
103
104 if (!isLocalScanAlreadyRequested(capc)) {
105 ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(context);
106 markLocalScanRequested(capc);
107 }
108 }
109 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
110 } else if (localScan == Boolean.FALSE && topScanBoolean == Boolean.TRUE) {
111 addInfo("Scan attribute set to false for [" + inputURL + "]. Will not add it to configuration watch list.");
112 }
113
114 InputStream in = openURL(inputURL);
115 if (in == null) {
116 inError = true;
117 return;
118 }
119
120 addInfo("Reading configuration from [" + getAttribureInUse() + "]");
121
122 PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
123 propertiesConfigurator.setContext(capc.getContext());
124 try {
125 propertiesConfigurator.doConfigure(in);
126 } catch (JoranException e) {
127 addError("Could not configure from " + getAttribureInUse());
128 throw new ModelHandlerException(e);
129 }
130
131 }
132
133 private boolean isLocalScanAlreadyRequested(ContextAwarePropertyContainer capc) {
134 if (capc instanceof ModelInterpretationContext) {
135 ModelInterpretationContext mic = (ModelInterpretationContext) capc;
136 return mic.getObjectMap().get(LOCAL_SCAN_REQUESTED_KEY) == Boolean.TRUE;
137 }
138 return false;
139 }
140
141 private void markLocalScanRequested(ContextAwarePropertyContainer capc) {
142 if (capc instanceof ModelInterpretationContext) {
143 ModelInterpretationContext mic = (ModelInterpretationContext) capc;
144 mic.getObjectMap().put(LOCAL_SCAN_REQUESTED_KEY, Boolean.TRUE);
145 }
146 }
147
148 protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
149 URL inputURL = getInputURL(capc, resourceModel);
150 if (inputURL == null)
151 return null;
152
153
154
155 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
156 return openURL(inputURL);
157 }
158
159 }