001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017 package org.apache.logging.log4j.core.config;
018
019 import java.io.File;
020 import java.util.List;
021 import java.util.concurrent.atomic.AtomicInteger;
022 import java.util.concurrent.locks.Lock;
023 import java.util.concurrent.locks.ReentrantLock;
024
025 /**
026 * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
027 * ConfigurationListeners when an update occurs.
028 */
029 public class FileConfigurationMonitor implements ConfigurationMonitor {
030
031 private static final int MASK = 0x0f;
032
033 private static final int MIN_INTERVAL = 5;
034
035 private static final int MILLIS_PER_SECOND = 1000;
036
037 private final File file;
038
039 private long lastModified;
040
041 private final List<ConfigurationListener> listeners;
042
043 private final int intervalSeconds;
044
045 private long nextCheck;
046
047 private final AtomicInteger counter = new AtomicInteger(0);
048
049 private static final Lock LOCK = new ReentrantLock();
050
051 private final Reconfigurable reconfigurable;
052
053 /**
054 * Constructor.
055 * @param reconfigurable The Configuration that can be reconfigured.
056 * @param file The File to monitor.
057 * @param listeners The List of ConfigurationListeners to notify upon a change.
058 * @param intervalSeconds The monitor interval in seconds. The minimum interval is 5 seconds.
059 */
060 public FileConfigurationMonitor(final Reconfigurable reconfigurable, final File file,
061 final List<ConfigurationListener> listeners,
062 final int intervalSeconds) {
063 this.reconfigurable = reconfigurable;
064 this.file = file;
065 this.lastModified = file.lastModified();
066 this.listeners = listeners;
067 this.intervalSeconds = (intervalSeconds < MIN_INTERVAL ? MIN_INTERVAL : intervalSeconds) * MILLIS_PER_SECOND;
068 this.nextCheck = System.currentTimeMillis() + intervalSeconds;
069 }
070
071 /**
072 * Called to determine if the configuration has changed.
073 */
074 @Override
075 public void checkConfiguration() {
076 final long current = System.currentTimeMillis();
077 if (((counter.incrementAndGet() & MASK) == 0) && (current >= nextCheck)) {
078 LOCK.lock();
079 try {
080 nextCheck = current + intervalSeconds;
081 if (file.lastModified() > lastModified) {
082 lastModified = file.lastModified();
083 for (final ConfigurationListener listener : listeners) {
084 final Thread thread = new Thread(new ReconfigurationWorker(listener, reconfigurable));
085 thread.setDaemon(true);
086 thread.start();
087 }
088 }
089 } finally {
090 LOCK.unlock();
091 }
092 }
093 }
094
095 private class ReconfigurationWorker implements Runnable {
096
097 private final ConfigurationListener listener;
098 private final Reconfigurable reconfigurable;
099
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 }