Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ProcessMonitor.java
*
* created: Wed Aug 6 2003
*
* This file is part of Artemis
*
* Copyright (C) 2003 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/ProcessMonitor.java,v 1.1 2004-06-09 09:45:01 tjc Exp $
*/
package uk.ac.sanger.artemis;
import java.io.*;
/**
* Objects of this class watch a Process object and send a
* ExternalProgramEvent when the process status changes (eg. the end of a
* process).
*
* @author Kim Rutherford <kmr@sanger.ac.uk>
* @version $Id: ProcessMonitor.java,v 1.1 2004-06-09 09:45:01 tjc Exp $
**/
public class ProcessMonitor
extends SimpleExternalProgramMonitor
implements ExternalProgramMonitor
{
/** The Process that was passed to the constructor. */
final public Process process;
/**
* Create a new ProcessMonitor object for the given Process.
* @param process The Process to monitor.
* @param name The name of the process that is being monitored.
* @param logger The log for errors, STDOUT and STDERR of the Process.
**/
public ProcessMonitor(final Process process, final String name,
final Logger logger)
{
super(name, logger);
this.process = process;
}
/**
* This code will wait for a Process to change status then log it and call
* sendEvent().
**/
public void run()
{
try
{
final Reader reader = new InputStreamReader (process.getErrorStream ());
getLogger().log (reader);
}
catch(IOException e)
{
getLogger().log("cannot read the errer stream from " +
getProgramName ());
}
try
{
final Reader reader = new InputStreamReader (process.getInputStream ());
getLogger().log (reader);
}
catch (IOException e)
{
getLogger().log("cannot read the ouput stream from " +
getProgramName ());
}
while(true)
{
try
{
final int return_value = process.waitFor();
final boolean core_dumped = (return_value & 0x80) != 0;
getLogger ().log ("\n--------------------------------------" +
"---------------------\n\n");
final String log_message;
if(core_dumped)
log_message = getProgramName() + " process dumped core";
else
{
final int sig_number = return_value & 0x7f;
if (sig_number > 0)
log_message =
getProgramName() + " process received signal: " + sig_number;
else
{
final int exit_code = return_value >> 8;
if(exit_code == 0)
log_message = getProgramName() + " process completed";
else
log_message = getProgramName() +
" process finished with exit code: " + exit_code;
}
}
final ExternalProgramEvent event =
new ExternalProgramEvent(ExternalProgramEvent.FINISHED,
log_message, process);
sendEvent(event);
getLogger().log (log_message + "\n");
return;
}
catch (InterruptedException e)
{
// go around the loop again
}
}
}
}