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.PropertiesConfiguratorModel;
19 import ch.qos.logback.core.Context;
20 import ch.qos.logback.core.joran.spi.JoranException;
21 import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
22 import ch.qos.logback.core.model.Model;
23 import ch.qos.logback.core.model.ResourceModel;
24 import ch.qos.logback.core.model.processor.ModelHandlerException;
25 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
26 import ch.qos.logback.core.model.processor.ResourceHandlerBase;
27 import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
28 import ch.qos.logback.core.util.OptionHelper;
29
30 import java.io.InputStream;
31 import java.net.URL;
32
33 public class PropertiesConfiguratorModelHandler extends ResourceHandlerBase {
34 boolean inError = false;
35
36 static final boolean CREATE_CWL_IF_NOT_ALREADY_CREATED = true;
37
38 public PropertiesConfiguratorModelHandler(Context context) {
39 super(context);
40 }
41
42 static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
43 return new PropertiesConfiguratorModelHandler(context);
44 }
45
46 @Override
47 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
48 detachedHandle(mic, model);
49 }
50
51
52
53
54
55
56
57
58
59
60
61 public void detachedHandle(ContextAwarePropertyContainer capc, Model model) throws ModelHandlerException {
62
63 PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
64
65 this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
66
67 if (!checkAttributes(propertyConfiguratorModel)) {
68 inError = true;
69 return;
70 }
71
72 InputStream in = getInputStream(capc, propertyConfiguratorModel);
73 if (in == null) {
74 inError = true;
75 return;
76 }
77
78 addInfo("Reading configuration from [" + getAttribureInUse() + "]");
79
80 PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
81 propertiesConfigurator.setContext(capc.getContext());
82 try {
83 propertiesConfigurator.doConfigure(in);
84 } catch (JoranException e) {
85 addError("Could not configure from " + getAttribureInUse());
86 throw new ModelHandlerException(e);
87 }
88
89 }
90
91 protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
92 URL inputURL = getInputURL(capc, resourceModel);
93 if (inputURL == null)
94 return null;
95
96 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
97 return openURL(inputURL);
98 }
99
100 }