Commit | Line | Data |
---|---|---|
6503ae0f | 1 | /********************************************************************** |
68d1057b | 2 | * Copyright (c) 2012, 2015 Ericsson |
cfdb727a | 3 | * |
6503ae0f 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: | |
6503ae0f | 10 | * Bernd Hufmann - Initial API and implementation |
ba3a9bd2 | 11 | * Bernd Hufmann - Updated for support of LTTng Tools 2.1 |
6503ae0f | 12 | **********************************************************************/ |
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers; |
6503ae0f BH |
14 | |
15 | import java.util.ArrayList; | |
c56972bb | 16 | import java.util.Arrays; |
6503ae0f BH |
17 | import java.util.Iterator; |
18 | import java.util.List; | |
19 | ||
6503ae0f BH |
20 | import org.eclipse.core.commands.ExecutionEvent; |
21 | import org.eclipse.core.commands.ExecutionException; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.Status; | |
25 | import org.eclipse.core.runtime.jobs.Job; | |
aa353506 | 26 | import org.eclipse.jdt.annotation.NonNull; |
68d1057b | 27 | import org.eclipse.jdt.annotation.NonNullByDefault; |
6503ae0f BH |
28 | import org.eclipse.jface.viewers.ISelection; |
29 | import org.eclipse.jface.viewers.StructuredSelection; | |
30 | import org.eclipse.jface.window.Window; | |
68d1057b | 31 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
9bc60be7 AM |
32 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
33 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView; | |
34 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.IGetEventInfoDialog; | |
35 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory; | |
36 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; | |
37 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent; | |
38 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.BaseEventComponent; | |
39 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.KernelProviderComponent; | |
40 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent; | |
41 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceChannelComponent; | |
42 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; | |
43 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.UstProviderComponent; | |
6503ae0f | 44 | import org.eclipse.ui.IWorkbenchPage; |
6503ae0f BH |
45 | |
46 | /** | |
6503ae0f BH |
47 | * <p> |
48 | * Command handler implementation to assign events to a session and channel and enable/configure them. | |
49 | * This is done on the trace provider level. | |
50 | * </p> | |
cfdb727a | 51 | * |
dbd4432d | 52 | * @author Bernd Hufmann |
6503ae0f | 53 | */ |
d132bcc7 | 54 | public class AssignEventHandler extends BaseControlViewHandler { |
6503ae0f BH |
55 | |
56 | // ------------------------------------------------------------------------ | |
57 | // Attributes | |
58 | // ------------------------------------------------------------------------ | |
11252342 | 59 | |
6503ae0f | 60 | /** |
c56972bb | 61 | * The command execution parameter. |
6503ae0f | 62 | */ |
c56972bb | 63 | private Parameter fParam; |
cfdb727a | 64 | |
6503ae0f BH |
65 | // ------------------------------------------------------------------------ |
66 | // Operations | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
6503ae0f BH |
69 | @Override |
70 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
71 | ||
68d1057b BH |
72 | // Make a copy for thread safety |
73 | Parameter tmpParam = null; | |
c56972bb BH |
74 | fLock.lock(); |
75 | try { | |
68d1057b BH |
76 | tmpParam = fParam; |
77 | if (tmpParam == null) { | |
c56972bb BH |
78 | return null; |
79 | } | |
68d1057b BH |
80 | tmpParam = new Parameter(tmpParam); |
81 | } finally { | |
82 | fLock.unlock(); | |
83 | } | |
84 | final Parameter param = tmpParam; | |
6503ae0f | 85 | |
68d1057b BH |
86 | // Open dialog box to retrieve the session and channel where the events should be enabled in. |
87 | final IGetEventInfoDialog dialog = TraceControlDialogFactory.getInstance().getGetEventInfoDialog(); | |
88 | dialog.setIsKernel(param.isKernel()); | |
89 | dialog.setSessions(param.getSessions()); | |
f455db37 | 90 | |
68d1057b BH |
91 | if (dialog.open() != Window.OK) { |
92 | return null; | |
93 | } | |
6503ae0f | 94 | |
68d1057b BH |
95 | Job job = new Job(Messages.TraceControl_EnableEventsJob) { |
96 | @Override | |
97 | protected IStatus run(IProgressMonitor monitor) { | |
98 | ||
99 | Exception error = null; | |
100 | TraceSessionComponent session = dialog.getSession(); | |
101 | try { | |
102 | List<String> eventNames = new ArrayList<>(); | |
103 | List<BaseEventComponent> events = param.getEvents(); | |
104 | // Create list of event names | |
105 | for (Iterator<BaseEventComponent> iterator = events.iterator(); iterator.hasNext();) { | |
106 | BaseEventComponent baseEvent = iterator.next(); | |
107 | eventNames.add(baseEvent.getName()); | |
108 | } | |
c56972bb | 109 | |
68d1057b BH |
110 | TraceChannelComponent channel = dialog.getChannel(); |
111 | if (channel == null) { | |
112 | // enable events on default channel (which will be created by lttng-tools) | |
113 | session.enableEvents(eventNames, param.isKernel(), dialog.getFilterExpression(), monitor); | |
114 | } else { | |
115 | channel.enableEvents(eventNames, dialog.getFilterExpression(), monitor); | |
6503ae0f | 116 | } |
6503ae0f | 117 | |
68d1057b BH |
118 | } catch (ExecutionException e) { |
119 | error = e; | |
120 | } | |
c56972bb | 121 | |
68d1057b BH |
122 | // refresh in all cases |
123 | if (session != null) { | |
124 | refresh(new CommandParameter(session)); | |
6503ae0f | 125 | } |
68d1057b BH |
126 | |
127 | if (error != null) { | |
128 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_EnableEventsFailure, error); | |
129 | } | |
130 | return Status.OK_STATUS; | |
131 | } | |
132 | }; | |
133 | job.setUser(true); | |
134 | job.schedule(); | |
c56972bb | 135 | |
6503ae0f BH |
136 | return null; |
137 | } | |
138 | ||
6503ae0f BH |
139 | @Override |
140 | public boolean isEnabled() { | |
aa353506 | 141 | @NonNull ArrayList<@NonNull BaseEventComponent> events = new ArrayList<>(); |
367e2932 | 142 | @NonNull TraceSessionComponent[] sessions = null; |
c56972bb | 143 | Boolean isKernel = null; |
6503ae0f | 144 | |
c56972bb | 145 | // Get workbench page for the Control View |
d132bcc7 | 146 | IWorkbenchPage page = getWorkbenchPage(); |
6503ae0f BH |
147 | if (page == null) { |
148 | return false; | |
149 | } | |
150 | ||
6503ae0f BH |
151 | // Check if one or more session are selected |
152 | ISelection selection = page.getSelection(ControlView.ID); | |
153 | if (selection instanceof StructuredSelection) { | |
cfdb727a | 154 | |
6503ae0f BH |
155 | StructuredSelection structered = ((StructuredSelection) selection); |
156 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 157 | Object element = iterator.next(); |
6503ae0f BH |
158 | if (element instanceof BaseEventComponent) { |
159 | BaseEventComponent event = (BaseEventComponent) element; | |
160 | ITraceControlComponent provider = event.getParent(); | |
cfdb727a | 161 | |
6503ae0f BH |
162 | // check for kernel or UST provider |
163 | boolean temp = false; | |
164 | if (provider instanceof KernelProviderComponent) { | |
165 | temp = true; | |
166 | } else if (provider instanceof UstProviderComponent) { | |
167 | temp = false; | |
168 | } else { | |
169 | return false; | |
170 | } | |
c56972bb BH |
171 | if (isKernel == null) { |
172 | isKernel = Boolean.valueOf(temp); | |
6503ae0f BH |
173 | } else { |
174 | // don't mix events from Kernel and UST provider | |
c56972bb | 175 | if (isKernel.booleanValue() != temp) { |
6503ae0f BH |
176 | return false; |
177 | } | |
178 | } | |
179 | ||
180 | // Add BaseEventComponents | |
c56972bb | 181 | events.add(event); |
cfdb727a | 182 | |
c56972bb | 183 | if (sessions == null) { |
6503ae0f | 184 | TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent(); |
c56972bb | 185 | sessions = root.getSessions(); |
6503ae0f BH |
186 | } |
187 | } | |
188 | } | |
189 | } | |
c56972bb BH |
190 | |
191 | boolean isEnabled = ((!events.isEmpty()) && (sessions != null) && (sessions.length > 0)); | |
64636df8 BH |
192 | |
193 | // To avoid compiler warnings check for null even if isKernel is always not null when used below | |
194 | if (isKernel == null) { | |
195 | return false; | |
196 | } | |
197 | ||
c56972bb BH |
198 | fLock.lock(); |
199 | try { | |
200 | fParam = null; | |
201 | if(isEnabled) { | |
68d1057b | 202 | fParam = new Parameter(NonNullUtils.checkNotNull(sessions), events, isKernel); |
c56972bb BH |
203 | } |
204 | } finally { | |
205 | fLock.unlock(); | |
206 | } | |
207 | return isEnabled; | |
208 | } | |
209 | ||
210 | /** | |
cfdb727a | 211 | * Class containing parameter for the command execution. |
c56972bb | 212 | */ |
68d1057b | 213 | @NonNullByDefault |
77735e82 | 214 | private static final class Parameter { |
c56972bb BH |
215 | |
216 | /** | |
cfdb727a | 217 | * The list of event components the command is to be executed on. |
c56972bb | 218 | */ |
cfdb727a AM |
219 | private final List<BaseEventComponent> fEvents; |
220 | ||
c56972bb BH |
221 | /** |
222 | * The list of available sessions. | |
223 | */ | |
367e2932 | 224 | private final @NonNull TraceSessionComponent[] fSessions; |
cfdb727a | 225 | |
c56972bb BH |
226 | /** |
227 | * Flag for indicating Kernel or UST. | |
228 | */ | |
77735e82 | 229 | private final boolean fIsKernel; |
cfdb727a | 230 | |
c56972bb BH |
231 | /** |
232 | * Constructor | |
cfdb727a | 233 | * |
c56972bb BH |
234 | * @param sessions - a array of trace sessions |
235 | * @param events - a lists of events to enable | |
236 | * @param isKernel - domain (true for kernel or UST) | |
237 | */ | |
367e2932 | 238 | public Parameter(@NonNull TraceSessionComponent[] sessions, List<BaseEventComponent> events, boolean isKernel) { |
68d1057b | 239 | fSessions = NonNullUtils.checkNotNull(Arrays.copyOf(sessions, sessions.length)); |
e0838ca1 | 240 | fEvents = new ArrayList<>(); |
c56972bb BH |
241 | fEvents.addAll(events); |
242 | fIsKernel = isKernel; | |
243 | } | |
cfdb727a | 244 | |
c56972bb BH |
245 | /** |
246 | * Copy constructor | |
247 | * @param other - a parameter to copy | |
248 | */ | |
249 | public Parameter(Parameter other) { | |
250 | this(other.fSessions, other.fEvents, other.fIsKernel); | |
251 | } | |
cfdb727a | 252 | |
c56972bb BH |
253 | public TraceSessionComponent[] getSessions() { |
254 | return fSessions; | |
255 | } | |
cfdb727a | 256 | |
c56972bb BH |
257 | public List<BaseEventComponent> getEvents() { |
258 | return fEvents; | |
259 | } | |
cfdb727a | 260 | |
c56972bb BH |
261 | public boolean isKernel() { |
262 | return fIsKernel; | |
263 | } | |
6503ae0f BH |
264 | } |
265 | } |