WatchService.java revision 4216
2362N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 1077N/A * A watch service that <em>watches</em> registered objects for changes and 0N/A * events. For example a file manager may use a watch service to monitor a 1077N/A * directory for changes so that it can update its display of the list of files 0N/A * when files are created or deleted. 0N/A * <p> A {@link Watchable} object is registered with a watch service by invoking 1077N/A * its {@link Watchable#register register} method, returning a {@link WatchKey} 1077N/A * to represent the registration. When an event for an object is detected the 1077N/A * key is <em>signalled</em>, and if not currently signalled, it is queued to 1077N/A * the watch service so that it can be retrieved by consumers that invoke the 1077N/A * {@link #poll() poll} or {@link #take() take} methods to retrieve keys 0N/A * and process events. Once the events have been processed the consumer 0N/A * invokes the key's {@link WatchKey#reset reset} method to reset the key which 1077N/A * allows the key to be signalled and re-queued with further events. 1077N/A * <p> Registration with a watch service is cancelled by invoking the key's 1077N/A * {@link WatchKey#cancel cancel} method. A key that is queued at the time that 1077N/A * it is cancelled remains in the queue until it is retrieved. Depending on the 1077N/A * object, a key may be cancelled automatically. For example, suppose a 1077N/A * directory is watched and the watch service detects that it has been deleted 1077N/A * or its file system is no longer accessible. When a key is cancelled in this 1077N/A * manner it is signalled and queued, if not currently signalled. To ensure 0N/A * that the consumer is notified the return value from the {@code reset} 0N/A * method indicates if the key is valid. 0N/A * <p> A watch service is safe for use by multiple concurrent consumers. To 0N/A * ensure that only one consumer processes the events for a particular object at 0N/A * any time then care should be taken to ensure that the key's {@code reset} 0N/A * method is only invoked after its events have been processed. The {@link 0N/A * #close close} method may be invoked at any time to close the service causing 0N/A * any threads waiting to retrieve keys, to throw {@code 0N/A * ClosedWatchServiceException}. 0N/A * <p> File systems may report events faster than they can be retrieved or 0N/A * processed and an implementation may impose an unspecified limit on the number 0N/A * of events that it may accumulate. Where an implementation <em>knowingly</em> 0N/A * discards events then it arranges for the key's {@link WatchKey#pollEvents 0N/A * pollEvents} method to return an element with an event type of {@link 0N/A * StandardWatchEventKinds#OVERFLOW OVERFLOW}. This event can be used by the 1077N/A * consumer as a trigger to re-examine the state of the object. 1077N/A * <p> When an event is reported to indicate that a file in a watched directory 0N/A * has been modified then there is no guarantee that the program (or programs) 0N/A * that have modified the file have completed. Care should be taken to coordinate 1077N/A * access with other programs that may be updating the file. 0N/A * The {@link java.nio.channels.FileChannel FileChannel} class defines methods 0N/A * to lock regions of a file against access by other programs. 0N/A * <h4>Platform dependencies</h4> 0N/A * <p> The implementation that observes events from the file system is intended 0N/A * to map directly on to the native file event notification facility where 0N/A * available, or to use a primitive mechanism, such as polling, when a native 0N/A * facility is not available. Consequently, many of the details on how events 0N/A * are detected, their timeliness, and whether their ordering is preserved are 0N/A * highly implementation specific. For example, when a file in a watched 0N/A * directory is modified then it may result in a single {@link 0N/A * StandardWatchEventKinds#ENTRY_MODIFY ENTRY_MODIFY} event in some 0N/A * implementations but several events in other implementations. Short-lived 0N/A * files (meaning files that are deleted very quickly after they are created) 1077N/A * may not be detected by primitive implementations that periodically poll the 0N/A * file system to detect changes. 0N/A * <p> If a watched file is not located on a local storage device then it is 0N/A * implementation specific if changes to the file can be detected. In particular, 1077N/A * it is not required that changes to files carried out on remote systems be 0N/A * @see FileSystem#newWatchService 0N/A * Closes this watch service. 0N/A * <p> If a thread is currently blocked in the {@link #take take} or {@link 0N/A * #poll(long,TimeUnit) poll} methods waiting for a key to be queued then 0N/A * it immediately receives a {@link ClosedWatchServiceException}. Any 0N/A * valid keys associated with this watch service are {@link WatchKey#isValid 0N/A * <p> After a watch service is closed, any further attempt to invoke 0N/A * operations upon it will throw {@link ClosedWatchServiceException}. 0N/A * If this watch service is already closed then invoking this method 0N/A * @throws IOException 0N/A * if an I/O error occurs 0N/A * Retrieves and removes the next watch key, or {@code null} if none are 0N/A * @return the next watch key, or {@code null} 0N/A * @throws ClosedWatchServiceException 0N/A * if this watch service is closed 0N/A * Retrieves and removes the next watch key, waiting if necessary up to the 0N/A * specified wait time if none are yet present. 0N/A * how to wait before giving up, in units of unit 1077N/A * a {@code TimeUnit} determining how to interpret the timeout 0N/A * @return the next watch key, or {@code null} 0N/A * @throws ClosedWatchServiceException 1077N/A * if this watch service is closed, or it is closed while waiting 1077N/A * @throws InterruptedException 1077N/A * if interrupted while waiting 0N/A * Retrieves and removes next watch key, waiting if none are yet present. 0N/A * @return the next watch key 0N/A * @throws ClosedWatchServiceException 0N/A * if this watch service is closed, or it is closed while waiting 0N/A * @throws InterruptedException 0N/A * if interrupted while waiting