KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > CommandRunnable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
32  * Runnable that actually performs a command and stores
33  * an exception it may throw and its isFinished state.
34  *
35  * @author Maros Sandor
36  */

37 class CommandRunnable implements Runnable JavaDoc, Cancellable {
38
39     private final Client client;
40     private final GlobalOptions options;
41     private final Command cmd;
42     private Throwable JavaDoc failure;
43     
44     private boolean aborted;
45     private Thread JavaDoc interruptibleThread;
46     private ExecutorSupport support;
47
48     private static boolean testRetry = Boolean.getBoolean("netbeans.debug.cvs.io.retry"); // NOI18N
49

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 JavaDoc worker = new Runnable JavaDoc() {
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 JavaDoc msg = "Testing retry logic. Retry attempt will be OK. (-Dnetbeans.debug.cvs.io.retry=true)"; // NOI18N
75
throw new AuthenticationException(msg, msg);
76                     }
77                     client.executeCommand(cmd, options);
78                 } catch (Throwable JavaDoc e) {
79                     failure = e;
80                 } finally {
81                     counterTask.cancel();
82                     try {
83                         client.getConnection().close();
84                     } catch (Throwable JavaDoc e) {
85                         ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
86                     }
87                 }
88             }
89         };
90         Thread JavaDoc workerThread = new Thread JavaDoc(worker, "CVS I/O Worker "); // NOI18N
91
workerThread.start();
92         try {
93             workerThread.join();
94         } catch (InterruptedException JavaDoc e) {
95             ErrorManager err = ErrorManager.getDefault();
96             err.annotate(e, "Passing interrupt to possibly uninterruptible nested thread: " + workerThread + "\nCVS command: " + cmd.getCVSCommand()); // NOI18N
97
workerThread.interrupt(); // sometimes not interuptible e.g. while in Socket.connect()
98
err.notify(ErrorManager.INFORMATIONAL, e);
99             Thread.currentThread().interrupt(); // preserve interrupted flag
100
// let the thread finish before we return; it is (hopefully) processing the InterruptedException now
101
try {
102                 workerThread.join(2000);
103             } catch (InterruptedException JavaDoc e1) {
104                 // ignore
105
}
106         }
107     }
108
109     public Throwable JavaDoc getFailure() {
110         return failure;
111     }
112     
113     /**
114      * Cancelled?
115      */

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(); // waiting in join
128
}
129         return true;
130     }
131
132     public String JavaDoc toString() {
133         return "CommandRunnable command=" + cmd.getCVSCommand(); // NOI18N
134
}
135
136     /** Periodic task updating transmitted/received data counter. */
137     private class CounterRunnable implements Runnable JavaDoc {
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