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