Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 541c6fce166e62a3d0cd86dbc902be83e89948ab (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2014, 2015 Wind River Systems, Inc.
 * 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.remote.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.EventObject;

import org.eclipse.remote.core.AbstractRemoteProcess;
import org.eclipse.tcf.services.IProcesses.ProcessContext;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.interfaces.events.IEventListener;
import org.eclipse.tcf.te.tcf.processes.core.launcher.ProcessLauncher;
import org.eclipse.tcf.te.tcf.processes.core.launcher.ProcessStateChangeEvent;

public class TCFProcess extends AbstractRemoteProcess implements IEventListener {
	private InputStream fStdout;
	private InputStream fStderr;
	private OutputStream fStdin;

	private PipedOutputStream fCombinedOutput;
	private int fReadersDone;

	private final ProcessLauncher fLauncher;
	private int fExitValue;
	private boolean fCompleted;

	private class StreamForwarder implements Runnable {
		private final static int BUF_SIZE = 8192;

		private final InputStream fInput;
		private final OutputStream fOutput;

		public StreamForwarder(InputStream input, OutputStream output) {
			fInput = input;
			fOutput = output;
		}

		@Override
        public void run() {
			int len;
			byte b[] = new byte[BUF_SIZE];
			try {
				while ((len = fInput.read(b)) > 0) {
					fOutput.write(b, 0, len);
				}
			} catch (IOException e) {
			}
			onProcReaderDone();
		}
	}

	private static class NullInputStream extends InputStream {
        public NullInputStream() {
        }

		@Override
		public int read() throws IOException {
			return -1;
		}

		@Override
		public int available() {
			return 0;
		}
	}

	public TCFProcess(ProcessLauncher launcher) {
		fLauncher = launcher;
		EventManager.getInstance().addEventListener(this, ProcessStateChangeEvent.class);
	}

	public void connectStreams(TCFProcessStreams streams, boolean redirectStderr) throws IOException {
		if (redirectStderr) {
			fCombinedOutput = new PipedOutputStream();
			fStdout = new PipedInputStream(fCombinedOutput);
			fStderr = null;

			new Thread(new StreamForwarder(streams.getStdout(), fCombinedOutput)).start();
			new Thread(new StreamForwarder(streams.getStderr(), fCombinedOutput)).start();
		} else {
			fStdout = streams.getStdout();
			fStderr = streams.getStderr();
		}
		fStdin = streams.getStdin();
	}

	protected void onProcReaderDone() {
		synchronized (this) {
			if (++fReadersDone == 2) {
				try {
	                fCombinedOutput.close();
                } catch (IOException e) {
                }
			}
		}
    }

	@Override
	public void destroy() {
		synchronized(this) {
			if (fCompleted)
				return;
		}

		fLauncher.cancel();
		fLauncher.terminate();
		synchronized (this) {
			if (!fCompleted) {
				fExitValue = -1;
				fCompleted = true;
				notifyAll();
			}
		}
		EventManager.getInstance().removeEventListener(this);
	}

	@Override
	public int exitValue() {
		return fExitValue;
	}

	@Override
	public InputStream getErrorStream() {
		return fStderr != null ? fStderr : new NullInputStream();
	}

	@Override
	public InputStream getInputStream() {
		return fStdout != null ? fStdout : new NullInputStream();
	}

	@Override
	public OutputStream getOutputStream() {
		return fStdin;
	}

	@Override
	public int waitFor() throws InterruptedException {
		synchronized (this) {
			while (!isCompleted()) {
				wait();
			}
		}
		return exitValue();
	}

	@Override
	public boolean isCompleted() {
		return fCompleted;
	}

	@Override
    public void eventFired(EventObject event) {
		if (event instanceof ProcessStateChangeEvent) {
			ProcessStateChangeEvent pscEvent = (ProcessStateChangeEvent) event;
			if (pscEvent.getEventId().equals(ProcessStateChangeEvent.EVENT_PROCESS_TERMINATED)) {
				Object source = pscEvent.getSource();
				if ((source instanceof ProcessContext)) {
					ProcessContext context = (ProcessContext) fLauncher.getAdapter(ProcessContext.class);
					if (context != null && ((ProcessContext) source).getID().equals(context.getID())) {
						synchronized (this) {
							fExitValue = pscEvent.getExitCode();
							fCompleted = true;
							notifyAll();
						}
					}
				}
			}
		}
	}
}

Back to the top