1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
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
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
17 import java
.util
.regex
.Matcher
;
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
.tmf
.remote
.core
.shell
.ICommandResult
;
25 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandShell
;
28 * Factory to create LTTngControlService instances depending on the version of
29 * the LTTng Trace Control installed on the remote host.
31 * @author Bernd Hufmann
33 public class LTTngControlServiceFactory
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 * The singleton instance.
41 private static LTTngControlServiceFactory fInstance
= null;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
49 private LTTngControlServiceFactory() {
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 * @return the LTTngControlServiceFactory singleton instance.
58 public static synchronized LTTngControlServiceFactory
getInstance() {
59 if (fInstance
== null) {
60 fInstance
= new LTTngControlServiceFactory();
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 * Gets the LTTng Control Service implementation based on the version of the
73 * - the shell implementation to pass to the service
74 * @return - LTTng Control Service implementation
75 * @throws ExecutionException
76 * If the command fails
78 public ILttngControlService
getLttngControlService(ICommandShell shell
) throws ExecutionException
{
80 boolean machineInterfaceMode
= true;
81 List
<String
> command
= new ArrayList
<>();
82 command
.add(LTTngControlServiceConstants
.CONTROL_COMMAND
);
83 command
.add(LTTngControlServiceConstants
.COMMAND_VERSION
);
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
);
92 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
93 ControlCommandLogger
.log(LTTngControlService
.toCommandString(commandMi
));
96 ICommandResult result
= null;
98 // Looking for a machine interface on LTTng side
100 result
= shell
.executeCommand(commandMi
, new NullProgressMonitor());
101 } catch (ExecutionException e
) {
102 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
, e
);
106 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
107 ControlCommandLogger
.log(LTTngControlService
.formatOutput(result
));
110 if (result
.getResult() != 0) {
111 machineInterfaceMode
= false;
112 // Fall back if no machine interface is present
115 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
116 ControlCommandLogger
.log(LTTngControlService
.toCommandString(command
));
120 result
= shell
.executeCommand(command
, new NullProgressMonitor());
121 } catch (ExecutionException e
) {
122 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
+ ": " + e
); //$NON-NLS-1$
126 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
127 ControlCommandLogger
.log(LTTngControlService
.formatOutput(result
));
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());
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
);
151 throw new ExecutionException(Messages
.TraceControl_UnsupportedVersionError
+ ": " + version
); //$NON-NLS-1$
156 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
);