Commit | Line | Data |
---|---|---|
291cbdbf | 1 | /********************************************************************** |
ba3a9bd2 | 2 | * Copyright (c) 2012, 2013 Ericsson |
cfdb727a | 3 | * |
291cbdbf BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
291cbdbf | 10 | * Bernd Hufmann - Initial API and implementation |
ba3a9bd2 | 11 | * Bernd Hufmann - Updated for support of streamed traces |
291cbdbf BH |
12 | **********************************************************************/ |
13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; | |
14 | ||
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.core.commands.ExecutionEvent; | |
19 | import org.eclipse.core.commands.ExecutionException; | |
20 | import org.eclipse.core.resources.IFolder; | |
21 | import org.eclipse.core.resources.IProject; | |
22 | import org.eclipse.core.runtime.CoreException; | |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
24 | import org.eclipse.core.runtime.IStatus; | |
25 | import org.eclipse.core.runtime.NullProgressMonitor; | |
26 | import org.eclipse.core.runtime.Status; | |
27 | import org.eclipse.core.runtime.jobs.Job; | |
28 | import org.eclipse.jface.viewers.ISelection; | |
29 | import org.eclipse.jface.viewers.StructuredSelection; | |
30 | import org.eclipse.jface.window.Window; | |
9315aeee | 31 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState; |
291cbdbf BH |
32 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
291cbdbf | 34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog; |
f455db37 | 35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ImportFileInfo; |
291cbdbf | 36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; |
9315aeee | 37 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
291cbdbf BH |
38 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
39 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
40 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; | |
41 | import org.eclipse.rse.services.files.IFileService; | |
42 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; | |
43 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem; | |
44 | import org.eclipse.ui.IWorkbenchPage; | |
45 | import org.eclipse.ui.IWorkbenchWindow; | |
46 | import org.eclipse.ui.PlatformUI; | |
47 | ||
48 | /** | |
291cbdbf BH |
49 | * <p> |
50 | * Command handler implementation to import traces from a (remote) session to a tracing project. | |
51 | * </p> | |
cfdb727a | 52 | * |
dbd4432d | 53 | * @author Bernd Hufmann |
291cbdbf BH |
54 | */ |
55 | public class ImportHandler extends BaseControlViewHandler { | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Attributes | |
59 | // ------------------------------------------------------------------------ | |
6f4e8ec0 AM |
60 | |
61 | /** | |
62 | * The command parameter | |
63 | */ | |
291cbdbf | 64 | protected CommandParameter fParam; |
cfdb727a | 65 | |
291cbdbf BH |
66 | // ------------------------------------------------------------------------ |
67 | // Accessors | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | // ------------------------------------------------------------------------ | |
71 | // Operations | |
72 | // ------------------------------------------------------------------------ | |
cfdb727a | 73 | |
291cbdbf BH |
74 | /* |
75 | * (non-Javadoc) | |
76 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
77 | */ | |
78 | @Override | |
79 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
80 | ||
81 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
82 | ||
83 | if (window == null) { | |
84 | return false; | |
85 | } | |
86 | ||
87 | fLock.lock(); | |
88 | try { | |
89 | final CommandParameter param = fParam.clone(); | |
cfdb727a | 90 | |
291cbdbf BH |
91 | final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog(); |
92 | dialog.setSession(param.getSession()); | |
93 | ||
f3b33d40 | 94 | if ((dialog.open() != Window.OK) || param.getSession().isStreamedTrace()) { |
291cbdbf BH |
95 | return null; |
96 | } | |
97 | ||
98 | Job job = new Job(Messages.TraceControl_ImportJob) { | |
99 | @Override | |
100 | protected IStatus run(IProgressMonitor monitor) { | |
101 | try { | |
102 | List<ImportFileInfo> traces = dialog.getTracePathes(); | |
103 | IProject project = dialog.getProject(); | |
cfdb727a | 104 | |
291cbdbf | 105 | for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) { |
cfdb727a | 106 | ImportFileInfo remoteFile = iterator.next(); |
291cbdbf BH |
107 | downloadTrace(remoteFile, project); |
108 | } | |
109 | ||
110 | } catch (ExecutionException e) { | |
cfdb727a AM |
111 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e); |
112 | } | |
291cbdbf BH |
113 | return Status.OK_STATUS; |
114 | } | |
115 | }; | |
116 | job.setUser(true); | |
117 | job.schedule(); | |
118 | } finally { | |
119 | fLock.unlock(); | |
120 | } | |
121 | return null; | |
122 | } | |
123 | ||
124 | /* | |
125 | * (non-Javadoc) | |
126 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
127 | */ | |
128 | @Override | |
129 | public boolean isEnabled() { | |
130 | // Get workbench page for the Control View | |
131 | IWorkbenchPage page = getWorkbenchPage(); | |
132 | if (page == null) { | |
133 | return false; | |
134 | } | |
135 | ||
136 | // Check if one or more session are selected | |
137 | ISelection selection = page.getSelection(ControlView.ID); | |
138 | TraceSessionComponent session = null; | |
139 | if (selection instanceof StructuredSelection) { | |
140 | StructuredSelection structered = ((StructuredSelection) selection); | |
141 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 142 | Object element = iterator.next(); |
291cbdbf BH |
143 | if (element instanceof TraceSessionComponent) { |
144 | // Add only TraceSessionComponents that are inactive and not destroyed | |
145 | TraceSessionComponent tmpSession = (TraceSessionComponent) element; | |
146 | if ((tmpSession.getSessionState() == TraceSessionState.INACTIVE) && (!tmpSession.isDestroyed())) { | |
147 | session = tmpSession; | |
148 | } | |
149 | } | |
150 | } | |
151 | } | |
152 | boolean isEnabled = session != null; | |
153 | ||
154 | fLock.lock(); | |
155 | try { | |
156 | fParam = null; | |
157 | if (isEnabled) { | |
158 | fParam = new CommandParameter(session); | |
159 | } | |
160 | } finally { | |
161 | fLock.unlock(); | |
162 | } | |
163 | return isEnabled; | |
164 | } | |
cfdb727a | 165 | |
291cbdbf BH |
166 | // ------------------------------------------------------------------------ |
167 | // Helper methods | |
168 | // ------------------------------------------------------------------------ | |
169 | /** | |
170 | * Downloads a trace from the remote host to the given project. | |
cfdb727a AM |
171 | * |
172 | * @param trace | |
173 | * - trace information of trace to import | |
174 | * @param project | |
175 | * - project to import to | |
291cbdbf BH |
176 | * @throws ExecutionException |
177 | */ | |
0a78d11a AM |
178 | private static void downloadTrace(ImportFileInfo trace, IProject project) |
179 | throws ExecutionException { | |
291cbdbf BH |
180 | try { |
181 | IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem(); | |
cfdb727a | 182 | |
291cbdbf BH |
183 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); |
184 | if (!traceFolder.exists()) { | |
185 | throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ | |
186 | } | |
187 | ||
188 | String traceName = trace.getLocalTraceName(); | |
189 | IFolder folder = traceFolder.getFolder(traceName); | |
190 | if (folder.exists()) { | |
191 | if(!trace.isOverwrite()) { | |
192 | throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$ | |
193 | } | |
194 | } else { | |
195 | folder.create(true, true, null); | |
196 | } | |
197 | ||
198 | IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor()); | |
199 | ||
200 | String[] destinations = new String[sources.length]; | |
201 | String[] encodings = new String[sources.length]; | |
202 | for (int i = 0; i < sources.length; i++) { | |
203 | destinations[i] = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString(); | |
204 | encodings[i] = null; | |
205 | } | |
cfdb727a | 206 | |
291cbdbf | 207 | fsss.downloadMultiple(sources, destinations, encodings, new NullProgressMonitor()); |
cfdb727a | 208 | |
291cbdbf | 209 | } catch (SystemMessageException e) { |
9fa32496 | 210 | throw new ExecutionException(e.toString(), e); |
291cbdbf | 211 | } catch (CoreException e) { |
9fa32496 | 212 | throw new ExecutionException(e.toString(), e); |
291cbdbf BH |
213 | } |
214 | } | |
215 | } |