6d9407725e643a66021986d48716eac3668f2fe9
[deliverable/tracecompass.git] / lttng / 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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
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.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;
28
29 /**
30 * Factory to create LTTngControlService instances depending on the version of
31 * the LTTng Trace Control installed on the remote host.
32 *
33 * @author Bernd Hufmann
34 */
35 @NonNullByDefault
36 public final class LTTngControlServiceFactory {
37
38 // ------------------------------------------------------------------------
39 // Constructor
40 // ------------------------------------------------------------------------
41 /**
42 * Constructor
43 */
44 private LTTngControlServiceFactory() {
45 }
46
47 // ------------------------------------------------------------------------
48 // Factory method
49 // ------------------------------------------------------------------------
50 /**
51 * Gets the LTTng Control Service implementation based on the version of the
52 * remote LTTng Tools.
53 *
54 * @param shell
55 * - the shell implementation to pass to the service
56 * @return - LTTng Control Service implementation
57 * @throws ExecutionException
58 * If the command fails
59 */
60 public static ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException {
61 // get the version
62 boolean machineInterfaceMode = true;
63
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);
71
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);
79 }
80
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());
85 return service;
86 }
87
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));
97 return service;
98 }
99 throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$
100 }
101 }
102 }
103 throw new ExecutionException(Messages.TraceControl_GettingVersionError);
104 }
105
106 private static ICommandResult executeCommand(ICommandShell shell, ICommandInput command) throws ExecutionException {
107 // Logging
108 if (ControlPreferences.getInstance().isLoggingEnabled()) {
109 ControlCommandLogger.log(command.toString());
110 }
111
112 ICommandResult result = null;
113
114 try {
115 result = shell.executeCommand(command, new NullProgressMonitor());
116 } catch (ExecutionException e) {
117 throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$
118 }
119
120 // Output logging
121 if (ControlPreferences.getInstance().isLoggingEnabled()) {
122 ControlCommandLogger.log(result.toString());
123 }
124 return result;
125 }
126 }
This page took 0.037839 seconds and 4 git commands to generate.