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 final 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(final Reconfigurable reconfigurable, final File file,
56                                      final List<ConfigurationListener> listeners,
57                                      final int interval) {
58          this.reconfigurable = reconfigurable;
59          this.file = file;
60          this.lastModified = file.lastModified();
61          this.listeners = listeners;
62          this.interval = (interval < MIN_INTERVAL ? MIN_INTERVAL : interval) * MILLIS_PER_SECOND;
63          this.nextCheck = System.currentTimeMillis() + interval;
64      }
65  
66      /**
67       * Called to determine if the configuration has changed.
68       */
69      public void checkConfiguration() {
70          if ((++counter & MASK) == 0) {
71              synchronized (this) {
72                  final long current = System.currentTimeMillis();
73                  if (current >= nextCheck) {
74                      nextCheck = current + interval;
75                      if (file.lastModified() > lastModified) {
76                          lastModified = file.lastModified();
77                          for (final ConfigurationListener listener : listeners) {
78                              listener.onChange(reconfigurable);
79                          }
80                      }
81                  }
82              }
83          }
84      }
85  }