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