2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
20 package org.eclipse.jetty.util;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import java.util.Timer;
35 import java.util.TimerTask;
37 import org.eclipse.jetty.util.component.AbstractLifeCycle;
38 import org.eclipse.jetty.util.log.Log;
39 import org.eclipse.jetty.util.log.Logger;
45 * Utility for scanning a directory for added, removed and changed
46 * files and reporting these events via registered Listeners.
49 public class Scanner extends AbstractLifeCycle
51 private static final Logger LOG = Log.getLogger(Scanner.class);
52 private static int __scannerId=0;
53 private int _scanInterval;
54 private int _scanCount = 0;
55 private final List<Listener> _listeners = new ArrayList<Listener>();
56 private final Map<String,TimeNSize> _prevScan = new HashMap<String,TimeNSize> ();
57 private final Map<String,TimeNSize> _currentScan = new HashMap<String,TimeNSize> ();
58 private FilenameFilter _filter;
59 private final List<File> _scanDirs = new ArrayList<File>();
60 private volatile boolean _running = false;
61 private boolean _reportExisting = true;
62 private boolean _reportDirs = true;
64 private TimerTask _task;
65 private int _scanDepth=0;
67 public enum Notification { ADDED, CHANGED, REMOVED };
68 private final Map<String,Notification> _notifications = new HashMap<String,Notification>();
70 static class TimeNSize
72 final long _lastModified;
75 public TimeNSize(long lastModified, long size)
77 _lastModified = lastModified;
84 return (int)_lastModified^(int)_size;
88 public boolean equals(Object o)
90 if (o instanceof TimeNSize)
92 TimeNSize tns = (TimeNSize)o;
93 return tns._lastModified==_lastModified && tns._size==_size;
99 public String toString()
101 return "[lm="+_lastModified+",s="+_size+"]";
108 * Marker for notifications re file changes.
110 public interface Listener
114 public interface ScanListener extends Listener
119 public interface DiscreteListener extends Listener
121 public void fileChanged (String filename) throws Exception;
122 public void fileAdded (String filename) throws Exception;
123 public void fileRemoved (String filename) throws Exception;
127 public interface BulkListener extends Listener
129 public void filesChanged (List<String> filenames) throws Exception;
133 * Listener that notifies when a scan has started and when it has ended.
135 public interface ScanCycleListener extends Listener
137 public void scanStarted(int cycle) throws Exception;
138 public void scanEnded(int cycle) throws Exception;
149 * Get the scan interval
150 * @return interval between scans in seconds
152 public synchronized int getScanInterval()
154 return _scanInterval;
158 * Set the scan interval
159 * @param scanInterval pause between scans in seconds, or 0 for no scan after the initial scan.
161 public synchronized void setScanInterval(int scanInterval)
163 _scanInterval = scanInterval;
167 public void setScanDirs (List<File> dirs)
170 _scanDirs.addAll(dirs);
173 public synchronized void addScanDir( File dir )
175 _scanDirs.add( dir );
178 public List<File> getScanDirs ()
180 return Collections.unmodifiableList(_scanDirs);
183 /* ------------------------------------------------------------ */
185 * @param recursive True if scanning is recursive
186 * @see #setScanDepth(int)
188 public void setRecursive (boolean recursive)
190 _scanDepth=recursive?-1:0;
193 /* ------------------------------------------------------------ */
195 * @return True if scanning is fully recursive (scandepth==-1)
196 * @see #getScanDepth()
198 public boolean getRecursive ()
200 return _scanDepth==-1;
203 /* ------------------------------------------------------------ */
204 /** Get the scanDepth.
205 * @return the scanDepth
207 public int getScanDepth()
212 /* ------------------------------------------------------------ */
213 /** Set the scanDepth.
214 * @param scanDepth the scanDepth to set
216 public void setScanDepth(int scanDepth)
218 _scanDepth = scanDepth;
222 * Apply a filter to files found in the scan directory.
223 * Only files matching the filter will be reported as added/changed/removed.
226 public void setFilenameFilter (FilenameFilter filter)
232 * Get any filter applied to files in the scan dir.
233 * @return the filename filter
235 public FilenameFilter getFilenameFilter ()
240 /* ------------------------------------------------------------ */
242 * Whether or not an initial scan will report all files as being
244 * @param reportExisting if true, all files found on initial scan will be
245 * reported as being added, otherwise not
247 public void setReportExistingFilesOnStartup (boolean reportExisting)
249 _reportExisting = reportExisting;
252 /* ------------------------------------------------------------ */
253 public boolean getReportExistingFilesOnStartup()
255 return _reportExisting;
258 /* ------------------------------------------------------------ */
259 /** Set if found directories should be reported.
262 public void setReportDirs(boolean dirs)
267 /* ------------------------------------------------------------ */
268 public boolean getReportDirs()
273 /* ------------------------------------------------------------ */
275 * Add an added/removed/changed listener
278 public synchronized void addListener (Listener listener)
280 if (listener == null)
282 _listeners.add(listener);
285 /* ------------------------------------------------------------ */
287 * Remove a registered listener
288 * @param listener the Listener to be removed
290 public synchronized void removeListener (Listener listener)
292 if (listener == null)
294 _listeners.remove(listener);
299 * Start the scanning action.
302 public synchronized void doStart()
311 // if files exist at startup, report them
313 scan(); // scan twice so files reported as stable
317 //just register the list of existing files and only report changes
319 _prevScan.putAll(_currentScan);
324 public TimerTask newTimerTask ()
326 return new TimerTask()
329 public void run() { scan(); }
333 public Timer newTimer ()
335 return new Timer("Scanner-"+__scannerId++, true);
338 public void schedule ()
346 if (getScanInterval() > 0)
349 _task = newTimerTask();
350 _timer.schedule(_task, 1010L*getScanInterval(),1010L*getScanInterval());
358 public synchronized void doStop()
373 * @return true if the path exists in one of the scandirs
375 public boolean exists(String path)
377 for (File dir : _scanDirs)
378 if (new File(dir,path).exists())
385 * Perform a pass of the scanner and report changes
387 public synchronized void scan ()
389 reportScanStart(++_scanCount);
391 reportDifferences(_currentScan, _prevScan);
393 _prevScan.putAll(_currentScan);
394 reportScanEnd(_scanCount);
396 for (Listener l : _listeners)
400 if (l instanceof ScanListener)
401 ((ScanListener)l).scan();
415 * Recursively scan all files in the designated directories.
417 public synchronized void scanFiles ()
422 _currentScan.clear();
423 Iterator<File> itor = _scanDirs.iterator();
424 while (itor.hasNext())
426 File dir = itor.next();
428 if ((dir != null) && (dir.exists()))
431 scanFile(dir.getCanonicalFile(), _currentScan,0);
433 catch (IOException e)
435 LOG.warn("Error scanning files.", e);
442 * Report the adds/changes/removes to the registered listeners
444 * @param currentScan the info from the most recent pass
445 * @param oldScan info from the previous pass
447 public synchronized void reportDifferences (Map<String,TimeNSize> currentScan, Map<String,TimeNSize> oldScan)
449 // scan the differences and add what was found to the map of notifications:
451 Set<String> oldScanKeys = new HashSet<String>(oldScan.keySet());
453 // Look for new and changed files
454 for (Map.Entry<String, TimeNSize> entry: currentScan.entrySet())
456 String file = entry.getKey();
457 if (!oldScanKeys.contains(file))
459 Notification old=_notifications.put(file,Notification.ADDED);
466 _notifications.put(file,Notification.CHANGED);
470 else if (!oldScan.get(file).equals(currentScan.get(file)))
472 Notification old=_notifications.put(file,Notification.CHANGED);
478 _notifications.put(file,Notification.ADDED);
484 // Look for deleted files
485 for (String file : oldScan.keySet())
487 if (!currentScan.containsKey(file))
489 Notification old=_notifications.put(file,Notification.REMOVED);
495 _notifications.remove(file);
501 if (LOG.isDebugEnabled())
502 LOG.debug("scanned "+_scanDirs+": "+_notifications);
504 // Process notifications
505 // Only process notifications that are for stable files (ie same in old and current scan).
506 List<String> bulkChanges = new ArrayList<String>();
507 for (Iterator<Entry<String,Notification>> iter = _notifications.entrySet().iterator();iter.hasNext();)
509 Entry<String,Notification> entry=iter.next();
510 String file=entry.getKey();
512 // Is the file stable?
513 if (oldScan.containsKey(file))
515 if (!oldScan.get(file).equals(currentScan.get(file)))
518 else if (currentScan.containsKey(file))
521 // File is stable so notify
522 Notification notification=entry.getValue();
524 bulkChanges.add(file);
528 reportAddition(file);
538 if (!bulkChanges.isEmpty())
539 reportBulkChanges(bulkChanges);
544 * Get last modified time on a single file or recurse if
545 * the file is a directory.
546 * @param f file or directory
547 * @param scanInfoMap map of filenames to last modified times
549 private void scanFile (File f, Map<String,TimeNSize> scanInfoMap, int depth)
556 if (f.isFile() || depth>0&& _reportDirs && f.isDirectory())
558 if ((_filter == null) || ((_filter != null) && _filter.accept(f.getParentFile(), f.getName())))
560 if (LOG.isDebugEnabled())
561 LOG.debug("scan accepted {}",f);
562 String name = f.getCanonicalPath();
563 scanInfoMap.put(name, new TimeNSize(f.lastModified(),f.isDirectory()?0:f.length()));
567 if (LOG.isDebugEnabled())
568 LOG.debug("scan rejected {}",f);
572 // If it is a directory, scan if it is a known directory or the depth is OK.
573 if (f.isDirectory() && (depth<_scanDepth || _scanDepth==-1 || _scanDirs.contains(f)))
575 File[] files = f.listFiles();
578 for (int i=0;i<files.length;i++)
579 scanFile(files[i], scanInfoMap,depth+1);
582 LOG.warn("Error listing files in directory {}", f);
585 catch (IOException e)
587 LOG.warn("Error scanning watched files", e);
591 private void warn(Object listener,String filename,Throwable th)
593 LOG.warn(listener+" failed on '"+filename, th);
597 * Report a file addition to the registered FileAddedListeners
600 private void reportAddition (String filename)
602 Iterator<Listener> itor = _listeners.iterator();
603 while (itor.hasNext())
605 Listener l = itor.next();
608 if (l instanceof DiscreteListener)
609 ((DiscreteListener)l).fileAdded(filename);
624 * Report a file removal to the FileRemovedListeners
627 private void reportRemoval (String filename)
629 Iterator<Listener> itor = _listeners.iterator();
630 while (itor.hasNext())
632 Object l = itor.next();
635 if (l instanceof DiscreteListener)
636 ((DiscreteListener)l).fileRemoved(filename);
651 * Report a file change to the FileChangedListeners
654 private void reportChange (String filename)
656 Iterator<Listener> itor = _listeners.iterator();
657 while (itor.hasNext())
659 Listener l = itor.next();
662 if (l instanceof DiscreteListener)
663 ((DiscreteListener)l).fileChanged(filename);
676 private void reportBulkChanges (List<String> filenames)
678 Iterator<Listener> itor = _listeners.iterator();
679 while (itor.hasNext())
681 Listener l = itor.next();
684 if (l instanceof BulkListener)
685 ((BulkListener)l).filesChanged(filenames);
689 warn(l,filenames.toString(),e);
693 warn(l,filenames.toString(),e);
699 * signal any scan cycle listeners that a scan has started
701 private void reportScanStart(int cycle)
703 for (Listener listener : _listeners)
707 if (listener instanceof ScanCycleListener)
709 ((ScanCycleListener)listener).scanStarted(cycle);
714 LOG.warn(listener + " failed on scan start for cycle " + cycle, e);
722 private void reportScanEnd(int cycle)
724 for (Listener listener : _listeners)
728 if (listener instanceof ScanCycleListener)
730 ((ScanCycleListener)listener).scanEnded(cycle);
735 LOG.warn(listener + " failed on scan end for cycle " + cycle, e);