1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.core.cmd.threads;
20
21 import nl.toolforge.karma.core.cmd.Command;
22 import nl.toolforge.karma.core.cmd.CommandException;
23 import nl.toolforge.karma.core.cmd.CommandResponse;
24 import nl.toolforge.karma.core.cmd.event.CommandResponseListener;
25
26
27 /***
28 *
29 * @author D.A. Smedes
30 *
31 * @version $Id: ParallelCommandWrapper.java,v 1.3 2004/09/23 07:18:08 asmedes Exp $
32 */
33 public final class ParallelCommandWrapper extends Thread {
34
35 private boolean running = true;
36
37 private Command command = null;
38 private CommandResponseListener listener;
39 private CommandException exception = null;
40
41 public ParallelCommandWrapper(Command command, CommandResponseListener listener) {
42 this.command = command;
43 this.listener = listener;
44 }
45
46 public CommandResponse getCommandResponse() {
47 return null;
48 }
49
50 public void run() {
51
52 try {
53
54 startRunning();
55
56 command.execute();
57
58 } catch (CommandException c) {
59 exception = c;
60 } finally {
61 stopRunning();
62 }
63 }
64
65 private synchronized void startRunning() {
66
67 running = true;
68 command.registerCommandResponseListener(listener);
69 }
70
71 private synchronized void stopRunning() {
72
73 command.deregisterCommandResponseListener(listener);
74 command.cleanUp();
75
76 running = false;
77 }
78
79 public synchronized boolean isRunning() {
80 return running;
81 }
82
83 /***
84 * Returns the <code>CommandException</code> when the <code>run</code>-method had thrown one. This method can thus be
85 * used to check the actual results (if an error occurred).
86 *
87 * @return A <code>CommandException</code> or <code>null</code> if no exception was thrown.
88 */
89 public CommandException getException() {
90 return exception;
91 }
92
93 }