1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
13 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.actions
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.TimeUnit
;
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
.core
.tracecontrol
.model
.TraceResource
;
27 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.model
.TraceResource
.TraceState
;
28 import org
.eclipse
.linuxtools
.lttng
.core
.tracecontrol
.model
.config
.TraceConfig
;
29 import org
.eclipse
.linuxtools
.lttng
.core
.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
;
51 * <b><u>StartTrace</u></b>
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.
57 public class StartTrace
implements IObjectActionDelegate
, IWorkbenchWindowActionDelegate
, IViewActionDelegate
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private List
<TraceResource
> fSelectedTraces
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 fSelectedTraces
= new ArrayList
<TraceResource
>();
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
79 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
82 public void setActivePart(IAction arg0
, IWorkbenchPart arg1
) {
87 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
90 public void run(IAction arg0
) {
91 for (int i
= 0; i
< fSelectedTraces
.size(); i
++) {
93 TraceResource trace
= (TraceResource
) fSelectedTraces
.get(i
);
94 TraceSubSystem subSystem
= (TraceSubSystem
)trace
.getSubSystem();
96 TraceConfig traceConfig
= trace
.getTraceConfig();
97 if (traceConfig
!= null) {
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
);
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()))) {
108 SelectTracePathDialog selectDialog
= new SelectTracePathDialog(SystemBasePlugin
.getActiveWorkbenchShell());
110 if (selectDialog
.open() == Window
.OK
) {
111 traceConfig
.setTracePath(selectDialog
.getTracePath());
114 // we don't have place to store the trace files ... go to the next trace
119 startTrace(service
, trace
, traceConfig
);
121 trace
.setTraceState(TraceState
.STARTED
);
124 ISystemRegistry registry
= SystemStartHere
.getSystemRegistry();
125 registry
.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents
.SYSTEM_REMOTE_RESOURCE_CHANGED
, trace
, trace
.getParent(), subSystem
, null);
127 } catch (Exception e
) {
128 SystemMessageException sysExp
;
129 if (e
instanceof SystemMessageException
) {
130 sysExp
= (SystemMessageException
)e
;
132 sysExp
= new SystemMessageException(LTTngUiPlugin
.getDefault().getMessage(e
));
134 SystemBasePlugin
.logError(Messages
.Lttng_Control_ErrorStart
+ " (" + //$NON-NLS-1$
135 Messages
.Lttng_Resource_Trace
+ ": " + trace
.getName() + ")", sysExp
); //$NON-NLS-1$ //$NON-NLS-2$
143 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
145 @SuppressWarnings("unchecked")
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
);
162 * Returns the active workbench shell of this plug-in.
164 * @return active workbench shell.
166 protected Shell
getShell() {
167 return SystemBasePlugin
.getActiveWorkbenchShell();
172 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
175 public void init(IWorkbenchWindow arg0
) {
180 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
183 public void dispose() {
188 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
191 public void init(IViewPart view
) {
195 * Setup trace transport on the remote system
197 private void setTraceTransport(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
198 // Create future task
199 new TCFTask
<Boolean
>() {
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() {
211 public void doneSetTraceTransport(IToken token
, Exception error
, Object str
) {
218 // Notify about success
219 done(Boolean
.valueOf(true));
222 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
226 * Allocate trace resources on the remote system.
228 private void allocTrace(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
229 new TCFTask
<Boolean
>() {
233 // Setup trace transport using Lttng controller service proxy
234 service
.allocTrace(trace
.getParent().getParent().getName(),
235 trace
.getParent().getName(),
237 new ILttControllerService
.DoneAllocTrace() {
240 public void doneAllocTrace(IToken token
, Exception error
, Object str
) {
247 // Notify about success
248 done(Boolean
.valueOf(true));
251 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
255 * Setup the trace location. Only normal channels are written while trace is started.
257 private void setupLocation(final ILttControllerService service
, final TraceResource trace
, final TraceConfig traceConfig
) throws Exception
{
258 if (traceConfig
.isNetworkTrace()) {
260 File newDir
= new File(traceConfig
.getTracePath());
261 if (!newDir
.exists()) {
262 boolean created
= newDir
.mkdirs();
264 throw new Exception(Messages
.Lttng_Control_ErrorCreateTracePath
+ ": " + traceConfig
.getTracePath()); //$NON-NLS-1$
268 if (traceConfig
.getProject() != null) {
269 ImportToProject
.linkTrace(getShell(), trace
, traceConfig
.getProject(), traceConfig
.getTraceName());
272 // Create future task
273 new TCFTask
<Boolean
>() {
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(),
284 true, // write only normal channels
285 new ILttControllerService
.DoneWriteTraceNetwork() {
288 public void doneWriteTraceNetwork(IToken token
, Exception error
, Object str
) {
295 // Notify about success
296 done(Boolean
.valueOf(true));
299 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
303 // Create future task
304 new TCFTask
<Boolean
>() {
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(),
316 true, // write only normal channels
317 new ILttControllerService
.DoneWriteTraceLocal() {
320 public void doneWriteTraceLocal(IToken token
, Exception error
, Object str
) {
327 // Notify about success
328 done(Boolean
.valueOf(true));
331 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
336 * Starts the trace on the remote system.
338 private void startTrace(final ILttControllerService service
, final TraceResource trace
, final TraceConfig oldConfig
) throws Exception
{
339 new TCFTask
<Boolean
>() {
343 // Setup trace transport using Lttng controller service proxy
344 service
.startTrace(trace
.getParent().getParent().getName(), trace
.getParent().getName(), oldConfig
.getTraceName(), new ILttControllerService
.DoneStartTrace() {
347 public void doneStartTrace(IToken token
, Exception error
, Object str
) {
355 // Notify about success
356 done(Boolean
.valueOf(true));
359 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);