Fix some null warnings
[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.NonNull;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.tracecompass.common.core.NonNullUtils;
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;
44 import org.eclipse.ui.IWorkbenchPage;
45
46 /**
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>
51 *
52 * @author Bernd Hufmann
53 */
54 public class AssignEventHandler extends BaseControlViewHandler {
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59
60 /**
61 * The command execution parameter.
62 */
63 private Parameter fParam;
64
65 // ------------------------------------------------------------------------
66 // Operations
67 // ------------------------------------------------------------------------
68
69 @Override
70 public Object execute(ExecutionEvent event) throws ExecutionException {
71
72 // Make a copy for thread safety
73 Parameter tmpParam = null;
74 fLock.lock();
75 try {
76 tmpParam = fParam;
77 if (tmpParam == null) {
78 return null;
79 }
80 tmpParam = new Parameter(tmpParam);
81 } finally {
82 fLock.unlock();
83 }
84 final Parameter param = tmpParam;
85
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());
90
91 if (dialog.open() != Window.OK) {
92 return null;
93 }
94
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 }
109
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);
116 }
117
118 } catch (ExecutionException e) {
119 error = e;
120 }
121
122 // refresh in all cases
123 if (session != null) {
124 refresh(new CommandParameter(session));
125 }
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();
135
136 return null;
137 }
138
139 @Override
140 public boolean isEnabled() {
141 @NonNull ArrayList<@NonNull BaseEventComponent> events = new ArrayList<>();
142 TraceSessionComponent[] sessions = null;
143 Boolean isKernel = null;
144
145 // Get workbench page for the Control View
146 IWorkbenchPage page = getWorkbenchPage();
147 if (page == null) {
148 return false;
149 }
150
151 // Check if one or more session are selected
152 ISelection selection = page.getSelection(ControlView.ID);
153 if (selection instanceof StructuredSelection) {
154
155 StructuredSelection structered = ((StructuredSelection) selection);
156 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
157 Object element = iterator.next();
158 if (element instanceof BaseEventComponent) {
159 BaseEventComponent event = (BaseEventComponent) element;
160 ITraceControlComponent provider = event.getParent();
161
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 }
171 if (isKernel == null) {
172 isKernel = Boolean.valueOf(temp);
173 } else {
174 // don't mix events from Kernel and UST provider
175 if (isKernel.booleanValue() != temp) {
176 return false;
177 }
178 }
179
180 // Add BaseEventComponents
181 events.add(event);
182
183 if (sessions == null) {
184 TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent();
185 sessions = root.getSessions();
186 }
187 }
188 }
189 }
190
191 boolean isEnabled = ((!events.isEmpty()) && (sessions != null) && (sessions.length > 0));
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
198 fLock.lock();
199 try {
200 fParam = null;
201 if(isEnabled) {
202 fParam = new Parameter(NonNullUtils.checkNotNull(sessions), events, isKernel);
203 }
204 } finally {
205 fLock.unlock();
206 }
207 return isEnabled;
208 }
209
210 /**
211 * Class containing parameter for the command execution.
212 */
213 @NonNullByDefault
214 private static final class Parameter {
215
216 /**
217 * The list of event components the command is to be executed on.
218 */
219 private final List<BaseEventComponent> fEvents;
220
221 /**
222 * The list of available sessions.
223 */
224 private final TraceSessionComponent[] fSessions;
225
226 /**
227 * Flag for indicating Kernel or UST.
228 */
229 private final boolean fIsKernel;
230
231 /**
232 * Constructor
233 *
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 */
238 public Parameter(TraceSessionComponent[] sessions, List<BaseEventComponent> events, boolean isKernel) {
239 fSessions = NonNullUtils.checkNotNull(Arrays.copyOf(sessions, sessions.length));
240 fEvents = new ArrayList<>();
241 fEvents.addAll(events);
242 fIsKernel = isKernel;
243 }
244
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 }
252
253 public TraceSessionComponent[] getSessions() {
254 return fSessions;
255 }
256
257 public List<BaseEventComponent> getEvents() {
258 return fEvents;
259 }
260
261 public boolean isKernel() {
262 return fIsKernel;
263 }
264 }
265 }
This page took 0.040878 seconds and 5 git commands to generate.