Contribution for Bug353020
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / actions / StartTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
12 *
13 *******************************************************************************/
14 package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions;
15
16 import java.io.File;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.linuxtools.lttng.tracecontrol.model.TraceResource;
27 import org.eclipse.linuxtools.lttng.tracecontrol.model.TraceResource.TraceState;
28 import org.eclipse.linuxtools.lttng.tracecontrol.model.config.TraceConfig;
29 import org.eclipse.linuxtools.lttng.tracecontrol.service.ILttControllerService;
30 import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
31 import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
32 import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
33 import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.SelectTracePathDialog;
34 import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
35 import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
36 import org.eclipse.rse.core.model.ISystemRegistry;
37 import org.eclipse.rse.core.model.SystemStartHere;
38 import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
39 import org.eclipse.rse.ui.SystemBasePlugin;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.tm.tcf.protocol.IToken;
42 import org.eclipse.tm.tcf.util.TCFTask;
43 import org.eclipse.ui.IObjectActionDelegate;
44 import org.eclipse.ui.IViewActionDelegate;
45 import org.eclipse.ui.IViewPart;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.IWorkbenchWindow;
48 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
49
50 /**
51 * <b><u>StartTrace</u></b>
52 * <p>
53 * Action implementation to start and resume a trace. Starting a trace the first time will allocate all
54 * necessary resources and configure all necessary parameters on the remote system.
55 * </p>
56 */
57 public class StartTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
58
59 // ------------------------------------------------------------------------
60 // Attributes
61 // ------------------------------------------------------------------------
62
63 private List<TraceResource> fSelectedTraces;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 public StartTrace() {
70 fSelectedTraces = new ArrayList<TraceResource>();
71 }
72
73 // ------------------------------------------------------------------------
74 // Operations
75 // ------------------------------------------------------------------------
76
77 /*
78 * (non-Javadoc)
79 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
80 */
81 @Override
82 public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
83 }
84
85 /*
86 * (non-Javadoc)
87 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
88 */
89 @Override
90 public void run(IAction arg0) {
91 for (int i = 0; i < fSelectedTraces.size(); i++) {
92
93 TraceResource trace = (TraceResource) fSelectedTraces.get(i);
94 TraceSubSystem subSystem = (TraceSubSystem)trace.getSubSystem();
95
96 TraceConfig traceConfig = trace.getTraceConfig();
97 if (traceConfig != null) {
98 try {
99 ILttControllerService service = subSystem.getControllerService();
100 if (trace.getTraceState() == TraceState.CONFIGURED) {
101 setTraceTransport(service, trace, traceConfig);
102 allocTrace(service, trace, traceConfig);
103 setupLocation(service, trace, traceConfig);
104 }
105 // for network traces and if trace path is not available, open a dialog box for the user to specify the trace path
106 else if (traceConfig.isNetworkTrace() && (TraceConfig.InvalidTracePath.equals(traceConfig.getTracePath()))) {
107
108 SelectTracePathDialog selectDialog = new SelectTracePathDialog(SystemBasePlugin.getActiveWorkbenchShell());
109
110 if (selectDialog.open() == Window.OK) {
111 traceConfig.setTracePath(selectDialog.getTracePath());
112 }
113 else {
114 // we don't have place to store the trace files ... go to the next trace
115 continue;
116 }
117 }
118
119 startTrace(service, trace, traceConfig);
120
121 trace.setTraceState(TraceState.STARTED);
122
123 // Refresh display
124 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
125 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CHANGED, trace, trace.getParent(), subSystem, null);
126
127 } catch (Exception e) {
128 SystemMessageException sysExp;
129 if (e instanceof SystemMessageException) {
130 sysExp = (SystemMessageException)e;
131 } else {
132 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
133 }
134 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorStart + " (" + //$NON-NLS-1$
135 Messages.Lttng_Resource_Trace + ": " + trace.getName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
136 }
137 }
138 }
139 }
140
141 /*
142 * (non-Javadoc)
143 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
144 */
145 @SuppressWarnings("unchecked")
146 @Override
147 public void selectionChanged(IAction action, ISelection selection) {
148 if (selection instanceof IStructuredSelection) {
149 fSelectedTraces.clear();
150 // store the selected targets to be used when running
151 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
152 while (theSet.hasNext()) {
153 Object obj = theSet.next();
154 if (obj instanceof TraceResource) {
155 fSelectedTraces.add((TraceResource)obj);
156 }
157 }
158 }
159 }
160
161 /**
162 * Returns the active workbench shell of this plug-in.
163 *
164 * @return active workbench shell.
165 */
166 protected Shell getShell() {
167 return SystemBasePlugin.getActiveWorkbenchShell();
168 }
169
170 /*
171 * (non-Javadoc)
172 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
173 */
174 @Override
175 public void init(IWorkbenchWindow arg0) {
176 }
177
178 /*
179 * (non-Javadoc)
180 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
181 */
182 @Override
183 public void dispose() {
184 }
185
186 /*
187 * (non-Javadoc)
188 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
189 */
190 @Override
191 public void init(IViewPart view) {
192 }
193
194 /*
195 * Setup trace transport on the remote system
196 */
197 private void setTraceTransport(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
198 // Create future task
199 new TCFTask<Boolean>() {
200 @Override
201 public void run() {
202
203 // Setup trace transport using Lttng controller service proxy
204 service.setTraceTransport(trace.getParent().getParent().getName(),
205 trace.getParent().getName(),
206 oldConfig.getTraceName(),
207 oldConfig.getTraceTransport(),
208 new ILttControllerService.DoneSetTraceTransport() {
209
210 @Override
211 public void doneSetTraceTransport(IToken token, Exception error, Object str) {
212 if (error != null) {
213 // Notify with error
214 error(error);
215 return;
216 }
217
218 // Notify about success
219 done(Boolean.valueOf(true));
220 }
221 });
222 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
223 }
224
225 /*
226 * Allocate trace resources on the remote system.
227 */
228 private void allocTrace(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
229 new TCFTask<Boolean>() {
230 @Override
231 public void run() {
232
233 // Setup trace transport using Lttng controller service proxy
234 service.allocTrace(trace.getParent().getParent().getName(),
235 trace.getParent().getName(),
236 trace.getName(),
237 new ILttControllerService.DoneAllocTrace() {
238
239 @Override
240 public void doneAllocTrace(IToken token, Exception error, Object str) {
241 if (error != null) {
242 // Notify with error
243 error(error);
244 return;
245 }
246
247 // Notify about success
248 done(Boolean.valueOf(true));
249 }
250 });
251 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
252 }
253
254 /*
255 * Setup the trace location. Only normal channels are written while trace is started.
256 */
257 private void setupLocation(final ILttControllerService service, final TraceResource trace, final TraceConfig traceConfig) throws Exception {
258 if (traceConfig.isNetworkTrace()) {
259
260 File newDir = new File(traceConfig.getTracePath());
261 if (!newDir.exists()) {
262 boolean created = newDir.mkdirs();
263 if (!created) {
264 throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
265 }
266 }
267
268 if (traceConfig.getProject() != null) {
269 ImportToProject.linkTrace(getShell(), trace, traceConfig.getProject(), traceConfig.getTraceName());
270 }
271
272 // Create future task
273 new TCFTask<Boolean>() {
274 @Override
275 public void run() {
276
277 // Setup trace transport using Lttng controller service proxy
278 service.writeTraceNetwork(trace.getParent().getParent().getName(),
279 trace.getParent().getName(),
280 traceConfig.getTraceName(),
281 traceConfig.getNumChannel(),
282 traceConfig.getIsAppend(),
283 false,
284 true, // write only normal channels
285 new ILttControllerService.DoneWriteTraceNetwork() {
286
287 @Override
288 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
289 if (error != null) {
290 // Notify with error
291 error(error);
292 return;
293 }
294
295 // Notify about success
296 done(Boolean.valueOf(true));
297 }
298 });
299 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
300
301 } else {
302
303 // Create future task
304 new TCFTask<Boolean>() {
305 @Override
306 public void run() {
307
308 // Setup trace transport using Lttng controller service proxy
309 service.writeTraceLocal(trace.getParent().getParent().getName(),
310 trace.getParent().getName(),
311 traceConfig.getTraceName(),
312 traceConfig.getTracePath(),
313 traceConfig.getNumChannel(),
314 traceConfig.getIsAppend(),
315 false,
316 true, // write only normal channels
317 new ILttControllerService.DoneWriteTraceLocal() {
318
319 @Override
320 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
321 if (error != null) {
322 // Notify with error
323 error(error);
324 return;
325 }
326
327 // Notify about success
328 done(Boolean.valueOf(true));
329 }
330 });
331 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
332 }
333 }
334
335 /*
336 * Starts the trace on the remote system.
337 */
338 private void startTrace(final ILttControllerService service, final TraceResource trace, final TraceConfig oldConfig) throws Exception {
339 new TCFTask<Boolean>() {
340 @Override
341 public void run() {
342
343 // Setup trace transport using Lttng controller service proxy
344 service.startTrace(trace.getParent().getParent().getName(), trace.getParent().getName(), oldConfig.getTraceName(), new ILttControllerService.DoneStartTrace() {
345
346 @Override
347 public void doneStartTrace(IToken token, Exception error, Object str) {
348 if (error != null) {
349
350 // Notify with error
351 error(error);
352 return;
353 }
354
355 // Notify about success
356 done(Boolean.valueOf(true));
357 }
358 });
359 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS );
360 }
361 }
This page took 0.038531 seconds and 5 git commands to generate.