Commit | Line | Data |
---|---|---|
276c17e7 | 1 | /********************************************************************** |
ce709731 | 2 | * Copyright (c) 2012, 2016 Ericsson |
cfdb727a | 3 | * |
276c17e7 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: | |
276c17e7 | 10 | * Bernd Hufmann - Initial API and implementation |
0df4af5f | 11 | * Jonathan Rajotte - machine interface support |
276c17e7 | 12 | **********************************************************************/ |
9bc60be7 | 13 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.service; |
276c17e7 | 14 | |
364dcfaf BH |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
276c17e7 BH |
17 | import java.util.regex.Matcher; |
18 | ||
19 | import org.eclipse.core.commands.ExecutionException; | |
20 | import org.eclipse.core.runtime.NullProgressMonitor; | |
13729cbc | 21 | import org.eclipse.jdt.annotation.NonNullByDefault; |
9bc60be7 AM |
22 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.logging.ControlCommandLogger; |
23 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; | |
24 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlPreferences; | |
364dcfaf | 25 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput; |
ec619615 BH |
26 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult; |
27 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell; | |
276c17e7 BH |
28 | |
29 | /** | |
0df4af5f JRJ |
30 | * Factory to create LTTngControlService instances depending on the version of |
31 | * the LTTng Trace Control installed on the remote host. | |
cfdb727a | 32 | * |
dbd4432d | 33 | * @author Bernd Hufmann |
276c17e7 | 34 | */ |
13729cbc BH |
35 | @NonNullByDefault |
36 | public final class LTTngControlServiceFactory { | |
cfdb727a | 37 | |
276c17e7 BH |
38 | // ------------------------------------------------------------------------ |
39 | // Constructor | |
40 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
41 | /** |
42 | * Constructor | |
43 | */ | |
276c17e7 BH |
44 | private LTTngControlServiceFactory() { |
45 | } | |
46 | ||
276c17e7 BH |
47 | // ------------------------------------------------------------------------ |
48 | // Factory method | |
49 | // ------------------------------------------------------------------------ | |
c5f68877 | 50 | /** |
cfdb727a | 51 | * Gets the LTTng Control Service implementation based on the version of the |
c5f68877 | 52 | * remote LTTng Tools. |
cfdb727a | 53 | * |
0df4af5f JRJ |
54 | * @param shell |
55 | * - the shell implementation to pass to the service | |
c5f68877 | 56 | * @return - LTTng Control Service implementation |
0df4af5f JRJ |
57 | * @throws ExecutionException |
58 | * If the command fails | |
c5f68877 | 59 | */ |
13729cbc | 60 | public static ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
276c17e7 | 61 | // get the version |
0df4af5f | 62 | boolean machineInterfaceMode = true; |
13729cbc BH |
63 | |
64 | // Looking for a machine interface on LTTng side | |
364dcfaf | 65 | ICommandInput command = shell.createCommand(); |
774a7993 | 66 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); |
13729cbc BH |
67 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_OPTION); |
68 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_XML); | |
774a7993 | 69 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); |
13729cbc | 70 | ICommandResult result = executeCommand(shell, command); |
cfdb727a | 71 | |
0df4af5f JRJ |
72 | if (result.getResult() != 0) { |
73 | machineInterfaceMode = false; | |
74 | // Fall back if no machine interface is present | |
364dcfaf | 75 | command = shell.createCommand(); |
13729cbc BH |
76 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); |
77 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); | |
78 | result = executeCommand(shell, command); | |
0df4af5f JRJ |
79 | } |
80 | ||
cbc46cc9 | 81 | if ((result.getResult() == 0) && (!result.getOutput().isEmpty())) { |
0df4af5f | 82 | if (machineInterfaceMode) { |
ce709731 | 83 | LTTngControlServiceMI service = new LTTngControlServiceMI(shell, LTTngControlServiceMI.parseVersion(result)); |
0df4af5f JRJ |
84 | return service; |
85 | } | |
cbc46cc9 BH |
86 | |
87 | for (String line : result.getOutput()) { | |
c541f121 | 88 | line = line.replace("-", "."); //$NON-NLS-1$//$NON-NLS-2$ |
cfe737e4 BH |
89 | Matcher versionMatcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
90 | if (versionMatcher.matches()) { | |
91 | String version = versionMatcher.group(1).trim(); | |
92 | Matcher matcher = LTTngControlServiceConstants.VERSION_2_PATTERN.matcher(version); | |
93 | if (matcher.matches()) { | |
276c17e7 | 94 | LTTngControlService service = new LTTngControlService(shell); |
364dcfaf | 95 | service.setVersion(checkNotNull(version)); |
276c17e7 BH |
96 | return service; |
97 | } | |
98 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
99 | } | |
276c17e7 BH |
100 | } |
101 | } | |
102 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
103 | } | |
13729cbc | 104 | |
364dcfaf | 105 | private static ICommandResult executeCommand(ICommandShell shell, ICommandInput command) throws ExecutionException { |
13729cbc BH |
106 | // Logging |
107 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
364dcfaf | 108 | ControlCommandLogger.log(command.toString()); |
13729cbc BH |
109 | } |
110 | ||
111 | ICommandResult result = null; | |
112 | ||
113 | try { | |
114 | result = shell.executeCommand(command, new NullProgressMonitor()); | |
115 | } catch (ExecutionException e) { | |
116 | throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$ | |
117 | } | |
118 | ||
119 | // Output logging | |
120 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
121 | ControlCommandLogger.log(result.toString()); | |
122 | } | |
123 | return result; | |
124 | } | |
276c17e7 | 125 | } |