| 1 | |
package org.webslinger.io; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.HashSet; |
| 5 | |
import java.util.concurrent.ConcurrentHashMap; |
| 6 | |
|
| 7 | |
import org.webslinger.util.BinTimer; |
| 8 | |
|
| 9 | 0 | public abstract class AbstractPollingMonitor<I, M extends AbstractPollingMonitor<I, M, W, L>, W extends AbstractPollingMonitor.PollingWatched<I, M, W, L>, L extends Monitor.Listener<I>> extends AbstractMonitor<I, M, W, L> { |
| 10 | |
protected final long delay; |
| 11 | |
|
| 12 | |
protected AbstractPollingMonitor(long delay) { |
| 13 | 0 | super(); |
| 14 | 0 | this.delay = delay; |
| 15 | 0 | } |
| 16 | |
|
| 17 | |
protected void activate(W watched) throws IOException { |
| 18 | 0 | BinTimer.schedule(watched, delay); |
| 19 | 0 | } |
| 20 | |
|
| 21 | |
protected void deactivate(W watched) throws IOException { |
| 22 | 0 | } |
| 23 | |
|
| 24 | |
protected final void reschedule(PollingWatched<I, M, W, L> watched) { |
| 25 | 0 | WatchedListener<I, M, W, L> wl = watches.get(watched.item); |
| 26 | 0 | if (wl == null) return; |
| 27 | 0 | BinTimer.schedule(watched, delay); |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
public static abstract class PollingWatched<I, M extends AbstractPollingMonitor<I, M, W, L>, W extends PollingWatched<I, M, W, L>, L extends Listener<I>> extends Watched<I, M, W, L> implements Runnable { |
| 31 | |
protected final I item; |
| 32 | |
protected boolean exists; |
| 33 | |
protected long lastModifiedTime; |
| 34 | |
protected long size; |
| 35 | |
|
| 36 | |
protected PollingWatched(M monitor, I item) { |
| 37 | 0 | super(monitor); |
| 38 | 0 | this.item = item; |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
protected void init() throws IOException { |
| 42 | 0 | super.init(); |
| 43 | 0 | refresh(); |
| 44 | 0 | exists = exists(); |
| 45 | 0 | if (exists) { |
| 46 | 0 | lastModifiedTime = lastModifiedTime(); |
| 47 | 0 | size = size(); |
| 48 | |
} |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public final void run() { |
| 52 | |
try { |
| 53 | 0 | refresh(); |
| 54 | 0 | if (exists != exists()) { |
| 55 | 0 | exists = !exists; |
| 56 | 0 | if (exists) { |
| 57 | 0 | lastModifiedTime = lastModifiedTime(); |
| 58 | 0 | size = size(); |
| 59 | 0 | monitor.fireEvent(item, Listener.Event.CREATE); |
| 60 | 0 | if (size != 0) monitor.fireEvent(item, Listener.Event.CHANGE); |
| 61 | |
} else { |
| 62 | 0 | monitor.fireEvent(item, Listener.Event.DELETE); |
| 63 | |
} |
| 64 | |
} else { |
| 65 | 0 | if (exists) { |
| 66 | 0 | long newLastModifiedTime = lastModifiedTime(); |
| 67 | 0 | long newSize = size(); |
| 68 | 0 | if (lastModifiedTime != newLastModifiedTime || size != newSize) { |
| 69 | 0 | lastModifiedTime = newLastModifiedTime; |
| 70 | 0 | size = newSize; |
| 71 | 0 | monitor.fireEvent(item, Listener.Event.CHANGE); |
| 72 | |
} |
| 73 | |
} |
| 74 | |
} |
| 75 | 0 | } catch (IOException e) { |
| 76 | |
} finally { |
| 77 | 0 | monitor.reschedule(this); |
| 78 | 0 | } |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
protected void refresh() throws IOException { |
| 82 | 0 | } |
| 83 | |
|
| 84 | |
protected abstract boolean exists() throws IOException; |
| 85 | |
protected abstract long lastModifiedTime() throws IOException; |
| 86 | |
protected abstract long size() throws IOException; |
| 87 | |
} |
| 88 | |
} |