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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.regex
.Matcher
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
21 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
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
;
25 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandInput
;
26 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandResult
;
27 import org
.eclipse
.tracecompass
.tmf
.remote
.core
.shell
.ICommandShell
;
30 * Factory to create LTTngControlService instances depending on the version of
31 * the LTTng Trace Control installed on the remote host.
33 * @author Bernd Hufmann
36 public final class LTTngControlServiceFactory
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
44 private LTTngControlServiceFactory() {
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 * Gets the LTTng Control Service implementation based on the version of the
55 * - the shell implementation to pass to the service
56 * @return - LTTng Control Service implementation
57 * @throws ExecutionException
58 * If the command fails
60 public static ILttngControlService
getLttngControlService(ICommandShell shell
) throws ExecutionException
{
62 boolean machineInterfaceMode
= true;
64 // Looking for a machine interface on LTTng side
65 ICommandInput command
= shell
.createCommand();
66 command
.add(LTTngControlServiceConstants
.CONTROL_COMMAND
);
67 command
.add(LTTngControlServiceConstants
.CONTROL_COMMAND_MI_OPTION
);
68 command
.add(LTTngControlServiceConstants
.CONTROL_COMMAND_MI_XML
);
69 command
.add(LTTngControlServiceConstants
.COMMAND_VERSION
);
70 ICommandResult result
= executeCommand(shell
, command
);
72 if (result
.getResult() != 0) {
73 machineInterfaceMode
= false;
74 // Fall back if no machine interface is present
75 command
= shell
.createCommand();
76 command
.add(LTTngControlServiceConstants
.CONTROL_COMMAND
);
77 command
.add(LTTngControlServiceConstants
.COMMAND_VERSION
);
78 result
= executeCommand(shell
, command
);
81 if ((result
.getResult() == 0) && (!result
.getOutput().isEmpty())) {
82 if (machineInterfaceMode
) {
83 LTTngControlServiceMI service
= new LTTngControlServiceMI(shell
, LTTngControlService
.class.getResource(LTTngControlServiceConstants
.MI_XSD_FILENAME
));
84 service
.setVersion(result
.getOutput());
88 for (String line
: result
.getOutput()) {
89 line
= line
.replace("-", "."); //$NON-NLS-1$//$NON-NLS-2$
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()) {
95 LTTngControlService service
= new LTTngControlService(shell
);
96 service
.setVersion(checkNotNull(version
));
99 throw new ExecutionException(Messages
.TraceControl_UnsupportedVersionError
+ ": " + version
); //$NON-NLS-1$
103 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
);
106 private static ICommandResult
executeCommand(ICommandShell shell
, ICommandInput command
) throws ExecutionException
{
108 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
109 ControlCommandLogger
.log(command
.toString());
112 ICommandResult result
= null;
115 result
= shell
.executeCommand(command
, new NullProgressMonitor());
116 } catch (ExecutionException e
) {
117 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
+ ": " + e
); //$NON-NLS-1$
121 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
122 ControlCommandLogger
.log(result
.toString());