Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8bab60e9dfbbfa6c7c10fd5d89cd0ab31d65dd1 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2011 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.scripting.launcher;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.core.AbstractChannel;
import org.eclipse.tcf.core.AbstractChannel.TraceListener;
import org.eclipse.tcf.core.Command;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IChannel.IChannelListener;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.IService;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tcf.te.tcf.core.scripting.activator.CoreBundleActivator;
import org.eclipse.tcf.te.tcf.core.scripting.events.ScriptEvent;
import org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncher;
import org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncherProperties;
import org.eclipse.tcf.te.tcf.core.scripting.nls.Messages;
import org.eclipse.tcf.te.tcf.core.scripting.parser.Parser;
import org.eclipse.tcf.te.tcf.core.scripting.parser.Token;
import org.eclipse.tcf.te.tcf.core.util.JSONUtils;

/**
 * Script launcher implementation.
 */
public class ScriptLauncher extends PlatformObject implements IScriptLauncher {
	// The channel instance
	/* default */ IChannel channel;
	// The process properties instance
	private IPropertiesContainer properties;

	// The callback instance
	private ICallback callback;

	// The channel trace listener instance
	/* default */ TraceListener traceListener;

	/**
     * Constructor.
     */
    public ScriptLauncher() {
    	super();
    }

