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