Coverage Report - org.apache.commons.io.monitor.FileAlterationMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
FileAlterationMonitor
92%
53/57
96%
25/26
2.417
 
 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.commons.io.monitor;
 18  
 
 19  
 import java.util.List;
 20  
 import java.util.concurrent.CopyOnWriteArrayList;
 21  
 import java.util.concurrent.ThreadFactory;
 22  
 
 23  
 /**
 24  
  * A runnable that spawns a monitoring thread triggering any
 25  
  * registered {@link FileAlterationObserver} at a specified interval.
 26  
  * 
 27  
  * @see FileAlterationObserver
 28  
  * @version $Id: FileAlterationMonitor.java 1415850 2012-11-30 20:51:39Z ggregory $
 29  
  * @since 2.0
 30  
  */
 31  
 public final class FileAlterationMonitor implements Runnable {
 32  
 
 33  
     private final long interval;
 34  10
     private final List<FileAlterationObserver> observers = new CopyOnWriteArrayList<FileAlterationObserver>();
 35  10
     private Thread thread = null;
 36  
     private ThreadFactory threadFactory;
 37  10
     private volatile boolean running = false;
 38  
 
 39  
     /**
 40  
      * Construct a monitor with a default interval of 10 seconds.
 41  
      */
 42  
     public FileAlterationMonitor() {
 43  2
         this(10000);
 44  2
     }
 45  
 
 46  
     /**
 47  
      * Construct a monitor with the specified interval.
 48  
      *
 49  
      * @param interval The amount of time in miliseconds to wait between
 50  
      * checks of the file system
 51  
      */
 52  10
     public FileAlterationMonitor(final long interval) {
 53  10
         this.interval = interval;
 54  10
     }
 55  
 
 56  
     /**
 57  
      * Construct a monitor with the specified interval and set of observers.
 58  
      *
 59  
      * @param interval The amount of time in miliseconds to wait between
 60  
      * checks of the file system
 61  
      * @param observers The set of observers to add to the monitor.
 62  
      */
 63  
     public FileAlterationMonitor(final long interval, final FileAlterationObserver... observers) {
 64  8
         this(interval);
 65  8
         if (observers != null) {
 66  12
             for (final FileAlterationObserver observer : observers) {
 67  6
                 addObserver(observer);
 68  
             }
 69  
         }
 70  8
     }
 71  
 
 72  
     /**
 73  
      * Return the interval.
 74  
      *
 75  
      * @return the interval
 76  
      */
 77  
     public long getInterval() {
 78  8
         return interval;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Set the thread factory.
 83  
      *
 84  
      * @param threadFactory the thread factory
 85  
      */
 86  
     public synchronized void setThreadFactory(final ThreadFactory threadFactory) {
 87  2
         this.threadFactory = threadFactory;
 88  2
     }
 89  
 
 90  
     /**
 91  
      * Add a file system observer to this monitor.
 92  
      *
 93  
      * @param observer The file system observer to add
 94  
      */
 95  
     public void addObserver(final FileAlterationObserver observer) {
 96  10
         if (observer != null) {
 97  6
             observers.add(observer);
 98  
         }
 99  10
     }
 100  
 
 101  
     /**
 102  
      * Remove a file system observer from this monitor.
 103  
      *
 104  
      * @param observer The file system observer to remove
 105  
      */
 106  
     public void removeObserver(final FileAlterationObserver observer) {
 107  4
         if (observer != null) {
 108  4
             while (observers.remove(observer)) {
 109  
             }
 110  
         }
 111  4
     }
 112  
 
 113  
     /**
 114  
      * Returns the set of {@link FileAlterationObserver} registered with
 115  
      * this monitor. 
 116  
      *
 117  
      * @return The set of {@link FileAlterationObserver}
 118  
      */
 119  
     public Iterable<FileAlterationObserver> getObservers() {
 120  10
         return observers;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Start monitoring.
 125  
      *
 126  
      * @throws Exception if an error occurs initializing the observer
 127  
      */
 128  
     public synchronized void start() throws Exception {
 129  6
         if (running) {
 130  2
             throw new IllegalStateException("Monitor is already running");
 131  
         }
 132  4
         for (final FileAlterationObserver observer : observers) {
 133  4
             observer.initialize();
 134  4
         }
 135  4
         running = true;
 136  4
         if (threadFactory != null) {
 137  2
             thread = threadFactory.newThread(this);
 138  
         } else {
 139  2
             thread = new Thread(this);
 140  
         }
 141  4
         thread.start();
 142  4
     }
 143  
 
 144  
     /**
 145  
      * Stop monitoring.
 146  
      *
 147  
      * @throws Exception if an error occurs initializing the observer
 148  
      */
 149  
     public synchronized void stop() throws Exception {
 150  6
         stop(interval);
 151  4
     }
 152  
 
 153  
     /**
 154  
      * Stop monitoring.
 155  
      *
 156  
      * @param stopInterval the amount of time in milliseconds to wait for the thread to finish.
 157  
      * A value of zero will wait until the thread is finished (see {@link Thread#join(long)}).
 158  
      * @throws Exception if an error occurs initializing the observer
 159  
      * @since 2.1
 160  
      */
 161  
     public synchronized void stop(final long stopInterval) throws Exception {
 162  6
         if (running == false) {
 163  2
             throw new IllegalStateException("Monitor is not running");
 164  
         }
 165  4
         running = false;
 166  
         try {
 167  4
             thread.join(stopInterval);
 168  0
         } catch (final InterruptedException e) {
 169  0
             Thread.currentThread().interrupt();
 170  4
         }
 171  4
         for (final FileAlterationObserver observer : observers) {
 172  4
             observer.destroy();
 173  4
         }
 174  4
     }
 175  
 
 176  
     /**
 177  
      * Run.
 178  
      */
 179  
     public void run() {
 180  17
         while (running) {
 181  13
             for (final FileAlterationObserver observer : observers) {
 182  13
                 observer.checkAndNotify();
 183  13
             }
 184  13
             if (!running) {
 185  0
                 break;
 186  
             }
 187  
             try {
 188  13
                 Thread.sleep(interval);
 189  0
             } catch (final InterruptedException ignored) {
 190  13
             }
 191  
         }
 192  4
     }
 193  
 }