View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config;
18  
19  import java.io.File;
20  import java.util.List;
21  import java.util.concurrent.TimeUnit;
22  import java.util.concurrent.atomic.AtomicInteger;
23  import java.util.concurrent.locks.Lock;
24  import java.util.concurrent.locks.ReentrantLock;
25  
26  /**
27   * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
28   * ConfigurationListeners when an update occurs.
29   */
30  public class FileConfigurationMonitor implements ConfigurationMonitor {
31  
32      private static final int MASK = 0x0f;
33  
34      static final int MIN_INTERVAL = 5;
35  
36      private final File file;
37  
38      private volatile long lastModified;
39  
40      private final List<ConfigurationListener> listeners;
41  
42      private final long intervalNano;
43  
44      private volatile long nextCheck;
45  
46      private final AtomicInteger counter = new AtomicInteger(0);
47  
48      private static final Lock LOCK = new ReentrantLock();
49  
50      private final Reconfigurable reconfigurable;
51  
52      /**
53       * Constructor.
54       * @param reconfigurable The Configuration that can be reconfigured.
55       * @param file The File to monitor.
56       * @param listeners The List of ConfigurationListeners to notify upon a change.
57       * @param intervalSeconds The monitor interval in seconds. The minimum interval is 5 seconds.
58       */
59      public FileConfigurationMonitor(final Reconfigurable reconfigurable, final File file,
60                                      final List<ConfigurationListener> listeners,
61                                      final int intervalSeconds) {
62          this.reconfigurable = reconfigurable;
63          this.file = file;
64          this.lastModified = file.lastModified();
65          this.listeners = listeners;
66          this.intervalNano = TimeUnit.SECONDS.toNanos(Math.max(intervalSeconds, MIN_INTERVAL));
67          this.nextCheck = System.nanoTime() + this.intervalNano;
68      }
69  
70      /**
71       * Called to determine if the configuration has changed.
72       */
73      @Override
74      public void checkConfiguration() {
75          final long current;
76          if (((counter.incrementAndGet() & MASK) == 0) && ((current = System.nanoTime()) - nextCheck >= 0)) {
77              LOCK.lock();
78              try {
79                  nextCheck = current + intervalNano;
80                  final long currentLastModified = file.lastModified();
81                  if (currentLastModified > lastModified) {
82                      lastModified = currentLastModified;
83                      for (final ConfigurationListener listener : listeners) {
84                          final Thread thread = new Thread(new ReconfigurationWorker(listener, reconfigurable));
85                          thread.setDaemon(true);
86                          thread.start();
87                      }
88                  }
89              } finally {
90                  LOCK.unlock();
91              }
92          }
93      }
94  
95      private static class ReconfigurationWorker implements Runnable {
96  
97          private final ConfigurationListener listener;
98          private final Reconfigurable reconfigurable;
99  
100         public ReconfigurationWorker(final ConfigurationListener listener, final Reconfigurable reconfigurable) {
101             this.listener = listener;
102             this.reconfigurable = reconfigurable;
103         }
104 
105         @Override
106         public void run() {
107             listener.onChange(reconfigurable);
108         }
109     }
110 
111     /* (non-Javadoc)
112      * @see org.apache.logging.log4j.core.config.ReliabilityStrategyFactory#getReliabilityStrategy(org.apache.logging.log4j.core.config.LoggerConfig)
113      */
114     @Override
115     public ReliabilityStrategy getReliabilityStrategy(LoggerConfig loggerConfig) {
116         return ReliabilityStrategyFactory.getReliabilityStrategy(loggerConfig);
117     }
118 }