Fix new errors due to automatic annotation of Class<T> types
[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.ui.Activator;
39 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
40 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
41 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.property.TargetNodePropertySource;
42 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.ILttngControlService;
43 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.LTTngControlServiceFactory;
44 import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy;
45 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.views.properties.IPropertySource;
48
49 /**
50 * <p>
51 * Implementation of the trace node component.
52 * </p>
53 *
54 * @author Bernd Hufmann
55 */
56 public class TargetNodeComponent extends TraceControlComponent implements IRemoteConnectionChangeListener {
57
58 // ------------------------------------------------------------------------
59 // Constants
60 // ------------------------------------------------------------------------
61
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
71 private static final ILttngControlService NULL_CONTROL_SERVICE = new NullControlService();
72
73 // ------------------------------------------------------------------------
74 // Attributes
75 // ------------------------------------------------------------------------
76
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;
85 /**
86 * The remote proxy implementation.
87 */
88 private @NonNull RemoteSystemProxy fRemoteProxy;
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 // ------------------------------------------------------------------------
101
102 /**
103 * Constructor
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
111 */
112 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull RemoteSystemProxy proxy) {
113 super(name, parent);
114 setImage(TARGET_NODE_CONNECTED_ICON_FILE);
115 fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
116 fRemoteProxy = proxy;
117 fRemoteProxy.getRemoteConnection().addConnectionChangeListener(this);
118 setToolTip(fRemoteProxy.getRemoteConnection().getName());
119 }
120
121 /**
122 * Constructor (using default proxy)
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
130 */
131 public TargetNodeComponent(String name, ITraceControlComponent parent, @NonNull IRemoteConnection host) {
132 this(name, parent, new RemoteSystemProxy(host));
133 }
134
135 @Override
136 public void dispose() {
137 fRemoteProxy.getRemoteConnection().removeConnectionChangeListener(this);
138 fRemoteProxy.dispose();
139 disposeControlService();
140 }
141
142 private void disposeControlService() {
143 fService = null;
144 final ICommandShell shell = fShell;
145 if (shell != null) {
146 shell.dispose();
147 fShell = null;
148 }
149 }
150
151 // ------------------------------------------------------------------------
152 // Accessors
153 // ------------------------------------------------------------------------
154
155 @Override
156 public Image getImage() {
157 if (fState == TargetNodeState.CONNECTED) {
158 return super.getImage();
159 }
160 return fDisconnectedImage;
161 }
162
163 @Override
164 public TargetNodeState getTargetNodeState() {
165 return fState;
166 }
167
168 @Override
169 public void setTargetNodeState(TargetNodeState state) {
170 fState = state;
171 fireComponentChanged(TargetNodeComponent.this);
172 }
173
174 @Override
175 public ILttngControlService getControlService() {
176 return fService == null ? NULL_CONTROL_SERVICE : fService;
177 }
178
179 @Override
180 public void setControlService(ILttngControlService service) {
181 fService = service;
182 }
183
184 @Override
185 public <T> @Nullable T getAdapter(Class<T> adapter) {
186 if (adapter == IPropertySource.class) {
187 return adapter.cast(new TargetNodePropertySource(this));
188 }
189 return null;
190 }
191
192 /**
193 * @return remote system proxy implementation
194 */
195 public @NonNull RemoteSystemProxy getRemoteSystemProxy() {
196 return fRemoteProxy;
197 }
198
199 /**
200 * @return all available sessions.
201 */
202 public TraceSessionComponent[] getSessions() {
203 List<ITraceControlComponent> compenents = getChildren(TraceSessionGroup.class);
204 if (compenents.size() > 0) {
205 TraceSessionGroup group = (TraceSessionGroup)compenents.get(0);
206 List<ITraceControlComponent> sessions = group.getChildren(TraceSessionComponent.class);
207 return sessions.toArray(new TraceSessionComponent[sessions.size()]);
208 }
209 return new TraceSessionComponent[0];
210 }
211
212 /**
213 * @return node version
214 */
215 public String getNodeVersion() {
216 // Control service is null during connection to node
217 if (getControlService() != NULL_CONTROL_SERVICE) {
218 return getControlService().getVersionString();
219 }
220 return ""; //$NON-NLS-1$
221 }
222
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
231 /**
232 * Returns if node supports networks streaming or not
233 * @return <code>true</code> if node supports filtering else <code>false</code>
234 *
235 */
236 public boolean isNetworkStreamingSupported() {
237 return getControlService().isVersionSupported("2.1.0"); //$NON-NLS-1$
238 }
239
240 /**
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>
243 */
244 public boolean isBufferTypeConfigSupported() {
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 }
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 }
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 }
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
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
298 // ------------------------------------------------------------------------
299 // Operations
300 // ------------------------------------------------------------------------
301
302 @Override
303 public void connectionChanged(RemoteConnectionChangeEvent e) {
304 if (fState == TargetNodeState.CONNECTING) {
305 return;
306 }
307
308 switch (e.getType()) {
309 case RemoteConnectionChangeEvent.CONNECTION_CLOSED:
310 case RemoteConnectionChangeEvent.CONNECTION_ABORTED:
311 handleDisconnected();
312 break;
313 case RemoteConnectionChangeEvent.CONNECTION_OPENED:
314 handleConnected();
315 break;
316 default:
317 break;
318 }
319 }
320
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 {
332 fRemoteProxy.connect(checkNotNull(monitor));
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 */
364 public void disconnect() {
365 if (fState == TargetNodeState.CONNECTED) {
366 try {
367 setTargetNodeState(TargetNodeState.DISCONNECTING);
368 fRemoteProxy.disconnect();
369 } catch (Exception e) {
370 Activator.getDefault().logError(Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e); //$NON-NLS-1$ //$NON-NLS-2$
371 } finally {
372 handleDisconnected();
373 }
374 }
375 }
376
377 /**
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.
380 */
381 public void getConfigurationFromNode() {
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);
390
391 // Get session information from node
392 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
393 addChild(sessionGroup);
394
395 providerGroup.getProviderFromNode(monitor);
396 sessionGroup.getSessionsFromNode(monitor);
397 } catch (ExecutionException e) {
398 removeAllChildren();
399 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RetrieveNodeConfigurationFailure, e);
400 }
401
402 return Status.OK_STATUS;
403 }
404 };
405 job.setUser(true);
406 job.schedule();
407 }
408
409 /**
410 * Refresh the node configuration
411 */
412 public void refresh() {
413 removeAllChildren();
414 getConfigurationFromNode();
415 }
416
417 // ------------------------------------------------------------------------
418 // Helper function
419 // ------------------------------------------------------------------------
420
421 /**
422 * @return returns the control service for LTTng specific commands.
423 * @throws ExecutionException
424 */
425 private ILttngControlService createControlService() throws ExecutionException {
426 if (fService == null) {
427 try {
428 ICommandShell shell = fRemoteProxy.createCommandShell();
429 fShell = shell;
430 fService = LTTngControlServiceFactory.getLttngControlService(shell);
431 } catch (ExecutionException e) {
432 disposeControlService();
433 throw e;
434 }
435 }
436 return fService;
437 }
438
439 /**
440 * Handles the connected event.
441 */
442 private void handleConnected() {
443 try {
444 createControlService();
445 getConfigurationFromNode();
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);
449 } catch (final ExecutionException e) {
450 // Disconnect only if no control service, otherwise stay connected.
451 if (getControlService() == NULL_CONTROL_SERVICE) {
452 fState = TargetNodeState.CONNECTED;
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$
468 }
469 }
470
471 /**
472 * Handles the disconnected event.
473 */
474 private void handleDisconnected() {
475 disposeControlService();
476 setTargetNodeState(TargetNodeState.DISCONNECTED);
477 removeAllChildren();
478 }
479
480 @Override
481 public void addChild(ITraceControlComponent component) {
482 if (getTargetNodeState() == TargetNodeState.DISCONNECTED) {
483 return;
484 }
485 super.addChild(component);
486 }
487 }
This page took 0.042759 seconds and 6 git commands to generate.