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