WatchService.java revision 51b1b6997fd3f980076b8081f7f1165ccc2a4008
1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/*
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
5b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * This code is free software; you can redistribute it and/or modify it
6b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * under the terms of the GNU General Public License version 2 only, as
7b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * published by the Free Software Foundation.  Oracle designates this
8b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * particular file as subject to the "Classpath" exception as provided
9b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * by Oracle in the LICENSE file that accompanied this code.
10b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * This code is distributed in the hope that it will be useful, but WITHOUT
12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * version 2 for more details (a copy is included in the LICENSE file that
15b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * accompanied this code).
16b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
17014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * You should have received a copy of the GNU General Public License version
18014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * 2 along with this work; if not, write to the Free Software Foundation,
19014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch *
21b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * or visit www.oracle.com if you need additional information or have any
23b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * questions.
24b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch */
25b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
26b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochpackage java.nio.file;
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
28014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochimport java.io.Closeable;
29b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport java.io.IOException;
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport java.util.concurrent.TimeUnit;
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
32b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch/**
33b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * A watch service that <em>watches</em> registered objects for changes and
34b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * events. For example a file manager may use a watch service to monitor a
35b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * directory for changes so that it can update its display of the list of files
36b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * when files are created or deleted.
37b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
38b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> A {@link Watchable} object is registered with a watch service by invoking
39b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * its {@link Watchable#register register} method, returning a {@link WatchKey}
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * to represent the registration. When an event for an object is detected the
41b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * key is <em>signalled</em>, and if not currently signalled, it is queued to
42f91f0611dbaf29ca0f1d4aecb357ce243a19d2faBen Murdoch * the watch service so that it can be retrieved by consumers that invoke the
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * {@link #poll() poll} or {@link #take() take} methods to retrieve keys
44b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * and process events. Once the events have been processed the consumer
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * invokes the key's {@link WatchKey#reset reset} method to reset the key which
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * allows the key to be signalled and re-queued with further events.
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> Registration with a watch service is cancelled by invoking the key's
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * {@link WatchKey#cancel cancel} method. A key that is queued at the time that
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * it is cancelled remains in the queue until it is retrieved. Depending on the
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * object, a key may be cancelled automatically. For example, suppose a
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * directory is watched and the watch service detects that it has been deleted
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * or its file system is no longer accessible. When a key is cancelled in this
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * manner it is signalled and queued, if not currently signalled. To ensure
55b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * that the consumer is notified the return value from the {@code reset}
56b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * method indicates if the key is valid.
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> A watch service is safe for use by multiple concurrent consumers. To
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * ensure that only one consumer processes the events for a particular object at
60014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * any time then care should be taken to ensure that the key's {@code reset}
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * method is only invoked after its events have been processed. The {@link
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * #close close} method may be invoked at any time to close the service causing
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * any threads waiting to retrieve keys, to throw {@code
64b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * ClosedWatchServiceException}.
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
66b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> File systems may report events faster than they can be retrieved or
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * processed and an implementation may impose an unspecified limit on the number
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * of events that it may accumulate. Where an implementation <em>knowingly</em>
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * discards events then it arranges for the key's {@link WatchKey#pollEvents
70b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * pollEvents} method to return an element with an event type of {@link
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * StandardWatchEventKinds#OVERFLOW OVERFLOW}. This event can be used by the
72b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * consumer as a trigger to re-examine the state of the object.
73b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
74b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> When an event is reported to indicate that a file in a watched directory
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * has been modified then there is no guarantee that the program (or programs)
76b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * that have modified the file have completed. Care should be taken to coordinate
77b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * access with other programs that may be updating the file.
78b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * The {@link java.nio.channels.FileChannel FileChannel} class defines methods
79b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * to lock regions of a file against access by other programs.
80b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
81b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <h4>Platform dependencies</h4>
82b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
83b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> The implementation that observes events from the file system is intended
84b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * to map directly on to the native file event notification facility where
85b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * available, or to use a primitive mechanism, such as polling, when a native
86b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * facility is not available. Consequently, many of the details on how events
87b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * are detected, their timeliness, and whether their ordering is preserved are
88b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * highly implementation specific. For example, when a file in a watched
89b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * directory is modified then it may result in a single {@link
90014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * StandardWatchEventKinds#ENTRY_MODIFY ENTRY_MODIFY} event in some
91b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * implementations but several events in other implementations. Short-lived
92b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * files (meaning files that are deleted very quickly after they are created)
93b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * may not be detected by primitive implementations that periodically poll the
94b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * file system to detect changes.
95b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
96b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * <p> If a watched file is not located on a local storage device then it is
97b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * implementation specific if changes to the file can be detected. In particular,
98b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * it is not required that changes to files carried out on remote systems be
99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch * detected.
100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch *
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch * @since 1.7
102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch *
103 * @see FileSystem#newWatchService
104 */
105
106public interface WatchService
107    extends Closeable
108{
109
110    /**
111     * Closes this watch service.
112     *
113     * <p> If a thread is currently blocked in the {@link #take take} or {@link
114     * #poll(long,TimeUnit) poll} methods waiting for a key to be queued then
115     * it immediately receives a {@link ClosedWatchServiceException}. Any
116     * valid keys associated with this watch service are {@link WatchKey#isValid
117     * invalidated}.
118     *
119     * <p> After a watch service is closed, any further attempt to invoke
120     * operations upon it will throw {@link ClosedWatchServiceException}.
121     * If this watch service is already closed then invoking this method
122     * has no effect.
123     *
124     * @throws  IOException
125     *          if an I/O error occurs
126     */
127    @Override
128    void close() throws IOException;
129
130    /**
131     * Retrieves and removes the next watch key, or {@code null} if none are
132     * present.
133     *
134     * @return  the next watch key, or {@code null}
135     *
136     * @throws  ClosedWatchServiceException
137     *          if this watch service is closed
138     */
139    WatchKey poll();
140
141    /**
142     * Retrieves and removes the next watch key, waiting if necessary up to the
143     * specified wait time if none are yet present.
144     *
145     * @param   timeout
146     *          how to wait before giving up, in units of unit
147     * @param   unit
148     *          a {@code TimeUnit} determining how to interpret the timeout
149     *          parameter
150     *
151     * @return  the next watch key, or {@code null}
152     *
153     * @throws  ClosedWatchServiceException
154     *          if this watch service is closed, or it is closed while waiting
155     *          for the next key
156     * @throws  InterruptedException
157     *          if interrupted while waiting
158     */
159    WatchKey poll(long timeout, TimeUnit unit)
160        throws InterruptedException;
161
162    /**
163     * Retrieves and removes next watch key, waiting if none are yet present.
164     *
165     * @return  the next watch key
166     *
167     * @throws  ClosedWatchServiceException
168     *          if this watch service is closed, or it is closed while waiting
169     *          for the next key
170     * @throws  InterruptedException
171     *          if interrupted while waiting
172     */
173    WatchKey take() throws InterruptedException;
174}
175