View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }