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.util;
018
019import java.io.File;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.logging.log4j.core.config.Configuration;
024import org.apache.logging.log4j.core.config.ConfigurationListener;
025import org.apache.logging.log4j.core.config.Reconfigurable;
026
027/**
028 *
029 */
030public class WrappedFileWatcher extends AbstractWatcher implements FileWatcher {
031
032    private final FileWatcher watcher;
033    private volatile long lastModifiedMillis;
034
035    public WrappedFileWatcher(FileWatcher watcher, final Configuration configuration,
036        final Reconfigurable reconfigurable, final List<ConfigurationListener> configurationListeners,
037        final long lastModifiedMillis) {
038        super(configuration, reconfigurable, configurationListeners);
039        this.watcher = watcher;
040        this.lastModifiedMillis = lastModifiedMillis;
041    }
042
043
044    public WrappedFileWatcher(FileWatcher watcher) {
045        super(null, null, null);
046        this.watcher = watcher;
047    }
048
049    @Override
050    public long getLastModified() {
051        return lastModifiedMillis;
052    }
053
054    @Override
055    public void fileModified(File file) {
056        watcher.fileModified(file);
057    }
058
059    @Override
060    public boolean isModified() {
061        long lastModified = getSource().getFile().lastModified();
062        if (lastModifiedMillis != lastModified) {
063            lastModifiedMillis = lastModified;
064            return true;
065        }
066        return false;
067    }
068
069    @Override
070    public List<ConfigurationListener> getListeners() {
071        if (super.getListeners() != null) {
072            return Collections.unmodifiableList(super.getListeners());
073        } else {
074            return null;
075        }
076    }
077
078    @Override
079    public void modified() {
080        if (getListeners() != null) {
081            super.modified();
082        }
083        fileModified(getSource().getFile());
084        lastModifiedMillis = getSource().getFile().lastModified();
085    }
086
087    @Override
088    public void watching(Source source) {
089        lastModifiedMillis = source.getFile().lastModified();
090        super.watching(source);
091    }
092
093    @Override
094    public Watcher newWatcher(final Reconfigurable reconfigurable, final List<ConfigurationListener> listeners,
095        long lastModifiedMillis) {
096        WrappedFileWatcher watcher = new WrappedFileWatcher(this.watcher, getConfiguration(), reconfigurable, listeners,
097            lastModifiedMillis);
098        if (getSource() != null) {
099            watcher.watching(getSource());
100        }
101        return watcher;
102    }
103}