Commit | Line | Data |
---|---|---|
276c17e7 | 1 | /********************************************************************** |
ed902a2b | 2 | * Copyright (c) 2012, 2015 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 | |
774a7993 BH |
15 | import java.util.ArrayList; |
16 | import java.util.List; | |
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; | |
ec619615 BH |
25 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult; |
26 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell; | |
276c17e7 BH |
27 | |
28 | /** | |
0df4af5f JRJ |
29 | * Factory to create LTTngControlService instances depending on the version of |
30 | * the LTTng Trace Control installed on the remote host. | |
cfdb727a | 31 | * |
dbd4432d | 32 | * @author Bernd Hufmann |
276c17e7 | 33 | */ |
13729cbc BH |
34 | @NonNullByDefault |
35 | public final class LTTngControlServiceFactory { | |
cfdb727a | 36 | |
276c17e7 BH |
37 | // ------------------------------------------------------------------------ |
38 | // Constructor | |
39 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
40 | /** |
41 | * Constructor | |
42 | */ | |
276c17e7 BH |
43 | private LTTngControlServiceFactory() { |
44 | } | |
45 | ||
276c17e7 BH |
46 | // ------------------------------------------------------------------------ |
47 | // Factory method | |
48 | // ------------------------------------------------------------------------ | |
c5f68877 | 49 | /** |
cfdb727a | 50 | * Gets the LTTng Control Service implementation based on the version of the |
c5f68877 | 51 | * remote LTTng Tools. |
cfdb727a | 52 | * |
0df4af5f JRJ |
53 | * @param shell |
54 | * - the shell implementation to pass to the service | |
c5f68877 | 55 | * @return - LTTng Control Service implementation |
0df4af5f JRJ |
56 | * @throws ExecutionException |
57 | * If the command fails | |
c5f68877 | 58 | */ |
13729cbc | 59 | public static ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
276c17e7 | 60 | // get the version |
0df4af5f | 61 | boolean machineInterfaceMode = true; |
13729cbc BH |
62 | |
63 | // Looking for a machine interface on LTTng side | |
774a7993 BH |
64 | List<String> command = new ArrayList<>(); |
65 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); | |
13729cbc BH |
66 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_OPTION); |
67 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_XML); | |
774a7993 | 68 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); |
13729cbc | 69 | ICommandResult result = executeCommand(shell, command); |
cfdb727a | 70 | |
0df4af5f JRJ |
71 | if (result.getResult() != 0) { |
72 | machineInterfaceMode = false; | |
73 | // Fall back if no machine interface is present | |
13729cbc BH |
74 | command.clear(); |
75 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); | |
76 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); | |
77 | result = executeCommand(shell, command); | |
0df4af5f JRJ |
78 | } |
79 | ||
cbc46cc9 | 80 | if ((result.getResult() == 0) && (!result.getOutput().isEmpty())) { |
0df4af5f JRJ |
81 | if (machineInterfaceMode) { |
82 | LTTngControlServiceMI service = new LTTngControlServiceMI(shell, LTTngControlService.class.getResource(LTTngControlServiceConstants.MI_XSD_FILENAME)); | |
83 | service.setVersion(result.getOutput()); | |
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 BH |
94 | LTTngControlService service = new LTTngControlService(shell); |
95 | service.setVersion(version); | |
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 BH |
104 | |
105 | private static ICommandResult executeCommand(ICommandShell shell, List<String> command) throws ExecutionException { | |
106 | // Logging | |
107 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
108 | ControlCommandLogger.log(LTTngControlService.toCommandString(command)); | |
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 | } |