Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9e1ff0eb839e64f6390abbfcd188018c2f68eb4 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.tcf.core.streams;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IProcesses;

/**
 * Remote context streams data receiver implementation.
 */
public class StreamsDataReceiver extends PlatformObject {
	// The associated writer instance
	private final Writer writer;
	// The list of applicable stream type id's
	private final List<String> streamTypeIds;
	// The list of registered listener
	private final ListenerList listeners;

	/**
	 * An interface to be implemented by listeners who want to listen
	 * to the streams data without interfering with the original data receiver.
	 * <p>
	 * Listeners are asynchronously invoked in the TCF dispatch thread.
	 */
	public static interface Listener {

		/**
		 * Signals that some data has been received by this streams data
		 * receiver.
		 *
		 * @param data The data received. Must not be <code>null</code>.
		 */
		public void dataReceived(String data);
	}

	/**
	 * Constructor.
	 *
	 * @param writer The writer instance. Must not be <code>null</code>.
	 * @param streamTypeIds The list of applicable stream type id's or <code>null</code>.
	 *
	 * @see IProcesses
	 */
	public StreamsDataReceiver(Writer writer, String[] streamTypeIds) {
		Assert.isNotNull(writer);
		this.writer = writer;
		this.streamTypeIds = streamTypeIds != null ? Arrays.asList(streamTypeIds) : null;
		this.listeners = new ListenerList();
	}

	/**
	 * Register a streams data receiver listener.
	 *
	 * @param listener The listener. Must not be <code>null</code>.
	 */
	public final void addListener(Listener listener) {
		Assert.isNotNull(listener);
		listeners.add(listener);
	}

	/**
	 * Unregister a streams data receiver listener.
	 *
	 * @param listener The listener. Must not be <code>null</code>.
	 */
	public final void removeListener(Listener listener) {
		Assert.isNotNull(listener);
		listeners.remove(listener);
	}

	/**
	 * Notify registered streams data receiver listener.
	 *
	 * @param data The data received. Must not be <code>null</code>.
	 */
	public final void notifyListener(final String data) {
		Assert.isNotNull(data);

		final Object[] listeners = this.listeners.getListeners();
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				for (Object listener : listeners) {
					Assert.isTrue(listener instanceof Listener);
					((Listener)listener).dataReceived(data);
				}
			}
		});
	}

	/**
	 * Dispose the data receiver instance.
	 */
	public void dispose() {
		listeners.clear();

		try {
			writer.close();
		}
		catch (IOException e) {
			/* ignored on purpose */
		}
	}

	/**
	 * Returns the associated writer instance.
	 *
	 * @return The associated writer instance.
	 */
	public final Writer getWriter() {
		return writer;
	}

	/**
	 * Returns if or if not the given stream type id is applicable for this data receiver.
	 *
	 * @param streamTypeId The stream type id. Must not be <code>null</code>.
	 * @return <code>True</code> if the given stream type id is applicable for this data receiver, <code>false</code>
	 *         otherwise.
	 */
	public final boolean isApplicable(String streamTypeId) {
		Assert.isNotNull(streamTypeId);
		return streamTypeIds == null || streamTypeIds.contains(streamTypeId);
	}
}

Back to the top