View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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  package ch.qos.logback.classic.joran.action;
15  
16  import java.net.URL;
17  import java.util.concurrent.ScheduledExecutorService;
18  import java.util.concurrent.ScheduledFuture;
19  import java.util.concurrent.TimeUnit;
20  
21  import org.xml.sax.Attributes;
22  
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.joran.ReconfigureOnChangeTask;
25  import ch.qos.logback.classic.util.EnvUtil;
26  import ch.qos.logback.core.CoreConstants;
27  import ch.qos.logback.core.joran.action.Action;
28  import ch.qos.logback.core.joran.spi.InterpretationContext;
29  import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
30  import ch.qos.logback.core.status.OnConsoleStatusListener;
31  import ch.qos.logback.core.util.ContextUtil;
32  import ch.qos.logback.core.util.Duration;
33  import ch.qos.logback.core.util.OptionHelper;
34  import ch.qos.logback.core.util.StatusListenerConfigHelper;
35  
36  public class ConfigurationAction extends Action {
37      static final String INTERNAL_DEBUG_ATTR = "debug";
38      static final String PACKAGING_DATA_ATTR = "packagingData";
39      static final String SCAN_ATTR = "scan";
40      static final String SCAN_PERIOD_ATTR = "scanPeriod";
41      static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback.debug";
42      static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1);
43      
44      long threshold = 0;
45  
46      public void begin(InterpretationContext ic, String name, Attributes attributes) {
47          threshold = System.currentTimeMillis();
48  
49          // See LOGBACK-527 (the system property is looked up first. Thus, it overrides
50          // the equivalent property in the config file. This reversal of scope priority is justified
51          // by the use case: the admin trying to chase rogue config file
52          String debugAttrib = getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY);
53          if (debugAttrib == null) {
54              debugAttrib = ic.subst(attributes.getValue(INTERNAL_DEBUG_ATTR));
55          }
56  
57          if (OptionHelper.isEmpty(debugAttrib) || debugAttrib.equalsIgnoreCase("false") || debugAttrib.equalsIgnoreCase("null")) {
58              addInfo(INTERNAL_DEBUG_ATTR + " attribute not set");
59          } else {
60              StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
61          }
62  
63          processScanAttrib(ic, attributes);
64  
65          LoggerContext lc = (LoggerContext) context;
66          boolean packagingData = OptionHelper.toBoolean(ic.subst(attributes.getValue(PACKAGING_DATA_ATTR)), LoggerContext.DEFAULT_PACKAGING_DATA);
67          lc.setPackagingDataEnabled(packagingData);
68  
69          if (EnvUtil.isGroovyAvailable()) {
70              ContextUtil contextUtil = new ContextUtil(context);
71              contextUtil.addGroovyPackages(lc.getFrameworkPackages());
72          }
73  
74          // the context is turbo filter attachable, so it is pushed on top of the
75          // stack
76          ic.pushObject(getContext());
77      }
78  
79      String getSystemProperty(String name) {
80          /*
81           * LOGBACK-743: accessing a system property in the presence of a SecurityManager (e.g. applet sandbox) can
82           * result in a SecurityException.
83           */
84          try {
85              return System.getProperty(name);
86          } catch (SecurityException ex) {
87              return null;
88          }
89      }
90  
91      void processScanAttrib(InterpretationContext ic, Attributes attributes) {
92          String scanAttrib = ic.subst(attributes.getValue(SCAN_ATTR));
93          if (!OptionHelper.isEmpty(scanAttrib) && !"false".equalsIgnoreCase(scanAttrib)) {
94  
95              ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService();
96              URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context);
97              if (mainURL == null) {
98                  addWarn("Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done.");
99                  return;
100             }
101             ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask();
102             rocTask.setContext(context);
103 
104             context.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, rocTask);
105 
106             String scanPeriodAttrib = ic.subst(attributes.getValue(SCAN_PERIOD_ATTR));
107             Duration duration = getDurationOfScanPeriodAttribute(scanPeriodAttrib, SCAN_PERIOD_DEFAULT);
108 
109             addInfo("Will scan for changes in [" + mainURL + "] ");
110             // Given that included files are encountered at a later phase, the complete list of files 
111             // to scan can only be determined when the configuration is loaded in full.
112             // However, scan can be active if mainURL is set. Otherwise, when changes are detected
113             // the top level config file cannot be accessed.
114             addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration);
115  
116             ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, duration.getMilliseconds(), duration.getMilliseconds(),
117                             TimeUnit.MILLISECONDS);
118             context.addScheduledFuture(scheduledFuture);
119         }
120     }
121 
122     private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) {
123         Duration duration = null;
124 
125         if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
126             try {
127                 duration = Duration.valueOf(scanPeriodAttrib);
128             } catch(IllegalStateException|IllegalArgumentException e) {
129                addWarn("Failed to parse 'scanPeriod' attribute ["+scanPeriodAttrib+"]", e);
130                 // default duration will be set below
131             }
132         }
133         
134         if(duration == null) {
135             addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString());
136             duration = defaultDuration;
137         }
138         return duration;
139     }
140 
141     public void end(InterpretationContext ec, String name) {
142         addInfo("End of configuration.");
143         ec.popObject();
144     }
145 }