1 /**********************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal, 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 * Patrick-Jeffrey Pollo Guilbert - Added headers, exporting .lttng profiles
11 * William Enright - Added ProfileHandler implementation
12 * William Tri-Khiem Truong - Completed documentation
13 * Bernd Hufmann - Renamed from ProfileHandler and redesign
14 **********************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.handlers
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.core
.filesystem
.EFS
;
23 import org
.eclipse
.core
.filesystem
.IFileStore
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.core
.runtime
.SubMonitor
;
30 import org
.eclipse
.core
.runtime
.jobs
.Job
;
31 import org
.eclipse
.jface
.viewers
.ISelection
;
32 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
33 import org
.eclipse
.jface
.window
.Window
;
34 import org
.eclipse
.osgi
.util
.NLS
;
35 import org
.eclipse
.remote
.core
.IRemoteConnection
;
36 import org
.eclipse
.remote
.core
.IRemoteFileService
;
37 import org
.eclipse
.remote
.core
.IRemoteProcessService
;
38 import org
.eclipse
.remote
.core
.RemoteServicesUtils
;
39 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
40 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.ControlView
;
41 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.ILoadDialog
;
42 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.dialogs
.TraceControlDialogFactory
;
43 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
44 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionGroup
;
45 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.service
.ILttngControlService
;
46 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.service
.LTTngControlServiceConstants
;
47 import org
.eclipse
.ui
.IWorkbenchPage
;
50 * Command handler implementation to execute load command.
52 * @author Bernd Hufmann
53 * @author Patrick-Jeffrey Pollo Guilbert
54 * @author William Enright
55 * @author William Tri-Khiem Truong
57 public class LoadHandler
extends BaseControlViewHandler
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * The trace session group the command is to be executed on.
66 private TraceSessionGroup fSessionGroup
= null;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
75 final TraceSessionGroup sessionGroup
;
76 final IRemoteConnection connection
;
80 sessionGroup
= fSessionGroup
;
81 if (sessionGroup
== null) {
84 connection
= sessionGroup
.getTargetNode().getRemoteSystemProxy().getRemoteConnection();
89 // Open dialog box for the session input path
90 final ILoadDialog dialog
= TraceControlDialogFactory
.getInstance().getLoadDialog();
91 dialog
.initialize(connection
);
92 if (dialog
.open() != Window
.OK
) {
96 Job job
= new Job(Messages
.TraceControl_LoadJob
) {
98 protected IStatus
run(IProgressMonitor monitor
) {
101 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, 3);
102 // create destination directory (if necessary)
103 IRemoteProcessService processService
= connection
.getService(IRemoteProcessService
.class);
105 if (processService
!= null) {
106 String cwd
= processService
.getWorkingDirectory();
107 path
= RemoteServicesUtils
.posixPath(cwd
);
108 path
= path
.append(LTTngControlServiceConstants
.DEFAULT_PATH
);
112 return Status
.CANCEL_STATUS
;
115 ILttngControlService service
= sessionGroup
.getControlService();
116 List
<String
> commands
= new ArrayList
<>();
117 commands
.add("mkdir -p " + path
.toString()); //$NON-NLS-1$
118 service
.runCommands(subMonitor
.newChild(1), commands
);
121 IRemoteFileService fileService
= connection
.getService(IRemoteFileService
.class);
122 if (fileService
== null) {
123 return Status
.CANCEL_STATUS
;
126 List
<IFileStore
> localFiles
= dialog
.getLocalResources();
127 List
<IFileStore
> remoteResources
;
128 if (localFiles
!= null) {
129 remoteResources
= new ArrayList
<>();
130 SubMonitor childMonitor
= subMonitor
.newChild(1);
131 for (IFileStore local
: localFiles
) {
132 IPath remotePath
= RemoteServicesUtils
.posixPath(path
.toString()).append(local
.getName());
133 IFileStore remoteResource
= fileService
.getResource(remotePath
.toString());
134 local
.copy(remoteResource
, EFS
.OVERWRITE
, childMonitor
);
135 remoteResources
.add(remoteResource
);
138 subMonitor
.newChild(1);
139 remoteResources
= dialog
.getRemoteResources();
141 loadRemoteProfile(sessionGroup
, subMonitor
.newChild(1), remoteResources
, dialog
.isForce());
142 } catch (ExecutionException
| CoreException e
) {
143 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_LoadFailure
, e
);
144 } catch (InterruptedException e
) {
145 return Status
.CANCEL_STATUS
;
148 return Status
.OK_STATUS
;
159 public boolean isEnabled() {
161 // Get workbench page for the Control View
162 IWorkbenchPage page
= getWorkbenchPage();
167 TraceSessionGroup sessionGroup
= null;
169 // Check if the session group project is selected
170 ISelection selection
= page
.getSelection(ControlView
.ID
);
171 if (selection
instanceof StructuredSelection
) {
172 Object element
= ((StructuredSelection
) selection
).getFirstElement();
173 sessionGroup
= (element
instanceof TraceSessionGroup
) ?
(TraceSessionGroup
) element
: null;
176 boolean isEnabled
= sessionGroup
!= null;
179 fSessionGroup
= null;
181 fSessionGroup
= sessionGroup
;
189 private static void loadRemoteProfile(final TraceSessionGroup sessionGroup
, IProgressMonitor monitor
, List
<IFileStore
> files
, boolean isForce
) throws ExecutionException
, InterruptedException
{
190 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, files
.size());
191 for (IFileStore file
: files
) {
192 // Check if operation was cancelled.
193 if (subMonitor
.isCanceled()) {
194 throw new InterruptedException();
196 subMonitor
.beginTask(NLS
.bind(Messages
.TraceControl_LoadTask
, file
.getName()), 1);
197 sessionGroup
.loadSession(file
.toURI().getPath(), isForce
, subMonitor
);