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    public long getLastModified() {
050        return lastModifiedMillis;
051    }
052
053    @Override
054    public void fileModified(File file) {
055        watcher.fileModified(file);
056    }
057
058    @Override
059    public boolean isModified() {
060        long lastModified = getSource().getFile().lastModified();
061        if (lastModifiedMillis != lastModified) {
062            lastModifiedMillis = lastModified;
063            return true;
064        }
065        return false;
066    }
067
068    @Override
069    public List<ConfigurationListener> getListeners() {
070        if (super.getListeners() != null) {
071            return Collections.unmodifiableList(super.getListeners());
072        } else {
073            return null;
074        }
075    }
076
077    @Override
078    public void modified() {
079        if (getListeners() != null) {
080            super.modified();
081        }
082        fileModified(getSource().getFile());
083        lastModifiedMillis = getSource().getFile().lastModified();
084    }
085
086    @Override
087    public void watching(Source source) {
088        lastModifiedMillis = source.getFile().lastModified();
089        super.watching(source);
090    }
091
092    @Override
093    public Watcher newWatcher(final Reconfigurable reconfigurable, final List<ConfigurationListener> listeners,
094        long lastModifiedMillis) {
095        WrappedFileWatcher watcher = new WrappedFileWatcher(this.watcher, getConfiguration(), reconfigurable, listeners,
096            lastModifiedMillis);
097        if (getSource() != null) {
098            watcher.watching(getSource());
099        }
100        return watcher;
101    }
102}