src/java.base/share/classes/sun/nio/fs/PollingWatchService.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.nio.file.attribute.*;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.security.PrivilegedActionException;
  34 import java.io.IOException;
  35 import java.util.*;
  36 import java.util.concurrent.*;
  37 import com.sun.nio.file.SensitivityWatchEventModifier;
  38 import sun.misc.ManagedLocalsThread;
  39 
  40 /**
  41  * Simple WatchService implementation that uses periodic tasks to poll
  42  * registered directories for changes.  This implementation is for use on
  43  * operating systems that do not have native file change notification support.
  44  */
  45 
  46 class PollingWatchService
  47     extends AbstractWatchService
  48 {
  49     // map of registrations
  50     private final Map<Object,PollingWatchKey> map =
  51         new HashMap<Object,PollingWatchKey>();
  52 
  53     // used to execute the periodic tasks that poll for changes
  54     private final ScheduledExecutorService scheduledExecutor;
  55 
  56     PollingWatchService() {
  57         // TBD: Make the number of threads configurable
  58         scheduledExecutor = Executors
  59             .newSingleThreadScheduledExecutor(new ThreadFactory() {
  60                  @Override
  61                  public Thread newThread(Runnable r) {
  62                      Thread t = new ManagedLocalsThread(r);
  63                      t.setDaemon(true);
  64                      return t;
  65                  }});
  66     }
  67 
  68     /**
  69      * Register the given file with this watch service
  70      */
  71     @Override
  72     WatchKey register(final Path path,
  73                       WatchEvent.Kind<?>[] events,
  74                       WatchEvent.Modifier... modifiers)
  75          throws IOException
  76     {
  77         // check events - CCE will be thrown if there are invalid elements
  78         final Set<WatchEvent.Kind<?>> eventSet =
  79             new HashSet<WatchEvent.Kind<?>>(events.length);
  80         for (WatchEvent.Kind<?> event: events) {
  81             // standard events
  82             if (event == StandardWatchEventKinds.ENTRY_CREATE ||




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.nio.file.attribute.*;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.security.PrivilegedActionException;
  34 import java.io.IOException;
  35 import java.util.*;
  36 import java.util.concurrent.*;
  37 import com.sun.nio.file.SensitivityWatchEventModifier;

  38 
  39 /**
  40  * Simple WatchService implementation that uses periodic tasks to poll
  41  * registered directories for changes.  This implementation is for use on
  42  * operating systems that do not have native file change notification support.
  43  */
  44 
  45 class PollingWatchService
  46     extends AbstractWatchService
  47 {
  48     // map of registrations
  49     private final Map<Object,PollingWatchKey> map =
  50         new HashMap<Object,PollingWatchKey>();
  51 
  52     // used to execute the periodic tasks that poll for changes
  53     private final ScheduledExecutorService scheduledExecutor;
  54 
  55     PollingWatchService() {
  56         // TBD: Make the number of threads configurable
  57         scheduledExecutor = Executors
  58             .newSingleThreadScheduledExecutor(new ThreadFactory() {
  59                  @Override
  60                  public Thread newThread(Runnable r) {
  61                      Thread t = new Thread(null, r, "FileSystemWatchService", 0, false);
  62                      t.setDaemon(true);
  63                      return t;
  64                  }});
  65     }
  66 
  67     /**
  68      * Register the given file with this watch service
  69      */
  70     @Override
  71     WatchKey register(final Path path,
  72                       WatchEvent.Kind<?>[] events,
  73                       WatchEvent.Modifier... modifiers)
  74          throws IOException
  75     {
  76         // check events - CCE will be thrown if there are invalid elements
  77         final Set<WatchEvent.Kind<?>> eventSet =
  78             new HashSet<WatchEvent.Kind<?>>(events.length);
  79         for (WatchEvent.Kind<?> event: events) {
  80             // standard events
  81             if (event == StandardWatchEventKinds.ENTRY_CREATE ||