Commit | Line | Data |
---|---|---|
276c17e7 | 1 | /********************************************************************** |
0df4af5f | 2 | * Copyright (c) 2012, 2014 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 BH |
14 | |
15 | import java.util.regex.Matcher; | |
16 | ||
17 | import org.eclipse.core.commands.ExecutionException; | |
18 | import org.eclipse.core.runtime.NullProgressMonitor; | |
9bc60be7 AM |
19 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.logging.ControlCommandLogger; |
20 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; | |
21 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlPreferences; | |
22 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote.ICommandResult; | |
23 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote.ICommandShell; | |
276c17e7 BH |
24 | |
25 | /** | |
0df4af5f JRJ |
26 | * Factory to create LTTngControlService instances depending on the version of |
27 | * the LTTng Trace Control installed on the remote host. | |
cfdb727a | 28 | * |
dbd4432d | 29 | * @author Bernd Hufmann |
276c17e7 BH |
30 | */ |
31 | public class LTTngControlServiceFactory { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
36 | /** |
37 | * The singleton instance. | |
38 | */ | |
276c17e7 | 39 | private static LTTngControlServiceFactory fInstance = null; |
cfdb727a | 40 | |
276c17e7 BH |
41 | // ------------------------------------------------------------------------ |
42 | // Constructor | |
43 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
44 | /** |
45 | * Constructor | |
46 | */ | |
276c17e7 BH |
47 | private LTTngControlServiceFactory() { |
48 | } | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Accessors | |
52 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
53 | /** |
54 | * @return the LTTngControlServiceFactory singleton instance. | |
55 | */ | |
276c17e7 BH |
56 | public static synchronized LTTngControlServiceFactory getInstance() { |
57 | if (fInstance == null) { | |
58 | fInstance = new LTTngControlServiceFactory(); | |
59 | } | |
60 | return fInstance; | |
61 | } | |
62 | ||
63 | // ------------------------------------------------------------------------ | |
64 | // Factory method | |
65 | // ------------------------------------------------------------------------ | |
c5f68877 | 66 | /** |
cfdb727a | 67 | * Gets the LTTng Control Service implementation based on the version of the |
c5f68877 | 68 | * remote LTTng Tools. |
cfdb727a | 69 | * |
0df4af5f JRJ |
70 | * @param shell |
71 | * - the shell implementation to pass to the service | |
c5f68877 | 72 | * @return - LTTng Control Service implementation |
0df4af5f JRJ |
73 | * @throws ExecutionException |
74 | * If the command fails | |
c5f68877 | 75 | */ |
276c17e7 BH |
76 | public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
77 | // get the version | |
0df4af5f | 78 | boolean machineInterfaceMode = true; |
7a6e4bfd | 79 | String command = LTTngControlServiceConstants.CONTROL_COMMAND + LTTngControlServiceConstants.COMMAND_VERSION; |
0df4af5f | 80 | String commandMi = LTTngControlServiceConstants.CONTROL_COMMAND_MI_XML + LTTngControlServiceConstants.COMMAND_VERSION; |
7a6e4bfd | 81 | |
0df4af5f | 82 | // Logging |
7a6e4bfd | 83 | if (ControlPreferences.getInstance().isLoggingEnabled()) { |
0df4af5f | 84 | ControlCommandLogger.log(commandMi); |
7a6e4bfd | 85 | } |
0df4af5f | 86 | |
973e19d6 | 87 | ICommandResult result = null; |
7a6e4bfd | 88 | |
0df4af5f | 89 | // Looking for a machine interface on LTTng side |
973e19d6 | 90 | try { |
0df4af5f | 91 | result = shell.executeCommand(commandMi, new NullProgressMonitor()); |
973e19d6 | 92 | } catch (ExecutionException e) { |
0df4af5f | 93 | throw new ExecutionException(Messages.TraceControl_GettingVersionError, e); |
973e19d6 | 94 | } |
7a6e4bfd | 95 | |
0df4af5f | 96 | // Output logging |
7a6e4bfd BH |
97 | if (ControlPreferences.getInstance().isLoggingEnabled()) { |
98 | ControlCommandLogger.log(LTTngControlService.formatOutput(result)); | |
99 | } | |
cfdb727a | 100 | |
0df4af5f JRJ |
101 | if (result.getResult() != 0) { |
102 | machineInterfaceMode = false; | |
103 | // Fall back if no machine interface is present | |
104 | ||
105 | // Logging | |
106 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
107 | ControlCommandLogger.log(command); | |
108 | } | |
109 | ||
110 | try { | |
111 | result = shell.executeCommand(command, new NullProgressMonitor()); | |
112 | } catch (ExecutionException e) { | |
113 | throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$ | |
114 | } | |
115 | ||
116 | // Output logging | |
117 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
118 | ControlCommandLogger.log(LTTngControlService.formatOutput(result)); | |
119 | } | |
120 | } | |
121 | ||
122 | ||
d128c979 | 123 | if ((result != null) && (result.getResult() == 0) && (result.getOutput().length >= 1)) { |
0df4af5f JRJ |
124 | if (machineInterfaceMode) { |
125 | LTTngControlServiceMI service = new LTTngControlServiceMI(shell, LTTngControlService.class.getResource(LTTngControlServiceConstants.MI_XSD_FILENAME)); | |
126 | service.setVersion(result.getOutput()); | |
127 | return service; | |
128 | } | |
276c17e7 BH |
129 | int index = 0; |
130 | while (index < result.getOutput().length) { | |
131 | String line = result.getOutput()[index]; | |
c541f121 | 132 | line = line.replace("-", "."); //$NON-NLS-1$//$NON-NLS-2$ |
cfe737e4 BH |
133 | Matcher versionMatcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
134 | if (versionMatcher.matches()) { | |
135 | String version = versionMatcher.group(1).trim(); | |
136 | Matcher matcher = LTTngControlServiceConstants.VERSION_2_PATTERN.matcher(version); | |
137 | if (matcher.matches()) { | |
276c17e7 BH |
138 | LTTngControlService service = new LTTngControlService(shell); |
139 | service.setVersion(version); | |
140 | return service; | |
141 | } | |
142 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
143 | } | |
144 | index++; | |
145 | } | |
146 | } | |
147 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
148 | } | |
149 | } |