1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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
.linuxtools
.internal
.lttng2
.control
.ui
.views
.service
;
15 import java
.util
.regex
.Matcher
;
17 import org
.eclipse
.core
.commands
.ExecutionException
;
18 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
19 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.logging
.ControlCommandLogger
;
20 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
21 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.preferences
.ControlPreferences
;
22 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.remote
.ICommandResult
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.remote
.ICommandShell
;
26 * Factory to create LTTngControlService instances depending on the version of
27 * the LTTng Trace Control installed on the remote host.
29 * @author Bernd Hufmann
31 public class LTTngControlServiceFactory
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 * The singleton instance.
39 private static LTTngControlServiceFactory fInstance
= null;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
47 private LTTngControlServiceFactory() {
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 * @return the LTTngControlServiceFactory singleton instance.
56 public static synchronized LTTngControlServiceFactory
getInstance() {
57 if (fInstance
== null) {
58 fInstance
= new LTTngControlServiceFactory();
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 * Gets the LTTng Control Service implementation based on the version of the
71 * - the shell implementation to pass to the service
72 * @return - LTTng Control Service implementation
73 * @throws ExecutionException
74 * If the command fails
76 public ILttngControlService
getLttngControlService(ICommandShell shell
) throws ExecutionException
{
78 boolean machineInterfaceMode
= true;
79 String command
= LTTngControlServiceConstants
.CONTROL_COMMAND
+ LTTngControlServiceConstants
.COMMAND_VERSION
;
80 String commandMi
= LTTngControlServiceConstants
.CONTROL_COMMAND_MI_XML
+ LTTngControlServiceConstants
.COMMAND_VERSION
;
83 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
84 ControlCommandLogger
.log(commandMi
);
87 ICommandResult result
= null;
89 // Looking for a machine interface on LTTng side
91 result
= shell
.executeCommand(commandMi
, new NullProgressMonitor());
92 } catch (ExecutionException e
) {
93 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
, e
);
97 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
98 ControlCommandLogger
.log(LTTngControlService
.formatOutput(result
));
101 if (result
.getResult() != 0) {
102 machineInterfaceMode
= false;
103 // Fall back if no machine interface is present
106 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
107 ControlCommandLogger
.log(command
);
111 result
= shell
.executeCommand(command
, new NullProgressMonitor());
112 } catch (ExecutionException e
) {
113 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
+ ": " + e
); //$NON-NLS-1$
117 if (ControlPreferences
.getInstance().isLoggingEnabled()) {
118 ControlCommandLogger
.log(LTTngControlService
.formatOutput(result
));
123 if ((result
!= null) && (result
.getResult() == 0) && (result
.getOutput().length
>= 1)) {
124 if (machineInterfaceMode
) {
125 LTTngControlServiceMI service
= new LTTngControlServiceMI(shell
, LTTngControlService
.class.getResource(LTTngControlServiceConstants
.MI_XSD_FILENAME
));
126 service
.setVersion(result
.getOutput());
130 while (index
< result
.getOutput().length
) {
131 String line
= result
.getOutput()[index
];
132 Matcher versionMatcher
= LTTngControlServiceConstants
.VERSION_PATTERN
.matcher(line
);
133 if (versionMatcher
.matches()) {
134 String version
= versionMatcher
.group(1).trim();
135 Matcher matcher
= LTTngControlServiceConstants
.VERSION_2_PATTERN
.matcher(version
);
136 if (matcher
.matches()) {
137 LTTngControlService service
= new LTTngControlService(shell
);
138 service
.setVersion(version
);
141 throw new ExecutionException(Messages
.TraceControl_UnsupportedVersionError
+ ": " + version
); //$NON-NLS-1$
146 throw new ExecutionException(Messages
.TraceControl_GettingVersionError
);