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 org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.core.AbstractLifeCycle;
21  import org.apache.logging.log4j.core.config.Configuration;
22  import org.apache.logging.log4j.core.config.ConfigurationScheduler;
23  import org.apache.logging.log4j.status.StatusLogger;
24  
25  import java.io.File;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  import java.util.concurrent.ConcurrentMap;
29  import java.util.concurrent.ScheduledExecutorService;
30  import java.util.concurrent.ScheduledFuture;
31  import java.util.concurrent.TimeUnit;
32  
33  /**
34   * Manages FileWatchers.
35   */
36  public class WatchManager extends AbstractLifeCycle {
37  
38      private static final long serialVersionUID = 8998356999926962686L;
39      private static Logger logger = StatusLogger.getLogger();
40      private final ConcurrentMap<File, FileMonitor> watchers = new ConcurrentHashMap<>();
41      private int intervalSeconds = 0;
42      private ScheduledFuture<?> future;
43      private ConfigurationScheduler scheduler;
44  
45      public WatchManager(ConfigurationScheduler scheduler) {
46          this.scheduler = scheduler;
47      }
48  
49      public void setIntervalSeconds(int intervalSeconds) {
50          if (!isStarted()) {
51              if (this.intervalSeconds > 0 && intervalSeconds == 0) {
52                  scheduler.decrementScheduledItems();
53              } else if (this.intervalSeconds == 0 && intervalSeconds > 0) {
54                  scheduler.incrementScheduledItems();
55              }
56              this.intervalSeconds = intervalSeconds;
57          }
58      }
59  
60      public int getIntervalSeconds() {
61          return this.intervalSeconds;
62      }
63  
64      @Override
65      public void start() {
66          super.start();
67          if (intervalSeconds > 0) {
68              future = scheduler.scheduleWithFixedDelay(new WatchWorker(), intervalSeconds, intervalSeconds,
69                      TimeUnit.SECONDS);
70          }
71      }
72  
73      @Override
74      public void stop() {
75          future.cancel(true);
76          super.stop();
77      }
78  
79      public void watchFile(File file, FileWatcher watcher) {
80          watchers.put(file, new FileMonitor(file.lastModified(), watcher));
81  
82      }
83  
84      private class WatchWorker implements Runnable {
85  
86          @Override
87          public void run() {
88              for (Map.Entry<File, FileMonitor> entry : watchers.entrySet()) {
89                  File file = entry.getKey();
90                  FileMonitor fileMonitor = entry.getValue();
91                  long lastModfied = file.lastModified();
92                  if (lastModfied > fileMonitor.lastModified) {
93                      logger.info("File {} was modified", file.toString());
94                      fileMonitor.lastModified = lastModfied;
95                      fileMonitor.fileWatcher.fileModified(file);
96                  }
97              }
98          }
99      }
100 
101     private class FileMonitor {
102         private final FileWatcher fileWatcher;
103         private long lastModified;
104 
105         public FileMonitor(long lastModified, FileWatcher fileWatcher) {
106             this.fileWatcher = fileWatcher;
107             this.lastModified = lastModified;
108         }
109     }
110 }