Contribution for Bug343438 (LTTng Trace Control)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / actions / CreateNewTrace.java
CommitLineData
e8d771d5
BH
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 *******************************************************************************/
14package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions;
15
16import java.io.File;
17import java.util.ArrayList;
18import java.util.Iterator;
19import java.util.List;
20import java.util.concurrent.TimeUnit;
21
22import org.eclipse.jface.action.IAction;
23import org.eclipse.jface.viewers.ISelection;
24import org.eclipse.jface.viewers.IStructuredSelection;
25import org.eclipse.linuxtools.lttng.tracecontrol.model.TargetResource;
26import org.eclipse.linuxtools.lttng.tracecontrol.model.TraceResource;
27import org.eclipse.linuxtools.lttng.tracecontrol.model.TraceResource.TraceState;
28import org.eclipse.linuxtools.lttng.tracecontrol.model.config.TraceConfig;
29import org.eclipse.linuxtools.lttng.tracecontrol.service.ILttControllerService;
30import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
31import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
32import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
33import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.NewTraceDialog;
34import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
35import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
36import org.eclipse.rse.core.model.ISystemRegistry;
37import org.eclipse.rse.core.model.SystemStartHere;
38import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
39import org.eclipse.rse.ui.SystemBasePlugin;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.tm.tcf.protocol.IToken;
42import org.eclipse.tm.tcf.util.TCFTask;
43import org.eclipse.ui.IObjectActionDelegate;
44import org.eclipse.ui.IViewActionDelegate;
45import org.eclipse.ui.IViewPart;
46import org.eclipse.ui.IWorkbenchPart;
47import org.eclipse.ui.IWorkbenchWindow;
48import org.eclipse.ui.IWorkbenchWindowActionDelegate;
49
50/**
51 * <b><u>CreateNewTrace</u></b>
52 * <p>
53 * Action implementation to create a new trace.
54 * </p>
55 */
56public class CreateNewTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 private TargetResource fSelectedTarget;
63 private List<TargetResource> fSelectedFiles;
64 private String fTraceName;
65
66 // ------------------------------------------------------------------------
67 // Constructors
68 // ------------------------------------------------------------------------
69
70 /**
71 * Constructor for CreateNewTrace.
72 */
73 public CreateNewTrace() {
74 fSelectedFiles= new ArrayList<TargetResource>();
75 }
76
77 // ------------------------------------------------------------------------
78 // Operations
79 // ------------------------------------------------------------------------
80
81 /*
82 * (non-Javadoc)
83 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
84 */
85 @Override
86 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
87 }
88
89 /**
90 * Returns the first selected target resource.
91 *
92 * @return first selected target resource
93 */
94 protected TargetResource getFirstSelectedTarget() {
95 if (fSelectedFiles.size() > 0) {
96 return (TargetResource) fSelectedFiles.get(0);
97 }
98 return null;
99 }
100
101 /*
102 * (non-Javadoc)
103 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
104 */
105 @Override
106 public void run(IAction action) {
107 Shell shell = getShell();
108 fSelectedTarget = getFirstSelectedTarget();
109 TraceSubSystem subSystem = (TraceSubSystem)fSelectedTarget.getSubSystem();
110 NewTraceDialog dialog = new NewTraceDialog(shell, subSystem, fSelectedTarget);
111
112 final TraceConfig traceConfig = dialog.open();
113
114 if (traceConfig != null) {
115
116 fTraceName = traceConfig.getTraceName();
117
118 try {
119 final ILttControllerService service = subSystem.getControllerService();
120
121 if (fSelectedTarget.isUst()) {
122 boolean ok = setupUstLocation(service, fSelectedTarget, traceConfig);
123 if (!ok) {
124 return;
125 }
126 }
127
128 // Create future task
129 @SuppressWarnings("unused")
130 Boolean success = new TCFTask<Boolean>() {
131 @Override
132 public void run() {
133
134 // Setup trace using Lttng controller service proxy
135 service.setupTrace(fSelectedTarget.getParent().getName(), fSelectedTarget.getName(), traceConfig.getTraceName(), new ILttControllerService.DoneSetupTrace() {
136
137 @Override
138 public void doneSetupTrace(IToken token, Exception error, Object str) {
139 if (error != null) {
140 // Notify with error
141 error(error);
142 return;
143 }
144
145 // Notify about success
146 done(Boolean.valueOf(true));
147 }
148 });
149 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
150
151 // Enable all channels by default
152 setChannelEnable(service, fSelectedTarget, traceConfig, TraceControlConstants.Lttng_Control_AllChannels, true);
153
154 } catch (Exception e) {
155 SystemMessageException sysExp;
156 if (e instanceof SystemMessageException) {
157 sysExp = (SystemMessageException)e;
158 } else {
159 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
160 }
161 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorNewTrace + " (" + //$NON-NLS-1$
162 Messages.Lttng_Resource_Trace + ": " + traceConfig.getTraceName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
163
164 return;
165 }
166
167 TraceResource trace = new TraceResource(fSelectedTarget.getSubSystem());
168 trace.setName(fTraceName);
169 trace.setParent(fSelectedTarget);
170
171 if (trace.isUst()) {
172 // in UST the tracing is started after setupTrace!!
173 trace.setTraceState(TraceState.STARTED);
174 }
175 else {
176 trace.setTraceState(TraceState.CONFIGURED);
177 }
178
179 trace.setTraceConfig(traceConfig);
180 fSelectedTarget.addTrace(trace);
181
182 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
183 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CREATED, trace, fSelectedTarget, subSystem, null);
184 }
185 }
186
187 /*
188 * (non-Javadoc)
189 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
190 */
191 @Override
192 @SuppressWarnings("unchecked")
193 public void selectionChanged(IAction action, ISelection selection) {
194 if (selection instanceof IStructuredSelection) {
195 fSelectedFiles.clear();
196 // store the selected targets to be used when running
197 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
198 while (theSet.hasNext()) {
199 Object obj = theSet.next();
200 if (obj instanceof TargetResource) {
201 fSelectedFiles.add((TargetResource)obj);
202 }
203 }
204 }
205 }
206
207 /**
208 * Returns the active workbench shell of this plug-in.
209 *
210 * @return active workbench shell.
211 */
212 protected Shell getShell() {
213 return SystemBasePlugin.getActiveWorkbenchShell();
214 }
215
216 /*
217 * (non-Javadoc)
218 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
219 */
220 @Override
221 public void init(IWorkbenchWindow window) {
222
223 }
224
225 /*
226 * (non-Javadoc)
227 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
228 */
229 @Override
230 public void dispose() {
231 }
232
233 /*
234 * (non-Javadoc)
235 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
236 */
237 @Override
238 public void init(IViewPart view) {
239 }
240
241 /*
242 * Setup the trace location for UST.
243 */
244 private boolean setupUstLocation(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig) throws Exception {
245 if (traceConfig.isNetworkTrace()) {
246 File localDir = new File(traceConfig.getTracePath());
247 if (!localDir.exists()) {
248 boolean success = localDir.mkdirs();
249 if (!success) {
250 return false;
251 }
252 }
253
254 // Create future task
255 boolean ok = new TCFTask<Boolean>() {
256 @Override
257 public void run() {
258
259 // Setup trace location using Lttng controller service proxy
260 service.writeTraceNetwork(targetResource.getParent().getName(),
261 targetResource.getName(),
262 traceConfig.getTraceName(),
263 traceConfig.getNumChannel(),
264 traceConfig.getIsAppend(),
265 traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
266 traceConfig.getMode() == TraceConfig.NORMAL_MODE,
267 new ILttControllerService.DoneWriteTraceNetwork() {
268
269 @Override
270 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
271 if (error != null) {
272 // Notify with error
273 error(error);
274 return;
275 }
276
277 // Notify about success
278 done(true);
279 }
280 });
281 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
282 return ok;
283 } else {
284 // Create future task
285 boolean ok = new TCFTask<Boolean>() {
286 @Override
287 public void run() {
288
289 // Setup trace location using Lttng controller service proxy
290 service.writeTraceLocal(targetResource.getParent().getName(),
291 targetResource.getName(),
292 traceConfig.getTraceName(),
293 traceConfig.getTracePath(),
294 traceConfig.getNumChannel(),
295 traceConfig.getIsAppend(),
296 traceConfig.getMode() == TraceConfig.NORMAL_MODE,
297 traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
298 new ILttControllerService.DoneWriteTraceLocal() {
299
300 @Override
301 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
302 if (error != null) {
303 // Notify with error
304 error(error);
305 return;
306 }
307
308 // Notify about success
309 done(true);
310 }
311 });
312 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
313 return ok;
314 }
315 }
316
317 /*
318 * Enable or disable a channel on the remote system.
319 */
320 private void setChannelEnable(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig, final String channelName, final boolean enabled) throws Exception{
321 // Create future task
322 new TCFTask<Boolean>() {
323 @Override
324 public void run() {
325
326 // Set marker enable using Lttng controller service proxy
327 service.setChannelEnable(targetResource.getParent().getName(),
328 targetResource.getName(),
329 traceConfig.getTraceName(),
330 channelName,
331 enabled,
332 new ILttControllerService.DoneSetChannelEnable() {
333
334 @Override
335 public void doneSetChannelEnable(IToken token, Exception error, Object str) {
336 if (error != null) {
337 // Notify with error
338 error(error);
339 return;
340 }
341
342 // Notify about success
343 done(Boolean.valueOf(true));
344 }
345 });
346 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
347 }
348}
This page took 0.036639 seconds and 5 git commands to generate.