Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf521fee5fe866c7abdaa092dfaf405cd8f9201c (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*******************************************************************************
 * 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.tcf.log.core.internal.listener;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.core.AbstractChannel;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.log.core.activator.CoreBundleActivator;
import org.eclipse.tcf.te.tcf.log.core.events.MonitorEvent;
import org.eclipse.tcf.te.tcf.log.core.interfaces.IPreferenceKeys;
import org.eclipse.tcf.te.tcf.log.core.interfaces.ITracing;
import org.eclipse.tcf.te.tcf.log.core.internal.nls.Messages;
import org.eclipse.tcf.te.tcf.log.core.manager.LogManager;

/**
 * TCF logging channel trace listener manager implementation.
 */
public class ChannelTraceListenerManager {
	/**
	 * Time format representing date and time with milliseconds.
	 */
	public final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //$NON-NLS-1$

	// The map of trace listeners per channel
	private final Map<IChannel, AbstractChannel.TraceListener> listeners = new HashMap<IChannel, AbstractChannel.TraceListener>();

	// The map of queued messaged per channel
	private final Map<IChannel, List<String>> queued = new HashMap<IChannel, List<String>>();

	/*
	 * Thread save singleton instance creation.
	 */
	private static class LazyInstanceHolder {
		public static ChannelTraceListenerManager instance = new ChannelTraceListenerManager();
	}

	/**
	 * Returns the singleton instance for the manager.
	 */
	public static ChannelTraceListenerManager getInstance() {
		return LazyInstanceHolder.instance;
	}

	/**
	 * Constructor.
	 */
	/* default */ ChannelTraceListenerManager() {
	}

	/**
	 * New channel opened. Attach a channel trace listener.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 * @param message A message or <code>null</code>.
	 */
	public void onChannelOpened(String logname, IChannel channel, String message) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// The trace listener interface does not have a onChannelOpenend method, but
		// for consistency, log the channel opening similar to the others.
		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER)) {
			CoreBundleActivator.getTraceHandler().trace("TraceListener.onChannelOpened ( " + channel + ", \"" + message + "\" )", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER, this);
		}

		// The trace listeners can be accessed only via AbstractChannel
		if (!(channel instanceof AbstractChannel)) return;

		// Get the preference key if or if not logging is enabled
		boolean loggingEnabled = CoreBundleActivator.getScopedPreferences().getBoolean(IPreferenceKeys.PREF_LOGGING_ENABLED);
		// If false, we are done here and wont create any console or trace listener.
		if (!loggingEnabled) return;

		// As the channel has just opened, there should be no trace listener, but better be safe and check
		AbstractChannel.TraceListener traceListener = listeners.remove(channel);
		if (traceListener != null) ((AbstractChannel)channel).removeTraceListener(traceListener);
		// Create a new trace listener instance
		traceListener = new ChannelTraceListener(logname, channel);
		// Attach trace listener to the channel
		((AbstractChannel)channel).addTraceListener(traceListener);
		// Remember the associated trace listener
		listeners.put(channel, traceListener);

		// Log the channel opening
		String date = DATE_FORMAT.format(new Date(System.currentTimeMillis()));

		String fullMessage = NLS.bind(Messages.ChannelTraceListener_channelOpened_message,
										new Object[] {
											date,
											Integer.toHexString(channel.hashCode()),
											message != null ? "(" + message.trim() + ")" : "" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								  		});

		// Get the file writer
		FileWriter writer = LogManager.getInstance().getWriter(logname, channel);
		if (writer != null) {
			try {
				writer.write("\n\n\n"); //$NON-NLS-1$

				// Write the queued redirects
				List<String> queue = queued.remove(channel);
				if (queue != null) {
					for (String m : queue) {
						writer.write(m);
						writer.write("\n"); //$NON-NLS-1$
					}
				}

				// Write the opened message
				writer.write(fullMessage);
				writer.write("\n"); //$NON-NLS-1$
				writer.flush();
			} catch (IOException e) {
				/* ignored on purpose */
			}
		}
		LogManager.getInstance().monitor(channel, MonitorEvent.Type.OPEN, new MonitorEvent.Message('F', fullMessage));
	}

	/**
	 * Channel is opening.
	 * <p>
	 * This is the state where {@link IPeer#openChannel()} got called but no
	 * further redirect or channel listener got invoked.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 * @param message A message or <code>null</code>.
	 */
	public void onChannelOpening(String logname, IChannel channel, String message) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER)) {
			CoreBundleActivator.getTraceHandler().trace("TraceListener.onChannelOpening ( " + channel + ", \"" + message + "\" )", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER, this);
		}

		// Get the preference key if or if not logging is enabled
		boolean loggingEnabled = CoreBundleActivator.getScopedPreferences().getBoolean(IPreferenceKeys.PREF_LOGGING_ENABLED);
		// If false, we are done here and wont create any console or trace listener.
		if (!loggingEnabled) return;

		// Log the channel opening
		String date = DATE_FORMAT.format(new Date(System.currentTimeMillis()));

		String fullMessage = NLS.bind(Messages.ChannelTraceListener_channelOpening_message,
										new Object[] {
											date,
											Integer.toHexString(channel.hashCode()),
											message != null ? "(" + message.trim() + ")" : "" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								  		});

		List<String> queue = queued.get(channel);
		if (queue == null) {
			queue = new ArrayList<String>();
			queued.put(channel, queue);
		}
		queue.add(fullMessage);
	}

	/**
	 * Channel got redirected.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 * @param message A message or <code>null</code>.
	 */
	public void onChannelRedirected(String logname, IChannel channel, String message) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER)) {
			CoreBundleActivator.getTraceHandler().trace("TraceListener.onChannelRedirected ( " + channel + ", \"" + message + "\" )", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER, this);
		}

		// Get the preference key if or if not logging is enabled
		boolean loggingEnabled = CoreBundleActivator.getScopedPreferences().getBoolean(IPreferenceKeys.PREF_LOGGING_ENABLED);
		// If false, we are done here and wont create any console or trace listener.
		if (!loggingEnabled) return;

		// Log the channel opening
		String date = DATE_FORMAT.format(new Date(System.currentTimeMillis()));

		String fullMessage = NLS.bind(Messages.ChannelTraceListener_channelRedirected_message,
										new Object[] {
											date,
											Integer.toHexString(channel.hashCode()),
											message != null ? "(" + message.trim() + ")" : "" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								  		});

		List<String> queue = queued.get(channel);
		if (queue == null) {
			queue = new ArrayList<String>();
			queued.put(channel, queue);
		}
		queue.add(fullMessage);
	}

	/**
	 * Channel remote services got queried.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 * @param message A message or <code>null</code>.
	 */
	public void onChannelServices(String logname, IChannel channel, String message) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER)) {
			CoreBundleActivator.getTraceHandler().trace("TraceListener.onChannelServices ( " + channel + ", \"" + message + "\" )", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER, this);
		}

		// Get the preference key if or if not logging is enabled
		boolean loggingEnabled = CoreBundleActivator.getScopedPreferences().getBoolean(IPreferenceKeys.PREF_LOGGING_ENABLED);
		// If false, we are done here and wont create any console or trace listener.
		if (!loggingEnabled) return;

		// Log the channel opening
		String date = DATE_FORMAT.format(new Date(System.currentTimeMillis()));

		String fullMessage = NLS.bind(Messages.ChannelTraceListener_channelServices_message,
										new Object[] {
											date,
											Integer.toHexString(channel.hashCode()),
											message != null ? "(" + message.trim() + ")" : "" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								  		});

		List<String> queue = queued.get(channel);
		if (queue == null) {
			queue = new ArrayList<String>();
			queued.put(channel, queue);
		}
		queue.add(fullMessage);
	}

	/**
	 * Mark an event in the channel log.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 * @param message A message or <code>null</code>.
	 */
	public void onMark(String logname, IChannel channel, String message) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// The trace listener interface does not have a onChannelOpenend method, but
		// for consistency, log the channel opening similar to the others.
		if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER)) {
			CoreBundleActivator.getTraceHandler().trace("TraceListener.onMark ( " + channel + ", \"" + message + "\" )", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
														ITracing.ID_TRACE_CHANNEL_TRACE_LISTENER, this);
		}

		// Get the preference key if or if not logging is enabled
		boolean loggingEnabled = CoreBundleActivator.getScopedPreferences().getBoolean(IPreferenceKeys.PREF_LOGGING_ENABLED);
		// If false, we are done here and wont create any console or trace listener.
		if (!loggingEnabled) return;

		// Log the channel opening
		String date = DATE_FORMAT.format(new Date(System.currentTimeMillis()));

		String fullMessage = NLS.bind(Messages.ChannelTraceListener_channelMark_message,
										new Object[] {
											date,
											Integer.toHexString(channel.hashCode()),
											message != null ? "(" + message.trim() + ")" : "" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								  		});

		// Get the file writer
		FileWriter writer = LogManager.getInstance().getWriter(logname, channel);
		if (writer != null) {
			try {
				// Write the message
				writer.write(fullMessage);
				writer.write("\n"); //$NON-NLS-1$
				writer.flush();
			} catch (IOException e) {
				/* ignored on purpose */
			}
		}
	}

	/**
	 * Channel closed. Detach the channel trace listener if any.
	 *
	 * @param logname The log name or <code>null</code>.
	 * @param channel The channel. Must not be <code>null</code>.
	 */
	public void onChannelClosed(String logname, final IChannel channel) {
		Assert.isNotNull(channel);
		Assert.isTrue(Protocol.isDispatchThread(), "Illegal Thread Access"); //$NON-NLS-1$

		// Remove the queued messages
		queued.remove(channel);

		// The trace listeners can be accessed only via AbstractChannel
		if (!(channel instanceof AbstractChannel)) return;

		// Remove the trace listener if any
		final AbstractChannel.TraceListener traceListener = listeners.remove(channel);
		if (traceListener != null) {
			Protocol.invokeLater(new Runnable() {
				@Override
				public void run() {
					((AbstractChannel)channel).removeTraceListener(traceListener);
				}
			});
		}
	}
}

Back to the top