Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07317d4ee8d659c971320d5e94c2af6dbb266e1d (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
/*******************************************************************************
 * Copyright (c) 2011, 2014 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.streams;

import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.te.core.terminals.interfaces.ITerminalServiceOutputStreamMonitorListener;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl;
import org.eclipse.tm.internal.terminal.provisional.api.provider.TerminalConnectorImpl;
import org.eclipse.ui.services.IDisposable;

/**
 * Streams connector implementation.
 */
@SuppressWarnings("restriction")
public abstract class AbstractStreamsConnector extends TerminalConnectorImpl {
	// Reference to the stdin monitor
    private InputStreamMonitor stdInMonitor;
    // Reference to the stdout monitor
    private OutputStreamMonitor stdOutMonitor;
    // Reference to the stderr monitor
    private OutputStreamMonitor stdErrMonitor;

    // Reference to the list of stdout output listeners
    private ITerminalServiceOutputStreamMonitorListener[] stdoutListeners = null;
    // Reference to the list of stderr output listeners
    private ITerminalServiceOutputStreamMonitorListener[] stderrListeners = null;

    /**
     * Set the list of stdout listeners.
     *
     * @param listeners The list of stdout listeners or <code>null</code>.
     */
    protected final void setStdoutListeners(ITerminalServiceOutputStreamMonitorListener[] listeners) {
    	this.stdoutListeners = listeners;
    }

    /**
     * Set the list of stderr listeners.
     *
     * @param listeners The list of stderr listeners or <code>null</code>.
     */
    protected final void setStderrListeners(ITerminalServiceOutputStreamMonitorListener[] listeners) {
    	this.stderrListeners = listeners;
    }

    /**
     * Connect the given streams. The streams connector will wrap each stream
     * with a corresponding terminal stream monitor.
     *
     * @param terminalControl The terminal control. Must not be <code>null</code>.
     * @param stdin The stdin stream or <code>null</code>.
     * @param stdout The stdout stream or <code>null</code>.
     * @param stderr The stderr stream or <code>null</code>.
	 * @param localEcho Local echo on or off.
	 * @param lineSeparator The line separator used by the stream.
     */
    protected void connectStreams(ITerminalControl terminalControl, OutputStream stdin, InputStream stdout, InputStream stderr, boolean localEcho, String lineSeparator) {
    	Assert.isNotNull(terminalControl);

    	// Create the input stream monitor
    	if (stdin != null) {
    		stdInMonitor = createStdInMonitor(terminalControl, stdin, localEcho, lineSeparator);
    		// Register the connector if it implements IDisposable and stdout/stderr are not monitored
    		if (stdout == null && stderr == null && this instanceof IDisposable) stdInMonitor.addDisposable((IDisposable)this);
    		// Start the monitoring
    		stdInMonitor.startMonitoring();
    	}

    	// Create the output stream monitor
    	if (stdout != null) {
    		stdOutMonitor = createStdOutMonitor(terminalControl, stdout, lineSeparator);
    		// Register the connector if it implements IDisposable
    		if (this instanceof IDisposable) stdOutMonitor.addDisposable((IDisposable)this);
    		// Register the listeners
    		if (stdoutListeners != null) {
    			for (ITerminalServiceOutputStreamMonitorListener l : stdoutListeners) {
    				stdOutMonitor.addListener(l);
    			}
    		}
    		// Start the monitoring
    		stdOutMonitor.startMonitoring();
    	}

    	// Create the error stream monitor
    	if (stderr != null) {
    		stdErrMonitor = createStdErrMonitor(terminalControl, stderr, lineSeparator);
    		// Register the connector if it implements IDisposable and stdout is not monitored
    		if (stdout == null && this instanceof IDisposable) stdErrMonitor.addDisposable((IDisposable)this);
    		// Register the listeners
    		if (stderrListeners != null) {
    			for (ITerminalServiceOutputStreamMonitorListener l : stderrListeners) {
    				stdErrMonitor.addListener(l);
    			}
    		}
    		// Start the monitoring
    		stdErrMonitor.startMonitoring();
    	}
    }

    /**
     * Creates an stdin monitor for the given terminal control and stdin stream.
     * Subclasses may override to create a specialized stream monitor.
     *
     * @param terminalControl The terminal control. Must not be <code>null</code>.
     * @param stdin The stdin stream or <code>null</code>.
	 * @param localEcho Local echo on or off.
	 * @param lineSeparator The line separator used by the stream.
	 *
     * @return input stream monitor
     */
    protected InputStreamMonitor createStdInMonitor(ITerminalControl terminalControl, OutputStream stdin, boolean localEcho, String lineSeparator) {
        return new InputStreamMonitor(terminalControl, stdin, localEcho, lineSeparator);
    }

    /**
     * Creates an stdout monitor for the given terminal control and stdout stream.
     * Subclasses may override to create a specialized stream monitor.
     *
     * @param terminalControl The terminal control. Must not be <code>null</code>.
     * @param stdout The stdout stream or <code>null</code>.
	 * @param lineSeparator The line separator used by the stream.
	 *
     * @return output stream monitor
     */
    protected OutputStreamMonitor createStdOutMonitor(ITerminalControl terminalControl, InputStream stdout, String lineSeparator) {
        return new OutputStreamMonitor(terminalControl, stdout, lineSeparator);
    }

    /**
     * Creates an stderr monitor for the given terminal control and stderr stream.
     * Subclasses may override to create a specialized stream monitor.
     *
     * @param terminalControl The terminal control. Must not be <code>null</code>.
     * @param stderr The stderr stream or <code>null</code>.
	 * @param lineSeparator The line separator used by the stream.
	 *
     * @return output stream monitor
     */
    protected OutputStreamMonitor createStdErrMonitor(ITerminalControl terminalControl, InputStream stderr, String lineSeparator) {
        return new OutputStreamMonitor(terminalControl, stderr, lineSeparator);
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.internal.terminal.provisional.api.provider.TerminalConnectorImpl#doDisconnect()
     */
    @Override
    protected void doDisconnect() {
    	// Dispose the streams
        if (stdInMonitor != null) { stdInMonitor.dispose(); stdInMonitor = null; }
        if (stdOutMonitor != null) { stdOutMonitor.dispose(); stdOutMonitor = null; }
        if (stdErrMonitor != null) { stdErrMonitor.dispose(); stdErrMonitor = null; }

    	super.doDisconnect();
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.internal.terminal.provisional.api.provider.TerminalConnectorImpl#getTerminalToRemoteStream()
	 */
	@Override
	public OutputStream getTerminalToRemoteStream() {
		return stdInMonitor;
	}

}

Back to the top