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