Fix some findbugs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / model / impl / TargetNodeComponent.java
CommitLineData
eb1bab5b
BH
1/**********************************************************************
2 * Copyright (c) 2012 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 **********************************************************************/
115b4a01 12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl;
eb1bab5b 13
6503ae0f
BH
14import java.util.List;
15
eb1bab5b
BH
16import org.eclipse.core.commands.ExecutionException;
17import org.eclipse.core.runtime.IProgressMonitor;
18import org.eclipse.core.runtime.IStatus;
19import org.eclipse.core.runtime.Status;
20import org.eclipse.core.runtime.jobs.Job;
9315aeee 21import org.eclipse.linuxtools.internal.lttng2.core.control.model.TargetNodeState;
115b4a01 22import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
9315aeee 23import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
115b4a01 24import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
115b4a01 25import org.eclipse.linuxtools.internal.lttng2.ui.views.control.property.TargetNodePropertySource;
9315aeee 26import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell;
115b4a01
BH
27import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy;
28import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.RemoteSystemProxy;
115b4a01 29import org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService;
276c17e7 30import org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.LTTngControlServiceFactory;
8577ed1e 31import org.eclipse.rse.core.RSECorePlugin;
eb1bab5b
BH
32import org.eclipse.rse.core.model.IHost;
33import org.eclipse.rse.core.model.IRSECallback;
8577ed1e 34import org.eclipse.rse.core.model.ISystemRegistry;
eb1bab5b
BH
35import org.eclipse.rse.core.subsystems.CommunicationsEvent;
36import org.eclipse.rse.core.subsystems.ICommunicationsListener;
37import org.eclipse.swt.graphics.Image;
38import org.eclipse.ui.views.properties.IPropertySource;
39
40/**
eb1bab5b
BH
41 * <p>
42 * Implementation of the trace node component.
43 * </p>
dbd4432d
BH
44 *
45 * @author Bernd Hufmann
eb1bab5b
BH
46 */
47public class TargetNodeComponent extends TraceControlComponent implements ICommunicationsListener {
48
49 // ------------------------------------------------------------------------
50 // Constants
51 // ------------------------------------------------------------------------
52 /**
53 * Path to icon file for this component (state connected).
54 */
55 public static final String TARGET_NODE_CONNECTED_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$
56 /**
57 * Path to icon file for this component (state disconnected).
58 */
59 public static final String TARGET_NODE_DISCONNECTED_ICON_FILE = "icons/obj16/target_disconnected.gif"; //$NON-NLS-1$
60
61 // ------------------------------------------------------------------------
62 // Attributes
63 // ------------------------------------------------------------------------
64 /**
65 * The node connection state.
66 */
67 private TargetNodeState fState = TargetNodeState.DISCONNECTED;
68 /**
69 * The image to be displayed in state disconnected.
70 */
71 private Image fDisconnectedImage = null;
72 /**
73 * The connection implementation.
74 */
75 private IHost fHost = null;
76 /**
77 * The remote proxy implementation.
78 */
79 private IRemoteSystemProxy fRemoteProxy = null;
80 /**
81 * The control service for LTTng specific commands.
82 */
83 private ILttngControlService fService = null;
84 /**
85 * The command shell for issuing commands.
86 */
87 private ICommandShell fShell = null;
88
89 // ------------------------------------------------------------------------
90 // Constructors
91 // ------------------------------------------------------------------------
92 /**
93 * Constructor
94 * @param name - the name of the component
95 * @param parent - the parent of the component
96 * @param host - the host connection implementation
97 * @param proxy - the remote proxy implementation
98 */
99 public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host, IRemoteSystemProxy proxy) {
100 super(name, parent);
101 setImage(TARGET_NODE_CONNECTED_ICON_FILE);
31a6a4e4 102 fDisconnectedImage = Activator.getDefault().loadIcon(TARGET_NODE_DISCONNECTED_ICON_FILE);
eb1bab5b
BH
103 fHost = host;
104 fRemoteProxy = proxy;
105 setToolTip(fHost.getHostName());
106 }
107
108 /**
109 * Constructor (using default proxy)
110 * @param name - the name of the component
111 * @param parent - the parent of the component
112 * @param host - the host connection implementation
113 */
114 public TargetNodeComponent(String name, ITraceControlComponent parent, IHost host) {
115 this(name, parent, host, new RemoteSystemProxy(host));
116 }
117
118 // ------------------------------------------------------------------------
119 // Accessors
120 // ------------------------------------------------------------------------
121 /*
122 * (non-Javadoc)
115b4a01 123 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getImage()
eb1bab5b
BH
124 */
125 @Override
126 public Image getImage() {
127 if (fState == TargetNodeState.CONNECTED) {
128 return super.getImage();
129 }
130 return fDisconnectedImage;
131 }
132
133 /*
134 * (non-Javadoc)
115b4a01 135 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getTargetNodeState()
eb1bab5b
BH
136 */
137 @Override
138 public TargetNodeState getTargetNodeState() {
139 return fState;
140 }
141
142 /*
143 * (non-Javadoc)
115b4a01 144 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#setTargetNodeState(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent.TargetNodeState)
eb1bab5b
BH
145 */
146 @Override
147 public void setTargetNodeState(TargetNodeState state) {
148 fState = state;
4775bcbf 149 fireComponentChanged(TargetNodeComponent.this);
eb1bab5b
BH
150 }
151
152 /*
153 * (non-Javadoc)
115b4a01 154 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getControlService()
eb1bab5b
BH
155 */
156 @Override
157 public ILttngControlService getControlService() {
158 return fService;
159 }
160
161 /*
162 * (non-Javadoc)
115b4a01 163 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#setControlService(org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService)
eb1bab5b
BH
164 */
165 @Override
166 public void setControlService(ILttngControlService service) {
167 fService = (ILttngControlService)service;
168 }
169
170 /*
171 * (non-Javadoc)
115b4a01 172 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent#getAdapter(java.lang.Class)
eb1bab5b
BH
173 */
174 @SuppressWarnings("rawtypes")
175 @Override
176 public Object getAdapter(Class adapter) {
177 if (adapter == IPropertySource.class) {
178 return new TargetNodePropertySource(this);
179 }
180 return null;
181 }
182
bbb3538a
BH
183 /**
184 * @return remote host name
185 */
eb1bab5b
BH
186 public String getHostName() {
187 return fHost.getHostName();
188 }
189
bbb3538a
BH
190 /**
191 * @return remote system proxy implementation
192 */
193 public IRemoteSystemProxy getRemoteSystemProxy() {
194 return fRemoteProxy;
195 }
196
6503ae0f
BH
197 /**
198 * @return all available sessions.
199 */
200 public TraceSessionComponent[] getSessions() {
201 List<ITraceControlComponent> compenents = getChildren(TraceSessionGroup.class);
202 if (compenents.size() > 0) {
203 TraceSessionGroup group = (TraceSessionGroup)compenents.get(0);
204 List<ITraceControlComponent> sessions = group.getChildren(TraceSessionComponent.class);
205 return (TraceSessionComponent[])sessions.toArray(new TraceSessionComponent[sessions.size()]);
206 }
207 return new TraceSessionComponent[0];
208 }
209
eb1bab5b
BH
210 // ------------------------------------------------------------------------
211 // Operations
212 // ------------------------------------------------------------------------
213
214 /*
215 * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#communicationsStateChange(org.eclipse.rse.core.subsystems.CommunicationsEvent)
216 */
217 @Override
218 public void communicationsStateChange(CommunicationsEvent e) {
219 if (e.getState() == CommunicationsEvent.AFTER_DISCONNECT ||
220 e.getState() == CommunicationsEvent.CONNECTION_ERROR) {
221 handleDisconnected();
222 } if ((e.getState() == CommunicationsEvent.AFTER_CONNECT) && (fState != TargetNodeState.CONNECTING)) {
223 handleConnected();
224 }
225 }
226
227 /* (non-Javadoc)
228 * @see org.eclipse.rse.core.subsystems.ICommunicationsListener#isPassiveCommunicationsListener()
229 */
230 @Override
231 public boolean isPassiveCommunicationsListener() {
232 return true;
233 }
234
235 /*
236 * (non-Javadoc)
115b4a01 237 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceControlComponent#dispose()
eb1bab5b
BH
238 */
239 @Override
240 public void dispose() {
241 fRemoteProxy.removeCommunicationListener(this);
242 }
243
244 /**
245 * Method to connect this node component to the remote target node.
246 */
247 public void connect() {
248 if (fState == TargetNodeState.DISCONNECTED) {
249 try {
250 setTargetNodeState(TargetNodeState.CONNECTING);
251 fRemoteProxy.connect(new IRSECallback() {
252 @Override
253 public void done(IStatus status, Object result) {
254 // Note: result might be null!
255 if(status.isOK()) {
256 handleConnected();
06b9339e
BH
257 } else {
258 handleDisconnected();
eb1bab5b
BH
259 }
260 }
261 });
262 } catch (Exception e) {
263 setTargetNodeState(TargetNodeState.DISCONNECTED);
115b4a01 264 Activator.getDefault().getLog().log(
f455db37 265 new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ConnectionFailure + " (" + getName() + "). \n", e)); //$NON-NLS-1$ //$NON-NLS-2$
eb1bab5b
BH
266 }
267 }
268 }
269
270 /**
271 * Method to disconnect this node component to the remote target node.
272 */
273 public void disconnect() {
274 if (fState == TargetNodeState.CONNECTED) {
275 try {
276 setTargetNodeState(TargetNodeState.DISCONNECTING);
277 fRemoteProxy.disconnect();
278 } catch (Exception e) {
115b4a01 279 Activator.getDefault().getLog().log(
f455db37 280 new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_DisconnectionFailure + " (" + getName() + "). \n", e)); //$NON-NLS-1$ //$NON-NLS-2$
eb1bab5b
BH
281 } finally {
282 handleDisconnected();
283 }
284 }
285 }
286
287 /**
288 * Retrieves the trace configuration from the target node and populates the information
289 * in the tree model. The execution is done in a own job.
290 *
291 * @throws ExecutionException
292 */
d132bcc7 293 public void getConfigurationFromNode() {
eb1bab5b
BH
294 Job job = new Job(Messages.TraceControl_RetrieveNodeConfigurationJob) {
295 @Override
296 protected IStatus run(IProgressMonitor monitor) {
297
298 try {
299 // Get provider information from node
300 TraceProviderGroup providerGroup = new TraceProviderGroup(Messages.TraceControl_ProviderDisplayName, TargetNodeComponent.this);
301 addChild(providerGroup);
302 providerGroup.getProviderFromNode(monitor);
303
304 // Get session information from node
305 TraceSessionGroup sessionGroup = new TraceSessionGroup(Messages.TraceControl_AllSessionsDisplayName, TargetNodeComponent.this);
306 addChild(sessionGroup);
307 sessionGroup.getSessionsFromNode(monitor);
308 } catch (ExecutionException e) {
309 removeAllChildren();
f455db37 310 return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ListSessionFailure, e);
eb1bab5b
BH
311 }
312
313 return Status.OK_STATUS;
314 }
315 };
316 job.setUser(true);
317 job.schedule();
d132bcc7 318 }
eb1bab5b 319
8577ed1e
BH
320 /**
321 * Refresh the node configuration
322 */
d132bcc7
BH
323 public void refresh() {
324 removeAllChildren();
325 getConfigurationFromNode();
eb1bab5b 326 }
8577ed1e
BH
327
328 /**
329 * Deregisters host from registry.
330 */
331 public void deregister() {
332 ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
333 registry.deleteHost(fHost);
334 }
eb1bab5b
BH
335
336 // ------------------------------------------------------------------------
337 // Helper function
338 // ------------------------------------------------------------------------
339 /**
340 * @return returns the control service for LTTng specific commands.
341 * @throws ExecutionException
342 */
343 private ILttngControlService createControlService() throws ExecutionException {
344 if (fShell == null) {
345 fShell = fRemoteProxy.createCommandShell();
346 fRemoteProxy.addCommunicationListener(this);
347 }
276c17e7 348 fService = LTTngControlServiceFactory.getInstance().getLttngControlService(fShell);
eb1bab5b
BH
349 return fService;
350 }
351
352 /**
353 * Handles the connected event.
354 */
355 private void handleConnected() {
356 setTargetNodeState(TargetNodeState.CONNECTED);
357 try {
358 createControlService();
359 getConfigurationFromNode();
360 } catch (ExecutionException e) {
115b4a01 361 Activator.getDefault().getLog().log(
f455db37 362 new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ListSessionFailure + " (" + getName() + "). \n", e)); //$NON-NLS-1$ //$NON-NLS-2$
eb1bab5b
BH
363 }
364 }
365
366 /**
367 * Handles the disconnected event.
368 */
369 private void handleDisconnected() {
370 removeAllChildren();
371 setTargetNodeState(TargetNodeState.DISCONNECTED);
372 fShell = null;
373 fService = null;
374 }
375}
This page took 0.059717 seconds and 5 git commands to generate.