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  
22  /**
23   * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
24   * ConfigurationListeners when an update occurs.
25   */
26  public class FileConfigurationMonitor implements ConfigurationMonitor {
27  
28      private static final int MASK = 0x0f;
29  
30      private static final int MIN_INTERVAL = 5;
31  
32      private static final int MILLIS_PER_SECOND = 1000;
33  
34      private final File file;
35  
36      private long lastModified;
37  
38      private final List<ConfigurationListener> listeners;
39  
40      private final int interval;
41  
42      private long nextCheck;
43  
44      private volatile int counter = 0;
45  
46      private Reconfigurable reconfigurable;
47  
48      /**
49       * Constructor.
50       * @param reconfigurable The Configuration that can be reconfigured.
51       * @param file The File to monitor.
52       * @param listeners The List of ConfigurationListeners to notify upon a change.
53       * @param interval The monitor interval in seconds. The minimum interval is 5 seconds.
54       */
55      public FileConfigurationMonitor(Reconfigurable reconfigurable, File file, List<ConfigurationListener> listeners,
56                                      int interval) {
57          this.reconfigurable = reconfigurable;
58          this.file = file;
59          this.lastModified = file.lastModified();
60          this.listeners = listeners;
61          this.interval = (interval < MIN_INTERVAL ? MIN_INTERVAL : interval) * MILLIS_PER_SECOND;
62          this.nextCheck = System.currentTimeMillis() + interval;
63      }
64  
65      /**
66       * Called to determine if the configuration has changed.
67       */
68      public void checkConfiguration() {
69          if ((++counter & MASK) == 0) {
70              synchronized (this) {
71                  long current = System.currentTimeMillis();
72                  if (current >= nextCheck) {
73                      nextCheck = current + interval;
74                      if (file.lastModified() > lastModified) {
75                          lastModified = file.lastModified();
76                          for (ConfigurationListener listener : listeners) {
77                              listener.onChange(reconfigurable);
78                          }
79                      }
80                  }
81              }
82          }
83      }
84  }