1 /*
   2  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  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.util.*;
  30 
  31 /**
  32  * Base implementation class for watch keys.
  33  */
  34 
  35 abstract class AbstractWatchKey extends WatchKey {
  36 
  37     /**
  38      * Maximum size of event list (in the future this may be tunable)
  39      */
  40     static final int MAX_EVENT_LIST_SIZE    = 512;
  41 
  42     /**
  43      * Special event to signal overflow
  44      */
  45     static final Event<Void> OVERFLOW_EVENT =
  46         new Event<Void>(StandardWatchEventKind.OVERFLOW, null);
  47 
  48     /**
  49      * Possible key states
  50      */
  51     private static enum State { READY, SIGNALLED };
  52 
  53     // reference to watcher
  54     private final AbstractWatchService watcher;
  55 
  56     // key state
  57     private State state;
  58 
  59     // pending events
  60     private List<WatchEvent<?>> events;
  61 
  62     // maps a context to the last event for the context (iff the last queued
  63     // event for the context is an ENTRY_MODIFY event).
  64     private Map<Object,WatchEvent<?>> lastModifyEvents;
  65 
  66     protected AbstractWatchKey(AbstractWatchService watcher) {
  67         this.watcher = watcher;
  68         this.state = State.READY;
  69         this.events = new ArrayList<WatchEvent<?>>();
  70         this.lastModifyEvents = new HashMap<Object,WatchEvent<?>>();
  71     }
  72 
  73     final AbstractWatchService watcher() {
  74         return watcher;
  75     }
  76 
  77     /**
  78      * Enqueues this key to the watch service
  79      */
  80     final void signal() {
  81         synchronized (this) {
  82             if (state == State.READY) {
  83                 state = State.SIGNALLED;
  84                 watcher.enqueueKey(this);
  85             }
  86         }
  87     }
  88 
  89     /**
  90      * Adds the event to this key and signals it.
  91      */
  92     @SuppressWarnings("unchecked")
  93     final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
  94         boolean isModify = (kind == StandardWatchEventKind.ENTRY_MODIFY);
  95         synchronized (this) {
  96             int size = events.size();
  97             if (size > 0) {
  98                 // if the previous event is an OVERFLOW event or this is a
  99                 // repeated event then we simply increment the counter
 100                 WatchEvent<?> prev = events.get(size-1);
 101                 if ((prev.kind() == StandardWatchEventKind.OVERFLOW) ||
 102                     ((kind == prev.kind() &&
 103                      Objects.equals(context, prev.context()))))
 104                 {
 105                     ((Event<?>)prev).increment();
 106                     return;
 107                 }
 108 
 109                 // if this is a modify event and the last entry for the context
 110                 // is a modify event then we simply increment the count
 111                 if (!lastModifyEvents.isEmpty()) {
 112                     if (isModify) {
 113                         WatchEvent<?> ev = lastModifyEvents.get(context);
 114                         if (ev != null) {
 115                             assert ev.kind() == StandardWatchEventKind.ENTRY_MODIFY;
 116                             ((Event<?>)ev).increment();
 117                             return;
 118                         }
 119                     } else {
 120                         // not a modify event so remove from the map as the
 121                         // last event will no longer be a modify event.
 122                         lastModifyEvents.remove(context);
 123                     }
 124                 }
 125 
 126                 // if the list has reached the limit then drop pending events
 127                 // and queue an OVERFLOW event
 128                 if (size >= MAX_EVENT_LIST_SIZE) {
 129                     kind = StandardWatchEventKind.OVERFLOW;
 130                     isModify = false;
 131                     context = null;
 132                 }
 133             }
 134 
 135             // non-repeated event
 136             Event<Object> ev =
 137                 new Event<Object>((WatchEvent.Kind<Object>)kind, context);
 138             if (isModify) {
 139                 lastModifyEvents.put(context, ev);
 140             } else if (kind == StandardWatchEventKind.OVERFLOW) {
 141                 // drop all pending events
 142                 events.clear();
 143                 lastModifyEvents.clear();
 144             }
 145             events.add(ev);
 146             signal();
 147         }
 148     }
 149 
 150     @Override
 151     public final List<WatchEvent<?>> pollEvents() {
 152         synchronized (this) {
 153             List<WatchEvent<?>> result = events;
 154             events = new ArrayList<WatchEvent<?>>();
 155             lastModifyEvents.clear();
 156             return result;
 157         }
 158     }
 159 
 160     @Override
 161     public final boolean reset() {
 162         synchronized (this) {
 163             if (state == State.SIGNALLED && isValid()) {
 164                 if (events.isEmpty()) {
 165                     state = State.READY;
 166                 } else {
 167                     // pending events so re-queue key
 168                     watcher.enqueueKey(this);
 169                 }
 170             }
 171             return isValid();
 172         }
 173     }
 174 
 175     /**
 176      * WatchEvent implementation
 177      */
 178     private static class Event<T> extends WatchEvent<T> {
 179         private final WatchEvent.Kind<T> kind;
 180         private final T context;
 181 
 182         // synchronize on watch key to access/increment count
 183         private int count;
 184 
 185         Event(WatchEvent.Kind<T> type, T context) {
 186             this.kind = type;
 187             this.context = context;
 188             this.count = 1;
 189         }
 190 
 191         @Override
 192         public WatchEvent.Kind<T> kind() {
 193             return kind;
 194         }
 195 
 196         @Override
 197         public T context() {
 198             return context;
 199         }
 200 
 201         @Override
 202         public int count() {
 203             return count;
 204         }
 205 
 206         // for repeated events
 207         void increment() {
 208             count++;
 209         }
 210     }
 211 }