View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.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       * Key under which a {@link Boolean#TRUE} value is placed in the object map of the
41       * {@link ModelInterpretationContext} when a <propertiesConfigurator> element requests scanning
42       * while the top-level <configuration> element does not.
43       *
44       * <p>Read by {@link ConfigurationModelHandlerFull#postHandle(ModelInterpretationContext, Model)} which is the
45       * only place where a ReconfigureOnChangeTask is scheduled.</p>
46       *
47       * @since 1.6.7
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       * Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the
69       * base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}.
70       *
71       * @param capc
72       * @param model
73       * @throws ModelHandlerException
74       * @since 1.5.10
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          // a local scan attribute, if set, overrides the scan attribute of the top-level <configuration> element
97          boolean watch = (localScan != null) ? localScan : (topScanBoolean == Boolean.TRUE);
98  
99          if (watch) {
100             if (topScanBoolean != Boolean.TRUE) {
101                 // if topScanBoolean is not TRUE, then a ConfigurationWatchList has not been created and registered
102                 // for this configuration run. We need to do so now, but only once per run so that previous
103                 // <propertiesConfigurator> elements are not dropped from the watch list.
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 }