1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.netbeans.lib.cvsclient.command.Command; 23 import org.netbeans.lib.cvsclient.command.GlobalOptions; 24 import org.netbeans.lib.cvsclient.Client; 25 import org.netbeans.lib.cvsclient.connection.AuthenticationException; 26 import org.netbeans.api.progress.ProgressHandle; 27 import org.openide.ErrorManager; 28 import org.openide.util.Cancellable; 29 import org.openide.util.RequestProcessor; 30 31 37 class CommandRunnable implements Runnable , Cancellable { 38 39 private final Client client; 40 private final GlobalOptions options; 41 private final Command cmd; 42 private Throwable failure; 43 44 private boolean aborted; 45 private Thread interruptibleThread; 46 private ExecutorSupport support; 47 48 private static boolean testRetry = Boolean.getBoolean("netbeans.debug.cvs.io.retry"); 50 public CommandRunnable(Client client, GlobalOptions options, Command cmd, ExecutorSupport support) { 51 this.client = client; 52 this.options = options; 53 this.cmd = cmd; 54 this.support = support; 55 } 56 57 public void run() { 58 synchronized(this) { 59 if (isAborted()) { 60 return; 61 } 62 support.commandStarted(this); 63 } 64 interruptibleThread = Thread.currentThread(); 65 Runnable worker = new Runnable () { 66 public void run() { 67 CounterRunnable counterUpdater = new CounterRunnable(); 68 RequestProcessor.Task counterTask = RequestProcessor.getDefault().create(counterUpdater); 69 counterUpdater.initTask(counterTask); 70 try { 71 counterTask.schedule(500); 72 if (testRetry && support.t9yRetryFlag == false) { 73 support.t9yRetryFlag = true; 74 String msg = "Testing retry logic. Retry attempt will be OK. (-Dnetbeans.debug.cvs.io.retry=true)"; throw new AuthenticationException(msg, msg); 76 } 77 client.executeCommand(cmd, options); 78 } catch (Throwable e) { 79 failure = e; 80 } finally { 81 counterTask.cancel(); 82 try { 83 client.getConnection().close(); 84 } catch (Throwable e) { 85 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 86 } 87 } 88 } 89 }; 90 Thread workerThread = new Thread (worker, "CVS I/O Worker "); workerThread.start(); 92 try { 93 workerThread.join(); 94 } catch (InterruptedException e) { 95 ErrorManager err = ErrorManager.getDefault(); 96 err.annotate(e, "Passing interrupt to possibly uninterruptible nested thread: " + workerThread + "\nCVS command: " + cmd.getCVSCommand()); workerThread.interrupt(); err.notify(ErrorManager.INFORMATIONAL, e); 99 Thread.currentThread().interrupt(); try { 102 workerThread.join(2000); 103 } catch (InterruptedException e1) { 104 } 106 } 107 } 108 109 public Throwable getFailure() { 110 return failure; 111 } 112 113 116 public synchronized boolean isAborted() { 117 return aborted; 118 } 119 120 public synchronized boolean cancel() { 121 if (aborted) { 122 return false; 123 } 124 aborted = true; 125 client.abort(); 126 if (interruptibleThread != null) { 127 interruptibleThread.interrupt(); } 129 return true; 130 } 131 132 public String toString() { 133 return "CommandRunnable command=" + cmd.getCVSCommand(); } 135 136 137 private class CounterRunnable implements Runnable { 138 139 private RequestProcessor.Task task; 140 141 private long counter; 142 143 public void run() { 144 long current = client.getCounter(); 145 long delta = current - counter; 146 counter = current; 147 support.increaseDataCounter(delta); 148 task.schedule(500); 149 } 150 151 void initTask(RequestProcessor.Task task) { 152 this.task = task; 153 } 154 } 155 } 156 | Popular Tags |