Contribution for Bug353020
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / tracecontrol / actions / CreateNewTrace.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.linuxtools.lttng.tracecontrol.model.TargetResource;
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.TraceControlConstants;
32 import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
33 import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.NewTraceDialog;
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>CreateNewTrace</u></b>
52 * <p>
53 * Action implementation to create a new trace.
54 * </p>
55 */
56 public class CreateNewTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 private List<TargetResource> fSelectedFiles;
63
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
68 /**
69 * Constructor for CreateNewTrace.
70 */
71 public CreateNewTrace() {
72 fSelectedFiles= new ArrayList<TargetResource>();
73 }
74
75 // ------------------------------------------------------------------------
76 // Operations
77 // ------------------------------------------------------------------------
78
79 /*
80 * (non-Javadoc)
81 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
82 */
83 @Override
84 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
85 }
86
87 /**
88 * Returns the first selected target resource.
89 *
90 * @return first selected target resource
91 */
92 protected TargetResource getFirstSelectedTarget() {
93 if (fSelectedFiles.size() > 0) {
94 return (TargetResource) fSelectedFiles.get(0);
95 }
96 return null;
97 }
98
99 /*
100 * (non-Javadoc)
101 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
102 */
103 @Override
104 public void run(IAction action) {
105 Shell shell = getShell();
106 final TargetResource targetResource = getFirstSelectedTarget();
107 TraceSubSystem subSystem = (TraceSubSystem)targetResource.getSubSystem();
108 NewTraceDialog dialog = new NewTraceDialog(shell, subSystem, targetResource);
109
110 final TraceConfig traceConfig = dialog.open();
111
112 if (traceConfig == null) {
113 return;
114 }
115
116 try {
117 final ILttControllerService service = subSystem.getControllerService();
118
119 TraceResource trace = new TraceResource(targetResource.getSubSystem(), service);
120 trace.setName(traceConfig.getTraceName());
121 trace.setParent(targetResource);
122 trace.setTraceConfig(traceConfig);
123
124 if (targetResource.isUst()) {
125 boolean ok = setupUstLocation(service, targetResource, traceConfig);
126 if (!ok) {
127 return;
128 }
129 }
130
131 trace.setupTrace();
132
133 if (!targetResource.isUst()) {
134
135 // Enable all channels by default
136 trace.setChannelEnable(TraceControlConstants.Lttng_Control_AllChannels, true);
137
138 // Set overwrite mode for all channels according to user selection (true for flight recorder, false for normal)
139 trace.setChannelOverwrite(TraceControlConstants.Lttng_Control_AllChannels, traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE);
140
141 // Set channel timer for all channels
142 final long period = 5000;
143 trace.setChannelTimer(TraceControlConstants.Lttng_Control_AllChannels, period);
144
145 // Set subbuffer size for all channels
146 final long subbufSize = 16384;
147 trace.setChannelSubbufSize(TraceControlConstants.Lttng_Control_AllChannels, subbufSize);
148
149 // Set number of subbuffers for all channels
150 final long subbufNum = 2;
151 trace.setChannelSubbufNum(TraceControlConstants.Lttng_Control_AllChannels, subbufNum);
152 }
153
154 if (traceConfig.isNetworkTrace()) {
155
156 File newDir = new File(traceConfig.getTracePath());
157 if (!newDir.exists()) {
158 boolean created = newDir.mkdirs();
159 if (!created) {
160 throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
161 }
162 }
163 }
164
165 if (trace.isUst()) {
166 // in UST the tracing is started after setupTrace!!
167 trace.setTraceState(TraceState.STARTED);
168 }
169 else {
170 trace.setTraceState(TraceState.CONFIGURED);
171 }
172
173 targetResource.addTrace(trace);
174
175 ISystemRegistry registry = SystemStartHere.getSystemRegistry();
176 registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CREATED, trace, targetResource, subSystem, null);
177
178 } catch (Exception e) {
179 SystemMessageException sysExp;
180 if (e instanceof SystemMessageException) {
181 sysExp = (SystemMessageException)e;
182 } else {
183 sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
184 }
185 SystemBasePlugin.logError(Messages.Lttng_Control_ErrorNewTrace + " (" + //$NON-NLS-1$
186 Messages.Lttng_Resource_Trace + ": " + traceConfig.getTraceName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
187
188 return;
189 }
190
191 }
192
193 /*
194 * (non-Javadoc)
195 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
196 */
197 @Override
198 @SuppressWarnings("unchecked")
199 public void selectionChanged(IAction action, ISelection selection) {
200 if (selection instanceof IStructuredSelection) {
201 fSelectedFiles.clear();
202 // store the selected targets to be used when running
203 Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
204 while (theSet.hasNext()) {
205 Object obj = theSet.next();
206 if (obj instanceof TargetResource) {
207 fSelectedFiles.add((TargetResource)obj);
208 }
209 }
210 }
211 }
212
213 /**
214 * Returns the active workbench shell of this plug-in.
215 *
216 * @return active workbench shell.
217 */
218 protected Shell getShell() {
219 return SystemBasePlugin.getActiveWorkbenchShell();
220 }
221
222 /*
223 * (non-Javadoc)
224 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
225 */
226 @Override
227 public void init(IWorkbenchWindow window) {
228
229 }
230
231 /*
232 * (non-Javadoc)
233 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
234 */
235 @Override
236 public void dispose() {
237 }
238
239 /*
240 * (non-Javadoc)
241 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
242 */
243 @Override
244 public void init(IViewPart view) {
245 }
246
247 /*
248 * Setup the trace location for UST.
249 */
250 private boolean setupUstLocation(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig) throws Exception {
251 if (traceConfig.isNetworkTrace()) {
252 File localDir = new File(traceConfig.getTracePath());
253 if (!localDir.exists()) {
254 boolean success = localDir.mkdirs();
255 if (!success) {
256 return false;
257 }
258 }
259
260 // Create future task
261 boolean ok = new TCFTask<Boolean>() {
262 @Override
263 public void run() {
264
265 // Setup trace location using Lttng controller service proxy
266 service.writeTraceNetwork(targetResource.getParent().getName(),
267 targetResource.getName(),
268 traceConfig.getTraceName(),
269 traceConfig.getNumChannel(),
270 traceConfig.getIsAppend(),
271 traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
272 traceConfig.getMode() == TraceConfig.NORMAL_MODE,
273 new ILttControllerService.DoneWriteTraceNetwork() {
274
275 @Override
276 public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
277 if (error != null) {
278 // Notify with error
279 error(error);
280 return;
281 }
282
283 // Notify about success
284 done(true);
285 }
286 });
287 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
288 return ok;
289 } else {
290 // Create future task
291 boolean ok = new TCFTask<Boolean>() {
292 @Override
293 public void run() {
294
295 // Setup trace location using Lttng controller service proxy
296 service.writeTraceLocal(targetResource.getParent().getName(),
297 targetResource.getName(),
298 traceConfig.getTraceName(),
299 traceConfig.getTracePath(),
300 traceConfig.getNumChannel(),
301 traceConfig.getIsAppend(),
302 traceConfig.getMode() == TraceConfig.NORMAL_MODE,
303 traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
304 new ILttControllerService.DoneWriteTraceLocal() {
305
306 @Override
307 public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
308 if (error != null) {
309 // Notify with error
310 error(error);
311 return;
312 }
313
314 // Notify about success
315 done(true);
316 }
317 });
318 }}.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
319 return ok;
320 }
321 }
322
323 }
This page took 0.038514 seconds and 6 git commands to generate.