Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9cce8b650f50d46257df550e5b8b9c6bbfc20e4 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2011, 2015 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.terminals.process;

import org.eclipse.core.runtime.Assert;


/**
 * Process monitor implementation.
 */
public class ProcessMonitor {
	// Reference to the parent process connector
	private final ProcessConnector processConnector;
	// Reference to the monitored process
	private final Process process;
	// Reference to the monitor thread
	private Thread thread;
	// Flag to mark the monitor disposed
	private boolean disposed;


    /**
     * Constructor.
     *
     * @param processConnector The parent process connector. Must not be <code>null</code>.
     */
    public ProcessMonitor(ProcessConnector processConnector) {
        super();

        Assert.isNotNull(processConnector);
		this.processConnector = processConnector;

		// Query the monitored process for easier access
		this.process = processConnector.getProcess();
    }

    /**
     * Dispose the process monitor.
     */
	public void dispose() {
    	// Set the disposed status
    	disposed = true;
    	// Not initialized -> return immediately
    	if (thread == null) return;

    	// Copy the reference
    	final Thread oldThread = thread;
    	// Unlink the monitor from the thread
    	thread = null;
    	// And interrupt the writer thread
    	oldThread.interrupt();
    }

    /**
     * Starts the terminal output stream monitor.
     */
    public void startMonitoring() {
    	// If already initialized -> return immediately
    	if (thread != null) return;

    	// Create a new runnable which is constantly reading from the stream
    	Runnable runnable = new Runnable() {
    		@Override
			public void run() {
    			monitorProcess();
    		}
    	};

    	// Create the monitor thread
    	thread = new Thread(runnable, "Terminal Process Monitor Thread"); //$NON-NLS-1$

    	// Configure the monitor thread
        thread.setDaemon(true);

        // Start the processing
        thread.start();
    }

    /**
     * Monitors the associated system process, waiting for it to terminate,
     * and notifies the associated process monitor's.
     */
    @SuppressWarnings("restriction")
	public void monitorProcess() {
    	// If already disposed -> return immediately
    	if (disposed) return;

    	try {
    		// Wait for the monitored process to terminate
    		process.waitFor();
    	} catch (InterruptedException ie) {
    		// clear interrupted state
    		Thread.interrupted();
    	} finally {
    		// Dispose the parent process connector
    		if (!disposed)
    			processConnector.disconnect();
    	}
    }
}

Back to the top