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 | ||
13729cbc | 80 | if ((result.getResult() == 0) && (result.getOutput().length >= 1)) { |
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 | } | |
276c17e7 BH |
86 | int index = 0; |
87 | while (index < result.getOutput().length) { | |
88 | String line = result.getOutput()[index]; | |
c541f121 | 89 | line = line.replace("-", "."); //$NON-NLS-1$//$NON-NLS-2$ |
cfe737e4 BH |
90 | Matcher versionMatcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
91 | if (versionMatcher.matches()) { | |
92 | String version = versionMatcher.group(1).trim(); | |
93 | Matcher matcher = LTTngControlServiceConstants.VERSION_2_PATTERN.matcher(version); | |
94 | if (matcher.matches()) { | |
276c17e7 BH |
95 | LTTngControlService service = new LTTngControlService(shell); |
96 | service.setVersion(version); | |
97 | return service; | |
98 | } | |
99 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
100 | } | |
101 | index++; | |
102 | } | |
103 | } | |
104 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
105 | } | |
13729cbc BH |
106 | |
107 | private static ICommandResult executeCommand(ICommandShell shell, List<String> command) throws ExecutionException { | |
108 | // Logging | |
109 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
110 | ControlCommandLogger.log(LTTngControlService.toCommandString(command)); | |
111 | } | |
112 | ||
113 | ICommandResult result = null; | |
114 | ||
115 | try { | |
116 | result = shell.executeCommand(command, new NullProgressMonitor()); | |
117 | } catch (ExecutionException e) { | |
118 | throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$ | |
119 | } | |
120 | ||
121 | // Output logging | |
122 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
123 | ControlCommandLogger.log(result.toString()); | |
124 | } | |
125 | return result; | |
126 | } | |
276c17e7 | 127 | } |