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
.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
;
51 * <b><u>CreateNewTrace</u></b>
53 * Action implementation to create a new trace.
56 public class CreateNewTrace
implements IObjectActionDelegate
, IWorkbenchWindowActionDelegate
, IViewActionDelegate
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private TargetResource fSelectedTarget
;
63 private List
<TargetResource
> fSelectedFiles
;
64 private String fTraceName
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * Constructor for CreateNewTrace.
73 public CreateNewTrace() {
74 fSelectedFiles
= new ArrayList
<TargetResource
>();
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
83 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
86 public void setActivePart(IAction action
, IWorkbenchPart targetPart
) {
90 * Returns the first selected target resource.
92 * @return first selected target resource
94 protected TargetResource
getFirstSelectedTarget() {
95 if (fSelectedFiles
.size() > 0) {
96 return (TargetResource
) fSelectedFiles
.get(0);
103 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
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
);
112 final TraceConfig traceConfig
= dialog
.open();
114 if (traceConfig
!= null) {
116 fTraceName
= traceConfig
.getTraceName();
119 final ILttControllerService service
= subSystem
.getControllerService();
121 if (fSelectedTarget
.isUst()) {
122 boolean ok
= setupUstLocation(service
, fSelectedTarget
, traceConfig
);
128 // Create future task
129 @SuppressWarnings("unused")
130 Boolean success
= new TCFTask
<Boolean
>() {
134 // Setup trace using Lttng controller service proxy
135 service
.setupTrace(fSelectedTarget
.getParent().getName(), fSelectedTarget
.getName(), traceConfig
.getTraceName(), new ILttControllerService
.DoneSetupTrace() {
138 public void doneSetupTrace(IToken token
, Exception error
, Object str
) {
145 // Notify about success
146 done(Boolean
.valueOf(true));
149 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
151 // Enable all channels by default
152 setChannelEnable(service
, fSelectedTarget
, traceConfig
, TraceControlConstants
.Lttng_Control_AllChannels
, true);
154 } catch (Exception e
) {
155 SystemMessageException sysExp
;
156 if (e
instanceof SystemMessageException
) {
157 sysExp
= (SystemMessageException
)e
;
159 sysExp
= new SystemMessageException(LTTngUiPlugin
.getDefault().getMessage(e
));
161 SystemBasePlugin
.logError(Messages
.Lttng_Control_ErrorNewTrace
+ " (" + //$NON-NLS-1$
162 Messages
.Lttng_Resource_Trace
+ ": " + traceConfig
.getTraceName() + ")", sysExp
); //$NON-NLS-1$ //$NON-NLS-2$
167 TraceResource trace
= new TraceResource(fSelectedTarget
.getSubSystem());
168 trace
.setName(fTraceName
);
169 trace
.setParent(fSelectedTarget
);
172 // in UST the tracing is started after setupTrace!!
173 trace
.setTraceState(TraceState
.STARTED
);
176 trace
.setTraceState(TraceState
.CONFIGURED
);
179 trace
.setTraceConfig(traceConfig
);
180 fSelectedTarget
.addTrace(trace
);
182 ISystemRegistry registry
= SystemStartHere
.getSystemRegistry();
183 registry
.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents
.SYSTEM_REMOTE_RESOURCE_CREATED
, trace
, fSelectedTarget
, subSystem
, null);
189 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
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
);
208 * Returns the active workbench shell of this plug-in.
210 * @return active workbench shell.
212 protected Shell
getShell() {
213 return SystemBasePlugin
.getActiveWorkbenchShell();
218 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
221 public void init(IWorkbenchWindow window
) {
227 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
230 public void dispose() {
235 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
238 public void init(IViewPart view
) {
242 * Setup the trace location for UST.
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();
254 // Create future task
255 boolean ok
= new TCFTask
<Boolean
>() {
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() {
270 public void doneWriteTraceNetwork(IToken token
, Exception error
, Object str
) {
277 // Notify about success
281 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
284 // Create future task
285 boolean ok
= new TCFTask
<Boolean
>() {
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() {
301 public void doneWriteTraceLocal(IToken token
, Exception error
, Object str
) {
308 // Notify about success
312 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
318 * Enable or disable a channel on the remote system.
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
>() {
326 // Set marker enable using Lttng controller service proxy
327 service
.setChannelEnable(targetResource
.getParent().getName(),
328 targetResource
.getName(),
329 traceConfig
.getTraceName(),
332 new ILttControllerService
.DoneSetChannelEnable() {
335 public void doneSetChannelEnable(IToken token
, Exception error
, Object str
) {
342 // Notify about success
343 done(Boolean
.valueOf(true));
346 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);