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 public PropertiesConfiguratorModelHandler(Context context) {
40 super(context);
41 }
42
43 static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
44 return new PropertiesConfiguratorModelHandler(context);
45 }
46
47 @Override
48 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
49
50 Boolean topScanBoolean = mic.getTopScanBoolean();
51 detachedHandle(mic, model, topScanBoolean);
52 }
53
54
55
56
57
58
59
60
61
62
63
64 public void detachedHandle(ContextAwarePropertyContainer capc, Model model, Boolean topScanBoolean) throws ModelHandlerException {
65
66 PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
67
68 this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
69
70 if (!checkAttributes(propertyConfiguratorModel)) {
71 inError = true;
72 return;
73 }
74
75 URL inputURL = getInputURL(capc, propertyConfiguratorModel);
76 if (inputURL == null) {
77 inError = true;
78 return;
79 }
80
81
82 Boolean localScan = OptionHelper.toBooleanObject(propertyConfiguratorModel.getScanStr());
83
84 InputStream in = openURL(inputURL);
85 if (in == null) {
86 inError = true;
87 return;
88 }
89
90 if(localScan == Boolean.TRUE || topScanBoolean == Boolean.TRUE) {
91 if(topScanBoolean != Boolean.TRUE) {
92
93
94 ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(context);
95 }
96 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
97 }
98
99
100
101 addInfo("Reading configuration from [" + getAttribureInUse() + "]");
102
103 PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
104 propertiesConfigurator.setContext(capc.getContext());
105 try {
106 propertiesConfigurator.doConfigure(in);
107 } catch (JoranException e) {
108 addError("Could not configure from " + getAttribureInUse());
109 throw new ModelHandlerException(e);
110 }
111
112 }
113
114 protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
115 URL inputURL = getInputURL(capc, resourceModel);
116 if (inputURL == null)
117 return null;
118
119
120
121 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
122 return openURL(inputURL);
123 }
124
125 }