control: Add enhanced support for loading of sessions
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / LoadHandler.java
1 /**********************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal, 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 * 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;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
48
49 /**
50 * Command handler implementation to execute load command.
51 *
52 * @author Bernd Hufmann
53 * @author Patrick-Jeffrey Pollo Guilbert
54 * @author William Enright
55 * @author William Tri-Khiem Truong
56 */
57 public class LoadHandler extends BaseControlViewHandler {
58
59 // ------------------------------------------------------------------------
60 // Attributes
61 // ------------------------------------------------------------------------
62
63 /**
64 * The trace session group the command is to be executed on.
65 */
66 private TraceSessionGroup fSessionGroup = null;
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 @Override
73 public Object execute(ExecutionEvent event) throws ExecutionException {
74
75 final TraceSessionGroup sessionGroup;
76 final IRemoteConnection connection;
77
78 fLock.lock();
79 try {
80 sessionGroup = fSessionGroup;
81 if (sessionGroup == null) {
82 return null;
83 }
84 connection = sessionGroup.getTargetNode().getRemoteSystemProxy().getRemoteConnection();
85 } finally {
86 fLock.unlock();
87 }
88
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) {
93 return null;
94 }
95
96 Job job = new Job(Messages.TraceControl_LoadJob) {
97 @Override
98 protected IStatus run(IProgressMonitor monitor) {
99 try {
100
101 SubMonitor subMonitor = SubMonitor.convert(monitor, 3);
102 // create destination directory (if necessary)
103 IRemoteProcessService processService = connection.getService(IRemoteProcessService.class);
104 IPath path = null;
105 if (processService != null) {
106 String cwd = processService.getWorkingDirectory();
107 path = RemoteServicesUtils.posixPath(cwd);
108 path = path.append(LTTngControlServiceConstants.DEFAULT_PATH);
109 }
110
111 if (path == null) {
112 return Status.CANCEL_STATUS;
113 }
114
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);
119
120 // upload files
121 IRemoteFileService fileService = connection.getService(IRemoteFileService.class);
122 if (fileService == null) {
123 return Status.CANCEL_STATUS;
124 }
125
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);
136 }
137 } else {
138 subMonitor.newChild(1);
139 remoteResources = dialog.getRemoteResources();
140 }
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;
146 }
147 monitor.done();
148 return Status.OK_STATUS;
149 }
150
151 };
152 job.setUser(true);
153 job.schedule();
154
155 return null;
156 }
157
158 @Override
159 public boolean isEnabled() {
160
161 // Get workbench page for the Control View
162 IWorkbenchPage page = getWorkbenchPage();
163 if (page == null) {
164 return false;
165 }
166
167 TraceSessionGroup sessionGroup = null;
168
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;
174 }
175
176 boolean isEnabled = sessionGroup != null;
177 fLock.lock();
178 try {
179 fSessionGroup = null;
180 if(isEnabled) {
181 fSessionGroup = sessionGroup;
182 }
183 } finally {
184 fLock.unlock();
185 }
186 return isEnabled;
187 }
188
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();
195 }
196 subMonitor.beginTask(NLS.bind(Messages.TraceControl_LoadTask, file.getName()), 1);
197 sessionGroup.loadSession(file.toURI().getPath(), isForce, subMonitor);
198 subMonitor.done();
199 }
200 }
201 }
This page took 0.042437 seconds and 5 git commands to generate.