1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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       * Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the
54       * base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}.
55       *
56       * @param capc
57       * @param model
58       * @throws ModelHandlerException
59       * @since 1.5.10
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 }