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 */
017package org.apache.logging.log4j.core.config;
018
019import java.io.File;
020import java.util.List;
021import java.util.concurrent.TimeUnit;
022import java.util.concurrent.atomic.AtomicInteger;
023import java.util.concurrent.locks.Lock;
024import java.util.concurrent.locks.ReentrantLock;
025
026/**
027 * Configuration monitor that periodically checks the timestamp of the configuration file and calls the
028 * ConfigurationListeners when an update occurs.
029 */
030public class FileConfigurationMonitor implements ConfigurationMonitor {
031
032    private static final int MASK = 0x0f;
033
034    static final int MIN_INTERVAL = 5;
035
036    private final File file;
037
038    private volatile long lastModified;
039
040    private final List<ConfigurationListener> listeners;
041
042    private final long intervalNano;
043
044    private volatile long nextCheck;
045
046    private final AtomicInteger counter = new AtomicInteger(0);
047
048    private static final Lock LOCK = new ReentrantLock();
049
050    private final Reconfigurable reconfigurable;
051
052    /**
053     * Constructor.
054     * @param reconfigurable The Configuration that can be reconfigured.
055     * @param file The File to monitor.
056     * @param listeners The List of ConfigurationListeners to notify upon a change.
057     * @param intervalSeconds The monitor interval in seconds. The minimum interval is 5 seconds.
058     */
059    public FileConfigurationMonitor(final Reconfigurable reconfigurable, final File file,
060                                    final List<ConfigurationListener> listeners,
061                                    final int intervalSeconds) {
062        this.reconfigurable = reconfigurable;
063        this.file = file;
064        this.lastModified = file.lastModified();
065        this.listeners = listeners;
066        this.intervalNano = TimeUnit.SECONDS.toNanos(Math.max(intervalSeconds, MIN_INTERVAL));
067        this.nextCheck = System.nanoTime() + this.intervalNano;
068    }
069
070    /**
071     * Called to determine if the configuration has changed.
072     */
073    @Override
074    public void checkConfiguration() {
075        final long current;
076        if (((counter.incrementAndGet() & MASK) == 0) && ((current = System.nanoTime()) - nextCheck >= 0)) {
077            LOCK.lock();
078            try {
079                nextCheck = current + intervalNano;
080                final long currentLastModified = file.lastModified();
081                if (currentLastModified > lastModified) {
082                    lastModified = currentLastModified;
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 static 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
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}