Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: efa845a7794b1b5e85d662643a77a32b2627bfef (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

 
/**
 * Monitors a system process, wiating for it to terminate, and
 * then notifies the associated runtime process.
 */
public class ProcessMonitor {
	/**
	 * The underlying <code>java.lang.Process</code> being monitored.
	 */
	protected Process fOSProcess;	
	/**
	 * The <code>IProcess</code> which will be informed when this
	 * monitor detects that the underlying process has terminated.
	 */
	protected RuntimeProcess fProcess;

	/**
	 * The <code>Thread</code> which is monitoring the underlying process.
	 */
	protected Thread fThread;
	/**
	 * Creates a new process monitor and starts monitoring the process
	 * for termination.
	 */
	public ProcessMonitor(RuntimeProcess process) {
		fProcess= process;
		fOSProcess= process.getSystemProcess();
		startMonitoring();
	}

	/**
	 * Monitors the underlying process for termination. When the underlying
	 * process terminates (or if the monitoring thread is interrupted),
	 * inform the <code>IProcess</code> that it has terminated.
	 */
	private void monitorProcess() {
		while (fOSProcess != null) {
			try {
				fOSProcess.waitFor();
			} catch (InterruptedException ie) {
			} finally {
				fOSProcess = null;
				fProcess.terminated();
			}
		}
	}

	/**
	 * Starts monitoring the underlying process to determine
	 * if it has terminated.
	 */
	private void startMonitoring() {
		if (fThread == null) {
			fThread= new Thread(new Runnable() {
				public void run() {
					monitorProcess();
				}
			}, DebugCoreMessages.getString("ProcessMonitor.label")); //$NON-NLS-1$
			fThread.start();
		}
	}
	
	/**
	 * Kills the monitoring thread.
	 * 
	 * This method is to be useful for dealing with the error
	 * case of an underlying process which has not informed this
	 * monitor of its termination.
	 */
	protected void killMonitoring() {
		fThread.interrupt();
	}
}

Back to the top