1 19 20 package org.netbeans.modules.settings; 21 22 import java.io.IOException ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import org.openide.filesystems.FileLock; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileSystem; 28 import org.openide.util.Exceptions; 29 import org.openide.util.RequestProcessor; 30 import org.openide.util.RequestProcessor.*; 31 32 35 public final class ScheduledRequest implements Runnable { 36 private final static RequestProcessor PROCESSOR = 37 new RequestProcessor("Settings Processor"); private final static int DELAY = 2000; 39 40 private Object inst; 41 private Task task; 42 private FileLock lock; 43 private final FileObject fobj; 44 private final FileSystem.AtomicAction run; 45 private boolean running = false; 46 47 private int counter = 0; 48 49 53 public ScheduledRequest(FileObject fobj, FileSystem.AtomicAction run) { 54 this.fobj = fobj; 55 this.run = run; 56 } 57 58 59 public boolean isRunning() { 60 return running; 61 } 62 63 67 public FileLock getFileLock() { 68 return lock; 69 } 70 71 75 public synchronized void schedule(Object obj) { 76 schedule(obj, DELAY); 77 } 78 79 private void schedule(Object obj, int delay) { 80 if (obj == null) return; 81 if (inst != null && inst != obj) 82 throw new IllegalStateException ("Inconsistant state! File: " + fobj); 84 try { 85 if (lock == null) lock = fobj.lock(); 86 } catch (IOException ex) { 87 Logger.getLogger(ScheduledRequest.class.getName()).log(Level.WARNING, null, ex); 88 return; 89 } 90 91 counter++; 92 if (task == null) { 93 task = PROCESSOR.post(this, delay); 94 } else { 95 task.schedule(delay); 96 } 97 98 this.inst = obj; 100 } 101 102 105 public synchronized void runAndWait() throws IOException { 106 if (task != null) { 107 task.cancel(); 109 counter = 0; 110 releaseResources(); 111 } 112 lock = fobj.lock(); 113 performRequest(); 114 } 115 116 117 public void forceToFinish() { 118 Task t = null; 119 synchronized (this) { 120 if (task != null) { 121 t = task; 122 task.schedule(0); 123 } 124 } 125 if (t != null) t.waitFinished(); 126 } 127 128 129 public void cancel() { 130 RequestProcessor.Task t = task; 131 if (t == null) return ; 132 133 if (isRunning()) { 134 t.waitFinished(); 135 } else { 136 synchronized (this) { 137 t.cancel(); 138 counter = 0; 139 releaseResources(); 140 } 141 } 142 } 143 144 public void run() { 145 synchronized (this) { 146 counter = 0; 147 } 148 149 try { 150 performRequest(); 151 } catch (IOException ex) { 152 Exceptions.attachLocalizedMessage(ex, fobj.toString()); 153 Logger.getLogger(ScheduledRequest.class.getName()).log(Level.WARNING, null, ex); 154 } 155 } 156 157 159 private void performRequest() throws IOException { 160 boolean isAlreadyRunning = false; 161 Task runningTask; 162 163 synchronized (this) { 164 isAlreadyRunning = running; 165 runningTask = task; 166 running = true; 167 } 168 169 if (isAlreadyRunning) { 170 runningTask.waitFinished(); 171 return; 172 } 173 174 try { 175 run.run(); 176 } finally { 177 synchronized (this) { 178 running = false; 179 if (task == null || counter == 0) { 180 releaseResources(); 181 } 182 } 183 } 184 } 185 186 private void releaseResources() { 187 if (lock != null) { 188 lock.releaseLock(); 189 lock = null; 190 } 191 inst = null; 192 task = null; 193 } 194 195 } 196 | Popular Tags |