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.util;
18  
19  import java.io.File;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.logging.log4j.core.config.Configuration;
24  import org.apache.logging.log4j.core.config.ConfigurationListener;
25  import org.apache.logging.log4j.core.config.Reconfigurable;
26  
27  /**
28   *
29   */
30  public class WrappedFileWatcher extends AbstractWatcher implements FileWatcher {
31  
32      private final FileWatcher watcher;
33      private volatile long lastModifiedMillis;
34  
35      public WrappedFileWatcher(FileWatcher watcher, final Configuration configuration,
36          final Reconfigurable reconfigurable, final List<ConfigurationListener> configurationListeners,
37          final long lastModifiedMillis) {
38          super(configuration, reconfigurable, configurationListeners);
39          this.watcher = watcher;
40          this.lastModifiedMillis = lastModifiedMillis;
41      }
42  
43  
44      public WrappedFileWatcher(FileWatcher watcher) {
45          super(null, null, null);
46          this.watcher = watcher;
47      }
48  
49      public long getLastModified() {
50          return lastModifiedMillis;
51      }
52  
53      @Override
54      public void fileModified(File file) {
55          watcher.fileModified(file);
56      }
57  
58      @Override
59      public boolean isModified() {
60          long lastModified = getSource().getFile().lastModified();
61          if (lastModifiedMillis != lastModified) {
62              lastModifiedMillis = lastModified;
63              return true;
64          }
65          return false;
66      }
67  
68      @Override
69      public List<ConfigurationListener> getListeners() {
70          if (super.getListeners() != null) {
71              return Collections.unmodifiableList(super.getListeners());
72          } else {
73              return null;
74          }
75      }
76  
77      @Override
78      public void modified() {
79          if (getListeners() != null) {
80              super.modified();
81          }
82          fileModified(getSource().getFile());
83          lastModifiedMillis = getSource().getFile().lastModified();
84      }
85  
86      @Override
87      public void watching(Source source) {
88          lastModifiedMillis = source.getFile().lastModified();
89          super.watching(source);
90      }
91  
92      @Override
93      public Watcher newWatcher(final Reconfigurable reconfigurable, final List<ConfigurationListener> listeners,
94          long lastModifiedMillis) {
95          WrappedFileWatcher watcher = new WrappedFileWatcher(this.watcher, getConfiguration(), reconfigurable, listeners,
96              lastModifiedMillis);
97          if (getSource() != null) {
98              watcher.watching(getSource());
99          }
100         return watcher;
101     }
102 }