~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.ui/src/org/eclipse/linuxtools/internal/lttng/ui/tracecontrol/actions/CreateNewTrace.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.internal.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.internal.lttng.core.tracecontrol.model.TargetResource;
 
26
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TraceResource;
 
27
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TraceResource.TraceState;
 
28
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.config.TraceConfig;
 
29
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.service.ILttControllerService;
 
30
import org.eclipse.linuxtools.internal.lttng.ui.Activator;
 
31
import org.eclipse.linuxtools.internal.lttng.ui.tracecontrol.Messages;
 
32
import org.eclipse.linuxtools.internal.lttng.ui.tracecontrol.TraceControlConstants;
 
33
import org.eclipse.linuxtools.internal.lttng.ui.tracecontrol.dialogs.NewTraceDialog;
 
34
import org.eclipse.linuxtools.internal.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.tcf.protocol.IToken;
 
42
import org.eclipse.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 final 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
     * 
 
82
     * @see
 
83
     * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
 
84
     * action.IAction, org.eclipse.ui.IWorkbenchPart)
 
85
     */
 
86
    @Override
 
87
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
 
88
    }
 
89
 
 
90
    /**
 
91
     * Returns the first selected target resource.
 
92
     * 
 
93
     * @return first selected target resource
 
94
     */
 
95
    protected TargetResource getFirstSelectedTarget() {
 
96
        if (fSelectedFiles.size() > 0) {
 
97
            return fSelectedFiles.get(0);
 
98
        }
 
99
        return null;
 
100
    }
 
101
 
 
102
    /*
 
103
     * (non-Javadoc)
 
104
     * 
 
105
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 
106
     */
 
107
    @Override
 
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);
 
113
 
 
114
        final TraceConfig traceConfig = dialog.open();
 
115
 
 
116
        if (traceConfig == null) {
 
117
            return;
 
118
        }
 
119
 
 
120
        try {
 
121
            final ILttControllerService service = subSystem.getControllerService();
 
122
 
 
123
            TraceResource trace = new TraceResource(targetResource.getSubSystem(), service);
 
124
            trace.setName(traceConfig.getTraceName());
 
125
            trace.setParent(targetResource);
 
126
            trace.setTraceConfig(traceConfig);
 
127
 
 
128
            if (targetResource.isUst()) {
 
129
                boolean ok = setupUstLocation(service, targetResource, traceConfig);
 
130
                if (!ok) {
 
131
                    return;
 
132
                }
 
133
            }
 
134
 
 
135
            trace.setupTrace();
 
136
 
 
137
            if (!targetResource.isUst()) {
 
138
 
 
139
                // Enable all channels by default
 
140
                trace.setChannelEnable(TraceControlConstants.Lttng_Control_AllChannels, true);
 
141
 
 
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);
 
145
 
 
146
                // Set channel timer for all channels
 
147
                final long period = 1000;
 
148
                trace.setChannelTimer(TraceControlConstants.Lttng_Control_AllChannels, period);
 
149
 
 
150
                // Set subbuffer size for all channels
 
151
                final long subbufSize = 16384;
 
152
                trace.setChannelSubbufSize(TraceControlConstants.Lttng_Control_AllChannels, subbufSize);
 
153
 
 
154
                // Set number of subbuffers for all channels
 
155
                final long subbufNum = 2;
 
156
                trace.setChannelSubbufNum(TraceControlConstants.Lttng_Control_AllChannels, subbufNum);
 
157
            }
 
158
 
 
159
            if (traceConfig.isNetworkTrace()) {
 
160
 
 
161
                File newDir = new File(traceConfig.getTracePath());
 
162
                if (!newDir.exists()) {
 
163
                    boolean created = newDir.mkdirs();
 
164
                    if (!created) {
 
165
                        throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
 
166
                    }
 
167
                }
 
168
            }
 
169
 
 
170
            if (trace.isUst()) {
 
171
                // in UST the tracing is started after setupTrace!!
 
172
                trace.setTraceState(TraceState.STARTED);
 
173
            } else {
 
174
                trace.setTraceState(TraceState.CONFIGURED);
 
175
            }
 
176
 
 
177
            targetResource.addTrace(trace);
 
178
 
 
179
            ISystemRegistry registry = SystemStartHere.getSystemRegistry();
 
