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
.core
.tracecontrol
.model
.TargetResource
;
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
.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 final List
<TargetResource
> fSelectedFiles
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Constructor for CreateNewTrace.
71 public CreateNewTrace() {
72 fSelectedFiles
= new ArrayList
<TargetResource
>();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
83 * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
84 * action.IAction, org.eclipse.ui.IWorkbenchPart)
87 public void setActivePart(IAction action
, IWorkbenchPart targetPart
) {
91 * Returns the first selected target resource.
93 * @return first selected target resource
95 protected TargetResource
getFirstSelectedTarget() {
96 if (fSelectedFiles
.size() > 0) {
97 return fSelectedFiles
.get(0);
105 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
108 public void run(IAction action
) {
109 Shell shell
= getShell();
110 final TargetResource targetResource
= getFirstSelectedTarget();
111 TraceSubSystem subSystem
= (TraceSubSystem
) targetResource
.getSubSystem();
112 NewTraceDialog dialog
= new NewTraceDialog(shell
, subSystem
, targetResource
);
114 final TraceConfig traceConfig
= dialog
.open();
116 if (traceConfig
== null) {
121 final ILttControllerService service
= subSystem
.getControllerService();
123 TraceResource trace
= new TraceResource(targetResource
.getSubSystem(), service
);
124 trace
.setName(traceConfig
.getTraceName());
125 trace
.setParent(targetResource
);
126 trace
.setTraceConfig(traceConfig
);
128 if (targetResource
.isUst()) {
129 boolean ok
= setupUstLocation(service
, targetResource
, traceConfig
);
137 if (!targetResource
.isUst()) {
139 // Enable all channels by default
140 trace
.setChannelEnable(TraceControlConstants
.Lttng_Control_AllChannels
, true);
142 // Set overwrite mode for all channels according to user
143 // selection (true for flight recorder, false for normal)
144 trace
.setChannelOverwrite(TraceControlConstants
.Lttng_Control_AllChannels
, traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
);
146 // Set channel timer for all channels
147 final long period
= 1000;
148 trace
.setChannelTimer(TraceControlConstants
.Lttng_Control_AllChannels
, period
);
150 // Set subbuffer size for all channels
151 final long subbufSize
= 16384;
152 trace
.setChannelSubbufSize(TraceControlConstants
.Lttng_Control_AllChannels
, subbufSize
);
154 // Set number of subbuffers for all channels
155 final long subbufNum
= 2;
156 trace
.setChannelSubbufNum(TraceControlConstants
.Lttng_Control_AllChannels
, subbufNum
);
159 if (traceConfig
.isNetworkTrace()) {
161 File newDir
= new File(traceConfig
.getTracePath());
162 if (!newDir
.exists()) {
163 boolean created
= newDir
.mkdirs();
165 throw new Exception(Messages
.Lttng_Control_ErrorCreateTracePath
+ ": " + traceConfig
.getTracePath()); //$NON-NLS-1$
171 // in UST the tracing is started after setupTrace!!
172 trace
.setTraceState(TraceState
.STARTED
);
174 trace
.setTraceState(TraceState
.CONFIGURED
);
177 targetResource
.addTrace(trace
);
179 ISystemRegistry registry
= SystemStartHere
.getSystemRegistry();
180 registry
.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents
.SYSTEM_REMOTE_RESOURCE_CREATED
, trace
, targetResource
, subSystem
, null);
182 } catch (Exception e
) {
183 SystemMessageException sysExp
;
184 if (e
instanceof SystemMessageException
) {
185 sysExp
= (SystemMessageException
) e
;
187 sysExp
= new SystemMessageException(LTTngUiPlugin
.getDefault().getMessage(e
));
189 SystemBasePlugin
.logError(Messages
.Lttng_Control_ErrorNewTrace
+ " (" + //$NON-NLS-1$
190 Messages
.Lttng_Resource_Trace
+ ": " + traceConfig
.getTraceName() + ")", sysExp
); //$NON-NLS-1$ //$NON-NLS-2$
201 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
202 * .IAction, org.eclipse.jface.viewers.ISelection)
205 @SuppressWarnings("unchecked")
206 public void selectionChanged(IAction action
, ISelection selection
) {
207 if (selection
instanceof IStructuredSelection
) {
208 fSelectedFiles
.clear();
209 // store the selected targets to be used when running
210 Iterator
<IStructuredSelection
> theSet
= ((IStructuredSelection
) selection
).iterator();
211 while (theSet
.hasNext()) {
212 Object obj
= theSet
.next();
213 if (obj
instanceof TargetResource
) {
214 fSelectedFiles
.add((TargetResource
) obj
);
221 * Returns the active workbench shell of this plug-in.
223 * @return active workbench shell.
225 protected Shell
getShell() {
226 return SystemBasePlugin
.getActiveWorkbenchShell();
232 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.
236 public void init(IWorkbenchWindow window
) {
243 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
246 public void dispose() {
252 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
255 public void init(IViewPart view
) {
259 * Setup the trace location for UST.
261 private boolean setupUstLocation(final ILttControllerService service
, final TargetResource targetResource
, final TraceConfig traceConfig
)
263 if (traceConfig
.isNetworkTrace()) {
264 File localDir
= new File(traceConfig
.getTracePath());
265 if (!localDir
.exists()) {
266 boolean success
= localDir
.mkdirs();
272 // Create future task
273 boolean ok
= new TCFTask
<Boolean
>() {
277 // Setup trace location using Lttng controller service proxy
278 service
.writeTraceNetwork(targetResource
.getParent().getName(), targetResource
.getName(), traceConfig
.getTraceName(),
279 traceConfig
.getNumChannel(), traceConfig
.getIsAppend(), traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
,
280 traceConfig
.getMode() == TraceConfig
.NORMAL_MODE
, new ILttControllerService
.DoneWriteTraceNetwork() {
283 public void doneWriteTraceNetwork(IToken token
, Exception error
, Object str
) {
290 // Notify about success
295 }.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
298 // Create future task
299 boolean ok
= new TCFTask
<Boolean
>() {
303 // Setup trace location using Lttng controller service proxy
304 service
.writeTraceLocal(targetResource
.getParent().getName(), targetResource
.getName(), traceConfig
.getTraceName(),
305 traceConfig
.getTracePath(), traceConfig
.getNumChannel(), traceConfig
.getIsAppend(),
306 traceConfig
.getMode() == TraceConfig
.NORMAL_MODE
, traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
,
307 new ILttControllerService
.DoneWriteTraceLocal() {
310 public void doneWriteTraceLocal(IToken token
, Exception error
, Object str
) {
317 // Notify about success
322 }.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);