1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.openide.util.Cancellable; 23 import org.openide.util.NbBundle; 24 import org.openide.ErrorManager; 25 import org.netbeans.api.progress.ProgressHandle; 26 import org.netbeans.api.progress.ProgressHandleFactory; 27 28 import javax.swing.*; 29 import java.util.*; 30 import java.awt.event.ActionEvent ; 31 32 45 public final class ExecutorGroup extends AbstractAction implements Cancellable { 46 47 private final String name; 48 private final boolean abortOnExecutorFailure; 49 public boolean executed; 50 private boolean cancelled; 51 private List listeners = new ArrayList(2); 52 private List executors = new ArrayList(2); 53 private List cleanups = new ArrayList(2); 54 55 private Map queues = new HashMap(); 56 57 private Set started = new HashSet(); 58 61 private ProgressHandle progressHandle; 62 private long dataCounter; 63 private boolean hasBarrier; 64 private boolean failed; 65 private boolean executingCleanup; 66 private boolean nonInteractive; 67 68 78 public ExecutorGroup(String displayName) { 79 this(displayName, true); 80 } 81 82 public ExecutorGroup(String displayName, boolean abortOnExecutorFailure) { 83 name = displayName; 84 this.abortOnExecutorFailure = abortOnExecutorFailure; 85 } 86 87 90 public String getDisplayName() { 91 return name; 92 } 93 94 100 public synchronized void progress(String details) { 101 if (nonInteractive) return; 102 if (progressHandle == null) { 103 progressHandle = ProgressHandleFactory.createHandle(NbBundle.getMessage(ExecutorGroup.class, "BK2001", name), this, this); 104 progressHandle.start(); 105 } 106 107 if (details != null) { 108 progressHandle.progress(details); 109 } 110 } 111 112 113 120 synchronized void enqueued(ClientRuntime queue, Object id) { 121 progress(null); 122 if (progressHandle != null && started.size() == 0) { 123 progressHandle.setDisplayName(NbBundle.getMessage(ExecutorGroup.class, "BK2005", name)); 124 progressHandle.progress(NbBundle.getMessage(ExecutorGroup.class, "BK1007")); 125 progressHandle.switchToDeterminate(100); 126 progressHandle.progress(1); 127 } 128 129 Collection keys; 130 if (queue == null) { 131 keys = queues.keySet(); 132 } else { 133 keys = Collections.singleton(queue); 134 } 135 136 Iterator it = keys.iterator(); 137 while (it.hasNext()) { 138 Object key = it.next(); 139 Set commands = (Set) queues.get(key); 140 if (commands == null) { 141 commands = new HashSet(); 142 } 143 commands.add(id); 144 queues.put(key, commands); 145 } 146 } 147 148 151 synchronized void started(ClientRuntime queue) { 152 153 if (progressHandle != null) { 154 progressHandle.switchToIndeterminate(); 155 progressHandle.setDisplayName(NbBundle.getMessage(ExecutorGroup.class, "BK2001", name)); 156 } 157 158 if (!nonInteractive && started.add(queue)) { 159 String msg = NbBundle.getMessage(ExecutorGroup.class, "BK1001", new Date(), getDisplayName()); 160 String sep = NbBundle.getMessage(ExecutorGroup.class, "BK1000"); 161 String header = "\n" + sep + "\n" + msg + "\n"; queue.log(header); 163 } 164 } 165 166 172 synchronized void finished(ClientRuntime queue, Object id) { 173 174 Collection keys; 175 if (queue == null) { 176 keys = new HashSet(queues.keySet()); 177 } else { 178 keys = Collections.singleton(queue); 179 } 180 181 boolean finished = executed; Iterator it = keys.iterator(); 183 while (it.hasNext()) { 184 Object key = it.next(); 185 186 Set commands = (Set) queues.get(key); 187 commands.remove(id); 188 if (commands.isEmpty()) { 189 queues.remove(key); 190 if (executed && queues.isEmpty() && progressHandle != null) { 191 progressHandle.finish(); 192 progressHandle = null; 193 } 194 } 195 finished &= commands.isEmpty(); 196 } 197 198 if (finished) { 199 logFinished(queue); 200 } 201 } 202 203 private void logFinished(ClientRuntime queue) { 204 if (nonInteractive) return; 205 Collection consoles; 206 if (queue == null) { 207 consoles = started; 208 } else { 209 consoles = Collections.singleton(queue); 210 } 211 212 String msg; 213 if (isCancelled()) { 214 msg = NbBundle.getMessage(ExecutorGroup.class, "BK1006", new Date(), getDisplayName()); 215 } else { 216 msg = NbBundle.getMessage(ExecutorGroup.class, "BK1002", new Date(), getDisplayName()); 217 } 218 219 Iterator it2 = consoles.iterator(); 220 while (it2.hasNext()) { 221 ClientRuntime console = (ClientRuntime) it2.next(); 222 console.log(msg + "\n"); console.flushLog(); 224 } 225 } 226 227 public boolean isCancelled() { 228 return cancelled; 229 } 230 231 235 public boolean cancel() { 236 cancelled = true; 237 fail(); 238 return true; 239 } 240 241 244 public void fail() { 245 if (!abortOnExecutorFailure) return; 246 failed = true; 247 Iterator it; 248 synchronized(listeners) { 249 it = new ArrayList(listeners).iterator(); 250 } 251 while (it.hasNext()) { 252 try { 253 Cancellable cancellable = (Cancellable) it.next(); 254 cancellable.cancel(); 255 } catch (RuntimeException ex) { 256 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 257 } 258 } 259 synchronized(this) { 260 if (progressHandle != null) { 261 progressHandle.finish(); 262 progressHandle = null; 263 } 264 } 265 } 266 267 270 public void addCancellable(Cancellable cancellable) { 271 synchronized(listeners) { 272 listeners.add(cancellable); 273 } 274 } 275 276 public void removeCancellable(Cancellable cancellable) { 277 synchronized(listeners) { 278 listeners.remove(cancellable); 279 } 280 } 281 282 285 public synchronized void addExecutor(ExecutorSupport executor) { 286 assert executed == false; 287 executor.joinGroup(this); executors.add(executor); 289 } 290 291 295 public final synchronized void addExecutors(ExecutorSupport[] executors) { 296 if (executors == null) { 297 return; 298 } else { 299 for (int i = 0; i < executors.length; i++) { 300 ExecutorSupport support = executors[i]; 301 addExecutor(support); 302 } 303 } 304 } 305 306 313 public synchronized void addBarrier(Runnable action) { 314 assert executed == false; 315 ExecutorGroupBar bar = new ExecutorGroupBar(executors, action); 316 bar.joinGroup(this); 317 executors.add(bar); 318 hasBarrier = true; 319 } 320 321 324 public synchronized void addCleanups(ExecutorSupport[] executors) { 325 if (executors == null) { 326 return; 327 } else { 328 for (int i = 0; i < executors.length; i++) { 329 ExecutorSupport support = executors[i]; 330 addCleanup(support); 331 } 332 } 333 } 334 335 338 public synchronized void addCleanup(ExecutorSupport executor) { 339 assert executingCleanup == false; 340 executor.joinGroup(this); 341 cleanups.add(executor); 342 } 343 344 356 public void execute() { 357 assert (SwingUtilities.isEventDispatchThread() && hasBarrier) == false; 358 359 synchronized(this) { 360 executed = true; 361 } 362 Iterator it = executors.iterator(); 363 int i = 0; 364 while (it.hasNext()) { 365 Groupable support = (Groupable) it.next(); 366 try { 367 support.execute(); 368 } catch (Error err) { 369 ErrorManager.getDefault().notify(err); 370 fail(); 371 } catch (RuntimeException ex) { 372 ErrorManager.getDefault().notify(ex); 373 fail(); 374 } 375 i++; 376 if (failed) break; 377 } 378 379 381 synchronized(this) { 382 executingCleanup = true; 383 } 384 it = cleanups.iterator(); 385 while (it.hasNext()) { 386 Groupable support = (Groupable) it.next(); 387 support.execute(); 388 i++; 389 } 390 391 synchronized(this) { 392 if (i == 0 && progressHandle != null) { progressHandle.finish(); 394 progressHandle = null; 395 } 396 } 397 } 398 399 406 public synchronized void executed() { 407 if (executed == false) { 408 if (progressHandle != null) { 409 progressHandle.finish(); 410 progressHandle = null; 411 } 412 logFinished(null); 413 } 414 } 415 416 synchronized void increaseDataCounter(long bytes) { 417 dataCounter += bytes; 418 if (progressHandle != null) { progressHandle.progress(NbBundle.getMessage(ExecutorGroup.class, "BK2002", name, format(dataCounter))); 420 } 421 } 422 423 private static String format(long counter) { 424 if (counter < 1024*16) { 425 return NbBundle.getMessage(ExecutorGroup.class, "BK2003", new Long (counter)); 426 } 427 counter /= 1024; 428 return NbBundle.getMessage(ExecutorGroup.class, "BK2004", new Long (counter)); 429 430 } 434 435 438 public void actionPerformed(ActionEvent e) { 439 if (queues != null) { 440 Set keys = queues.keySet(); 441 if (keys.isEmpty() == false) { 442 ClientRuntime queue = (ClientRuntime) keys.iterator().next(); 443 queue.focusLog(); 444 } 445 } 446 } 447 448 public void setNonInteractive(boolean nonInteractive) { 449 this.nonInteractive = nonInteractive; 450 } 451 452 public static interface Groupable { 453 454 460 void joinGroup(ExecutorGroup group); 461 462 463 void execute(); 464 } 465 } 466 | Popular Tags |