tmf-remote: Bug 460842: clean-up ICommandShell and ICommandResult APIs
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / model / impl / TargetNodeComponent.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 * Bernd Hufmann - Updated for support of LTTng Tools 2.1
12 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
13 * Bernd Hufmann - Update to org.eclipse.remote API 2.0
14 **********************************************************************/
15 package org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl;
16
17 import static java.text.MessageFormat.format;
18 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
20 import java.util.List;
21
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
27 import org.eclipse.core.runtime.jobs.Job;
28 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jface.dialogs.ErrorDialog;
31 import org.eclipse.remote.core.IRemoteConnection;
32 import org.eclipse.remote.core.IRemoteConnectionChangeListener;
33 import org.eclipse.remote.core.RemoteConnectionChangeEvent;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
37 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
38 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
39 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
40 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.property.TargetNodePropertySource;
41 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.ILttngControlService;
42 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.LTTngControlServiceFactory;
43 import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
44 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.views.properties.IPropertySource;
47
48 /**
49 * <p>
50 * Implementation of the trace node component.
51 * </p>
52 *
53 * @author Bernd Hufmann
54 */
55 public class TargetNodeComponent extends TraceControlComponent implements IRemoteConnectionChangeListener {
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
61 /**
62 * Path to icon file for this component (state connected).
63 */
64 public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$
65 /**
66 * Path to icon file for this component (state disconnected).
67 */
68 public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
69
70 private static final ILttngControlService NULL_CONTROL_SERVICE = new NullControlService();
71
72 // ------------------------------------------------------------------------
73 // Attributes
74 // ------------------------------------------------------------------------
75
76 /**
77 * The node connection state.
78 */
79 private TargetNodeState fState = TargetNodeState.DISCONNECTED;
80 /**
81 * The image to be displayed in state disconnected.
82 */
83 private Image fDisconnectedImage = null;
84 /**
85 * The remote proxy implementation.
86 */
87 private @NonNull RemoteSystemProxy fRemoteProxy;
88 /**
89 * The control service for LTTng specific commands.
90 */
91 private ILttngControlService fService = null;
92 /**
93 * The command shell for issuing commands.
94 */
95 private ICommandShell fShell = null;
96
97 // ------------------------------------------------------------------------
98 // Constructors
99 // ------------------------------------------------------------------------
100
101 /**
102 * Constructor
103 *
104 * @param name
105 * the name of the component
106 * @param parent
107 * the parent of the component
108 * @param proxy
109 * the remote proxy implementation
110 */
111 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull RemoteSystemProxy proxy) {
112 super(name, parent);
113 setImage(TARGET_NODE_CONNECTED_ICON_FILE);
114 fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
115 fRemoteProxy = proxy;
116 fRemoteProxy.addConnectionChangeListener(this);
117 setToolTip(fRemoteProxy.getRemoteConnection().getName());
118 }
119
120 /**
121 * Constructor (using default proxy)
122 *
123 * @param name
124 * the name of the component
125 * @param parent
126 * the parent of the component
127 * @param host
128 * the host connection implementation
129 */
130 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull IRemoteConnection host) {
131 this(name, parent, new RemoteSystemProxy(host));
132 }
133
134 @Override
135 public void dispose() {
136 fRemoteProxy.removeConnectionChangeListener(this);
137 fRemoteProxy.dispose();
138 disposeControlService();
139 }
140
141 private void disposeControlService() {
142 fService = null;
143 final ICommandShell shell = fShell;
144 if (shell != null) {
145 shell.dispose();
146 fShell = null;
147 }
148 }
149
150 // ------------------------------------------------------------------------
151 // Accessors
152 // ------------------------------------------------------------------------
153
154 @Override
155 public Image getImage() {
156 if (fState == TargetNodeState.CONNECTED) {
157 return super.getImage();
158 }
159 return fDisconnectedImage;
160 }
161
162 @Override
163 public TargetNodeState getTargetNodeState() {
164 return fState;
165 }
166
167 @Override
168 public void setTargetNodeState(TargetNodeState state) {
169 fState = state;
170 fireComponentChanged(TargetNodeComponent.this);
171 }
172
173 @Override
174 public ILttngControlService getControlService() {
175 return fService == null ? NULL_CONTROL_SERVICE : fService;
176 }
177
178 @Override
179 public void setControlService(ILttngControlService service) {
180 fService = service;
181 }
182
183 @Override
184 public Object getAdapter(Class adapter) {
185 if (adapter == IPropertySource.class) {
186 return new TargetNodePropertySource(this);
187 }
188 return null;
189 }
190
191 /**
192 * @return remote system proxy implementation
193 */
194 public @NonNull RemoteSystemProxy getRemoteSystemProxy() {
195 return fRemoteProxy;
196 }
197
198 /**
199 * @return all available sessions.
200 */
201 public TraceSessionComponent[] getSessions() {
202 List<ITraceControlComponent> compenents = getChildren(TraceSessionGroup.class);
203 if (compenents.size() > 0) {
204 TraceSessionGroup group = (TraceSessionGroup)compenents.get(0);
205 List<ITraceControlComponent> sessions = group.getChildren(TraceSessionComponent.class);
206 return sessions.toArray(new TraceSessionComponent[sessions.size()]);
207 }
208 return new TraceSessionComponent[0];
209 }
210
211 /**
212 * @return node version
213 */
214 public String getNodeVersion() {
215 // Control service is null during connection to node
216 if (getControlService() != NULL_CONTROL_SERVICE) {
217 return getControlService().getVersionString();
218 }
219 return ""; //$NON-NLS-1$
220 }
221
222 /**
223 * Returns if node supports filtering of events
224 * @return <code>true</code> if node supports filtering else <code>false</code>
225 */
226 public boolean isEventFilteringSupported() {
227 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
228 }
229
230 /**
231 * Returns if node supports networks streaming or not
232 * @return <code>true</code> if node supports filtering else <code>false</code>
233 *
234 */
235 public boolean isNetworkStreamingSupported() {
236 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
237 }
238
239 /**
240 * Returns if node supports configuring buffer type or not
241 * @return <code>true</code> if node supports buffer type configuration else <code>false</code>
242 */
243 public boolean isBufferTypeConfigSupported() {
244 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
245 }
246
247 /**
248 * Returns if node supports trace file rotation or not
249 * @return <code>true</code> if node supports trace file rotation else <code>false</code>
250 */
251 public boolean isTraceFileRotationSupported() {
252 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
253 }
254
255 /**
256 * Returns if node supports periodical flush for metadata or not
257 * @return <code>true</code> if node supports periodical flush for metadata else <code>false</code>
258 */
259 public boolean isPeriodicalMetadataFlushSupported() {
260 return getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
261 }
262 /**
263 * Returns if node supports snapshots or not
264 * @return <code>true</code> if it supports snapshots else <code>false</code>
265 *
266 */
267 public boolean isSnapshotSupported() {
268 return getControlService().isVersionSupported("2.3.0"); //$NON-NLS-1$
269 }
270 /**
271 * Returns if node supports live or not
272 * @return <code>true</code> if it supports live else <code>false</code>
273 *
274 */
275 public boolean isLiveSupported() {
276 return getControlService().isVersionSupported("2.4.0"); //$NON-NLS-1$;
277 }
278 /**
279 * Returns if node supports adding contexts on event
280 * @return <code>true</code> if it supports adding contexts on events else <code>false</code>
281 *
282 */
283 public boolean isContextOnEventSupported() {
284 return !getControlService().isVersionSupported("2.2.0"); //$NON-NLS-1$
285 }
286
287 // ------------------------------------------------------------------------
288 // Operations
289 // ------------------------------------------------------------------------
290
291 @Override
292 public void connectionChanged(RemoteConnectionChangeEvent e) {
293 if (fState == TargetNodeState.CONNECTING) {
294 return;
295 }
296
297 switch (e.getType()) {
298 case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
299 case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
300 handleDisconnected();
301 break;
302 case RemoteConnectionChangeEvent.CONNECTION_OPENED:
303 handleConnected();
304 break;
305 default:
306 break;
307 }
308 }
309
310 /**
311 * Method to connect this node component to the remote target node.
312 */
313 public void connect() {
314 if (fState == TargetNodeState.DISCONNECTED) {
315 try {
316 setTargetNodeState(TargetNodeState.CONNECTING);
317 Job job = new Job(format(Messages.TraceControl_OpenConnectionTo, getName())) {
318 @Override
319 protected IStatus run(IProgressMonitor monitor) {
320 try {
321 fRemoteProxy.connect(checkNotNull(monitor));
322 return Status.OK_STATUS;
323 } catch (Exception e) {
324 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure, e);
325 }
326 }
327 };
328 job.addJobChangeListener(new JobChangeAdapter() {
329 @Override
330 public void done(IJobChangeEvent event) {
331 IStatus status = event.getResult();
332 if (status.isOK()) {
333 handleConnected();
334 } else {
335 handleDisconnected();
336 if (status.getSeverity() != IStatus.CANCEL) {
337 Activator.getDefault().getLog().log(status);
338 }
339 }
340 }
341 });
342 job.schedule();
343 } catch (Exception e) {
344 setTargetNodeState(TargetNodeState.DISCONNECTED);
345 Activator.getDefault().logError(Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
346 }
347 }
348 }
349
350 /**
351 * Method to disconnect this node component to the remote target node.
352 */
353 public void disconnect() {
354 if (fState == TargetNodeState.CONNECTED) {
355 try {
356 setTargetNodeState(TargetNodeState.DISCONNECTING);
357 fRemoteProxy.disconnect();
358 } catch (Exception e) {
359 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
360 } finally {
361 handleDisconnected();
362 }
363 }
364 }
365
366 /**
367 * Retrieves the trace configuration from the target node and populates the
368 * information in the tree model. The execution is done in a own job.
369 */
370 public void getConfigurationFromNode() {
371 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
372 @Override
373 protected IStatus run(IProgressMonitor monitor) {
374
375 try {
376 // Get provider information from node
377 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
378 addChild(providerGroup);
379
380 // Get session information from node
381 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
382 addChild(sessionGroup);
383
384 providerGroup.getProviderFromNode(monitor);
385 sessionGroup.getSessionsFromNode(monitor);
386 } catch (ExecutionException e) {
387 removeAllChildren();
388 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
389 }
390
391 return Status.OK_STATUS;
392 }
393 };
394 job.setUser(true);
395 job.schedule();
396 }
397
398 /**
399 * Refresh the node configuration
400 */
401 public void refresh() {
402 removeAllChildren();
403 getConfigurationFromNode();
404 }
405
406 // ------------------------------------------------------------------------
407 // Helper function
408 // ------------------------------------------------------------------------
409
410 /**
411 * @return returns the control service for LTTng specific commands.
412 * @throws ExecutionException
413 */
414 private ILttngControlService createControlService() throws ExecutionException {
415 if (fService == null) {
416 try {
417 ICommandShell shell = fRemoteProxy.createCommandShell();
418 fShell = shell;
419 fService = LTTngControlServiceFactory.getLttngControlService(shell);
420 } catch (ExecutionException e) {
421 disposeControlService();
422 throw e;
423 }
424 }
425 return fService;
426 }
427
428 /**
429 * Handles the connected event.
430 */
431 private void handleConnected() {
432 try {
433 createControlService();
434 getConfigurationFromNode();
435 // Set connected only after the control service has been created and the jobs for creating the
436 // sub-nodes are scheduled.
437 setTargetNodeState(TargetNodeState.CONNECTED);
438 } catch (final ExecutionException e) {
439 // Disconnect only if no control service, otherwise stay connected.
440 if (getControlService() == NULL_CONTROL_SERVICE) {
441 fState = TargetNodeState.CONNECTED;
442 disconnect();
443 }
444
445 // Notify user
446 Display.getDefault().asyncExec(new Runnable() {
447 @Override
448 public void run() {
449 ErrorDialog er = new ErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
450 Messages.TraceControl_ErrorTitle, Messages.TraceControl_RetrieveNodeConfigurationFailure,
451 new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e),
452 IStatus.ERROR);
453 er.open();
454 }
455 });
456 Activator.getDefault().logError(Messages.TraceControl_RetrieveNodeConfigurationFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
457 }
458 }
459
460 /**
461 * Handles the disconnected event.
462 */
463 private void handleDisconnected() {
464 disposeControlService();
465 setTargetNodeState(TargetNodeState.DISCONNECTED);
466 removeAllChildren();
467 }
468
469 @Override
470 public void addChild(ITraceControlComponent component) {
471 if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
472 return;
473 }
474 super.addChild(component);
475 }
476 }
This page took 0.047431 seconds and 6 git commands to generate.