| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileAlterationMonitor |
|
| 2.4166666666666665;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 | 5 | private final List<FileAlterationObserver> observers = new CopyOnWriteArrayList<FileAlterationObserver>(); |
| 35 | 5 | private Thread thread = null; |
| 36 | private ThreadFactory threadFactory; | |
| 37 | 5 | private volatile boolean running = false; |
| 38 | ||
| 39 | /** | |
| 40 | * Construct a monitor with a default interval of 10 seconds. | |
| 41 | */ | |
| 42 | public FileAlterationMonitor() { | |
| 43 | 1 | this(10000); |
| 44 | 1 | } |
| 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 | 5 | public FileAlterationMonitor(final long interval) { |
| 53 | 5 | this.interval = interval; |
| 54 | 5 | } |
| 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 | 4 | this(interval); |
| 65 | 4 | if (observers != null) { |
| 66 | 6 | for (final FileAlterationObserver observer : observers) { |
| 67 | 3 | addObserver(observer); |
| 68 | } | |
| 69 | } | |
| 70 | 4 | } |
| 71 | ||
| 72 | /** | |
| 73 | * Return the interval. | |
| 74 | * | |
| 75 | * @return the interval | |
| 76 | */ | |
| 77 | public long getInterval() { | |
| 78 | 4 | 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 | 1 | this.threadFactory = threadFactory; |
| 88 | 1 | } |
| 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 | 5 | if (observer != null) { |
| 97 | 3 | observers.add(observer); |
| 98 | } | |
| 99 | 5 | } |
| 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 | 2 | if (observer != null) { |
| 108 | 2 | while (observers.remove(observer)) { |
| 109 | } | |
| 110 | } | |
| 111 | 2 | } |
| 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 | 5 | 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 | 3 | if (running) { |
| 130 | 1 | throw new IllegalStateException("Monitor is already running"); |
| 131 | } | |
| 132 | 2 | for (final FileAlterationObserver observer : observers) { |
| 133 | 2 | observer.initialize(); |
| 134 | 2 | } |
| 135 | 2 | running = true; |
| 136 | 2 | if (threadFactory != null) { |
| 137 | 1 | thread = threadFactory.newThread(this); |
| 138 | } else { | |
| 139 | 1 | thread = new Thread(this); |
| 140 | } | |
| 141 | 2 | thread.start(); |
| 142 | 2 | } |
| 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 | 3 | stop(interval); |
| 151 | 2 | } |
| 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 | 3 | if (running == false) { |
| 163 | 1 | throw new IllegalStateException("Monitor is not running"); |
| 164 | } | |
| 165 | 2 | running = false; |
| 166 | try { | |
| 167 | 2 | thread.join(stopInterval); |
| 168 | 0 | } catch (final InterruptedException e) { |
| 169 | 0 | Thread.currentThread().interrupt(); |
| 170 | 2 | } |
| 171 | 2 | for (final FileAlterationObserver observer : observers) { |
| 172 | 2 | observer.destroy(); |
| 173 | 2 | } |
| 174 | 2 | } |
| 175 | ||
| 176 | /** | |
| 177 | * Run. | |
| 178 | */ | |
| 179 | public void run() { | |
| 180 | 8 | while (running) { |
| 181 | 6 | for (final FileAlterationObserver observer : observers) { |
| 182 | 6 | observer.checkAndNotify(); |
| 183 | 6 | } |
| 184 | 6 | if (!running) { |
| 185 | 0 | break; |
| 186 | } | |
| 187 | try { | |
| 188 | 6 | Thread.sleep(interval); |
| 189 | 0 | } catch (final InterruptedException ignored) { |
| 190 | 6 | } |
| 191 | } | |
| 192 | 2 | } |
| 193 | } |