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