	/* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncher#dispose()
     */
    @Override
    public void dispose() {
    	if (channel != null) {
    		// Remove the trace listener
    		if (traceListener != null) { ((AbstractChannel)channel).removeTraceListener(traceListener); traceListener = null; }

    		// Close the channel as all disposal is done
    		Tcf.getChannelManager().closeChannel(channel);

			// Fire the stop event
			ScriptEvent event = new ScriptEvent(ScriptLauncher.this, ScriptEvent.Type.STOP, null);
			EventManager.getInstance().fireEvent(event);

    		// Dissociate the channel
    		channel = null;
    	}
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncher#launch(org.eclipse.tcf.protocol.IPeer, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
     */
    @Override
    public void launch(final IPeer peer, final IPropertiesContainer properties, final ICallback callback) {
    	Assert.isNotNull(peer);
    	Assert.isNotNull(properties);

		// Normalize the callback
		if (callback == null) {
			this.callback = new Callback() {
				/* (non-Javadoc)
				 * @see org.eclipse.tcf.te.runtime.callback.Callback#internalDone(java.lang.Object, org.eclipse.core.runtime.IStatus)
				 */
				@Override
				public void internalDone(Object caller, IStatus status) {
				}
			};
		}
		else {
			this.callback = callback;
		}

		// Remember the process properties
		this.properties = properties;

		// Open a dedicated channel to the given peer
		Map<String, Boolean> flags = new HashMap<String, Boolean>();
		flags.put(IChannelManager.FLAG_FORCE_NEW, Boolean.TRUE);
		Tcf.getChannelManager().openChannel(peer, flags, new IChannelManager.DoneOpenChannel() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager.DoneOpenChannel#doneOpenChannel(java.lang.Throwable, org.eclipse.tcf.protocol.IChannel)
			 */
			@Override
			public void doneOpenChannel(Throwable error, IChannel channel) {
				if (error == null) {
					ScriptLauncher.this.channel = channel;

					// Fire the start event
					ScriptEvent event = new ScriptEvent(ScriptLauncher.this, ScriptEvent.Type.START, null);
					EventManager.getInstance().fireEvent(event);

					// Attach a channel listener so we can dispose ourself if the channel
					// is closed from the remote side.
					channel.addChannelListener(new IChannelListener() {
						/* (non-Javadoc)
						 * @see org.eclipse.tcf.protocol.IChannel.IChannelListener#onChannelOpened()
						 */
						@Override
						public void onChannelOpened() {
						}
						/* (non-Javadoc)
						 * @see org.eclipse.tcf.protocol.IChannel.IChannelListener#onChannelClosed(java.lang.Throwable)
						 */
						@Override
						public void onChannelClosed(Throwable error) {
							if (traceListener != null) { ((AbstractChannel)ScriptLauncher.this.channel).removeTraceListener(traceListener); traceListener = null; }
							if (error != null) {
								IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
															NLS.bind(Messages.ScriptLauncher_error_channelConnectFailed, peer.getID(), error.getLocalizedMessage()),
															error);
								invokeCallback(status, null);
							}
						}
						/* (non-Javadoc)
						 * @see org.eclipse.tcf.protocol.IChannel.IChannelListener#congestionLevel(int)
						 */
						@Override
						public void congestionLevel(int level) {
						}
					});

					// Create the trace listener instance
					traceListener = new TraceListener() {

						@Override
						public void onMessageSent(char type, String token, String service, String name, byte[] data) {
							if (isFiltered(type, name)) return;
							String message = formatMessage(type, token, service, name, data, false);
							ScriptEvent event = new ScriptEvent(ScriptLauncher.this, ScriptEvent.Type.OUTPUT, new ScriptEvent.Message(type, message));
							EventManager.getInstance().fireEvent(event);
						}

						@Override
						public void onMessageReceived(char type, String token, String service, String name, byte[] data) {
							if (isFiltered(type, name)) return;
							String message = formatMessage(type, token, service, name, data, true);
							ScriptEvent event = new ScriptEvent(ScriptLauncher.this, ScriptEvent.Type.OUTPUT, new ScriptEvent.Message(type, message));
							EventManager.getInstance().fireEvent(event);
						}

						@Override
						public void onChannelClosed(Throwable error) {
						}

						/**
						 * Checks if a given message is filtered. Filtered messages are not send to
						 * the script output console.
						 *
						 * @param type The message type.
						 * @param name The message name.
						 *
						 * @return <code>True</code> if the message is filtered, <code>false</code> otherwise.
						 */
						private boolean isFiltered(char type, String name) {
							boolean filtered = false;

							// Filter out the heart beat and framework messages
							if (type == 'F' || (name != null && name.toLowerCase().contains("heartbeat"))) { //$NON-NLS-1$
								filtered = true;
							}

							return filtered;
						}

						/**
						 * Format the trace message.
						 */
						protected String formatMessage(char type, String token, String service, String name, byte[] data, boolean received) {
							// Decode the arguments again for tracing purpose
							String args = JSONUtils.decodeStringFromByteArray(data);

							// Construct the full message
							//
							// The message format is: [<---|--->] <type> <token> <service>#<name> <args>
							StringBuilder message = new StringBuilder();
							message.append(received ? "<---" : "--->"); //$NON-NLS-1$ //$NON-NLS-2$
							message.append(" ").append(Character.valueOf(type)); //$NON-NLS-1$
							if (token != null) message.append(" ").append(token); //$NON-NLS-1$
							if (service != null) message.append(" ").append(service); //$NON-NLS-1$
							if (name != null) message.append(" ").append(name); //$NON-NLS-1$
							if (args != null && args.trim().length() > 0) message.append(" ").append(args.trim()); //$NON-NLS-1$

							return message.toString();
						}
					};

					// Register the trace listener
					((AbstractChannel)channel).addTraceListener(traceListener);

					// Check if the channel is in connected state
					if (channel.getState() != IChannel.STATE_OPEN) {
						IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
													Messages.ScriptLauncher_error_channelNotConnected,
													new IllegalStateException());
						invokeCallback(status, null);
						return;
					}

					// Do some very basic sanity checking on the script properties
					if (properties.getStringProperty(IScriptLauncherProperties.PROP_SCRIPT) == null) {
						IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
													Messages.ScriptLauncher_error_missingScript,
													new IllegalArgumentException(IScriptLauncherProperties.PROP_SCRIPT));
						invokeCallback(status, null);
						return;
					}

					// Execute the launch now
					executeLaunch();
				} else {
					IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
												NLS.bind(Messages.ScriptLauncher_error_channelConnectFailed, peer.getID(), error.getLocalizedMessage()),
												error);
					invokeCallback(status, null);
				}
			}
		});
    }

	/**
	 * Executes the script launch.
	 */
    protected void executeLaunch() {
		// Get the script properties container
		final IPropertiesContainer properties = getProperties();
		if (properties == null) {
			// This is an illegal argument. Properties must be set
			IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										NLS.bind(Messages.ScriptLauncher_error_illegalNullArgument, "properties"), //$NON-NLS-1$
										new IllegalArgumentException());
			invokeCallback(status, null);
			return;
		}

		// Get the script to execute
		String script = properties.getStringProperty(IScriptLauncherProperties.PROP_SCRIPT);
		if (script == null || "".equals(script.trim())) { //$NON-NLS-1$
			IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										Messages.ScriptLauncher_error_missingScript,
										new IllegalArgumentException(IScriptLauncherProperties.PROP_SCRIPT));
			invokeCallback(status, null);
			return;
		}

		// Create the script parser instance
		Parser parser = new Parser(script);
		try {
			// Parse the script
			Token[] tokens = parser.parse();

			// And execute the tokens extracted, one by one sequentially
			if (tokens != null && tokens.length > 0) {
				executeToken(tokens, 0);
			} else {
				invokeCallback(Status.OK_STATUS, null);
				return;
			}
		} catch (IOException e) {
			IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										NLS.bind(Messages.ScriptLauncher_error_parsingScript, e.getLocalizedMessage()),
										e);
			invokeCallback(status, null);
			return;
		}
	}

	/**
	 * Executes the token at the given index.
	 *
	 * @param tokens The tokens. Must not be <code>null</code>.
	 * @param index The index.
	 */
	@SuppressWarnings("unused")
	protected void executeToken(final Token[] tokens, final int index) {
		Assert.isNotNull(tokens);

		if (index < 0 || index >= tokens.length) {
			IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										NLS.bind(Messages.ScriptLauncher_error_illegalIndex, Integer.valueOf(index)),
										new IllegalArgumentException("index")); //$NON-NLS-1$
			invokeCallback(status, null);
			return;
		}

		Token token = tokens[index];

		IService service = channel.getRemoteService(token.getServiceName());
		if (service != null) {
			new Command(channel, service, token.getCommandName(), token.getArguments()) {

				@Override
				public void done(Exception error, Object[] args) {
					if (error == null) {
						// Execute the next token
						int nextIndex = index + 1;
						if (nextIndex == tokens.length) {
							// All tokens executed
							invokeCallback(Status.OK_STATUS, null);
						} else {
							executeToken(tokens, nextIndex);
						}
					} else {
						// Stop the execution
						IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										NLS.bind(Messages.ScriptLauncher_error_parsingScript, error.getLocalizedMessage()),
										error);
						invokeCallback(status, null);
					}
				}
			};
		} else {
			IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
										NLS.bind(Messages.ScriptLauncher_error_serviceNotAvailable, token.getServiceName(), channel.getRemotePeer().getID()),
										null);
			invokeCallback(status, null);
		}
	}

	/**
	 * Invoke the callback with the given parameters. If the given status severity
	 * is {@link IStatus#ERROR}, the process launcher object is disposed automatically.
	 *
	 * @param status The status. Must not be <code>null</code>.
	 * @param result The result object or <code>null</code>.
	 */
	protected void invokeCallback(IStatus status, Object result) {
		// Dispose the process launcher if we report an error
		if (status.getSeverity() == IStatus.ERROR) {
			dispose();
		}

		// Invoke the callback
		ICallback callback = getCallback();
		if (callback != null) {
			callback.setResult(result);
			callback.done(this, status);
		}
	}

	/**
	 * Returns the channel instance.
	 *
	 * @return The channel instance or <code>null</code> if none.
	 */
	public final IChannel getChannel() {
		return channel;
	}

	/**
	 * Returns the process properties container.
	 *
	 * @return The process properties container or <code>null</code> if none.
	 */
	public final IPropertiesContainer getProperties() {
		return properties;
	}

	/**
	 * Returns the callback instance.
	 *
	 * @return The callback instance or <code>null</code> if none.
	 */
	protected final ICallback getCallback() {
		return callback;
	}

}

Back to the top