180
            registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CREATED, trace, targetResource, subSystem, null);
 
181
 
 
182
        } catch (Exception e) {
 
183
            SystemMessageException sysExp;
 
184
            if (e instanceof SystemMessageException) {
 
185
                sysExp = (SystemMessageException) e;
 
186
            } else {
 
187
                sysExp = new SystemMessageException(Activator.getDefault().getMessage(e));
 
188
            }
 
189
            SystemBasePlugin.logError(Messages.Lttng_Control_ErrorNewTrace + " (" + //$NON-NLS-1$
 
190
                    Messages.Lttng_Resource_Trace + ": " + traceConfig.getTraceName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$
 
191
 
 
192
            return;
 
193
        }
 
194
 
 
195
    }
 
196
 
 
197
    /*
 
198
     * (non-Javadoc)
 
199
     * 
 
200
     * @see
 
201
     * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
 
202
     * .IAction, org.eclipse.jface.viewers.ISelection)
 
203
     */
 
204
    @Override
 
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);
 
215
                }
 
216
            }
 
217
        }
 
218
    }
 
219
 
 
220
    /**
 
221
     * Returns the active workbench shell of this plug-in.
 
222
     * 
 
223
     * @return active workbench shell.
 
224
     */
 
225
    protected Shell getShell() {
 
226
        return SystemBasePlugin.getActiveWorkbenchShell();
 
227
    }
 
228
 
 
229
    /*
 
230
     * (non-Javadoc)
 
231
     * 
 
232
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.
 
233
     * IWorkbenchWindow)
 
234
     */
 
235
    @Override
 
236
    public void init(IWorkbenchWindow window) {
 
237
 
 
238
    }
 
239
 
 
240
    /*
 
241
     * (non-Javadoc)
 
242
     * 
 
243
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
 
244
     */
 
245
    @Override
 
246
    public void dispose() {
 
247
    }
 
248
 
 
249
    /*
 
250
     * (non-Javadoc)
 
251
     * 
 
252
     * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
 
253
     */
 
254
    @Override
 
255
    public void init(IViewPart view) {
 
256
    }
 
257
 
 
258
    /*
 
259
     * Setup the trace location for UST.
 
260
     */
 
261
    private boolean setupUstLocation(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig)
 
262
            throws Exception {
 
263
        if (traceConfig.isNetworkTrace()) {
 
264
            File localDir = new File(traceConfig.getTracePath());
 
265
            if (!localDir.exists()) {
 
266
                boolean success = localDir.mkdirs();
 
267
                if (!success) {
 
268
                    return false;
 
269
                }
 
270
            }
 
271
 
 
272
            // Create future task
 
273
            boolean ok = new TCFTask<Boolean>() {
 
274
                @Override
 
275
                public void run() {
 
276
 
 
277
                    // Setup trace location using Lttng controller service proxy
 
278
                    service.writeTraceNetwork(targetResource.getParent().getName(), targetResource.getName(), traceConfig.getTracePath(), traceConfig.getTraceName(),
 
279
                            traceConfig.getNumChannel(), traceConfig.getIsAppend(), traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
 
280
                            traceConfig.getMode() == TraceConfig.NORMAL_MODE, new ILttControllerService.DoneWriteTraceNetwork() {
 
281
 
 
282
                                @Override
 
283
                                public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
 
284
                                    if (error != null) {
 
285
                                        // Notify with error
 
286
                                        error(error);
 
287
                                        return;
 
288
                                    }
 
289
 
 
290
                                    // Notify about success
 
291
                                    done(true);
 
292
                                }
 
293
                            });
 
294
                }
 
295
            }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
 
296
            return ok;
 
297
        } else {
 
298
            // Create future task
 
299
            boolean ok = new TCFTask<Boolean>() {
 
300
                @Override
 
301
                public void run() {
 
302
 
 
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() {
 
308
 
 
309
                                @Override
 
310
                                public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
 
311
                                    if (error != null) {
 
312
                                        // Notify with error
 
313
                                        error(error);
 
314
                                        return;
 
315
                                    }
 
316
 
 
317
                                    // Notify about success
 
318
                                    done(true);
 
319
                                }
 
320
                            });
 
321
                }
 
322
            }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
 
323
            return ok;
 
324
        }
 
325
    }
 
326
 
 
327
}