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