tmf: lttngControl: mi: basic listing support
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / service / LTTngControlServiceFactory.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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.linuxtools.internal.lttng2.control.ui.views.service;
14
15 import java.util.regex.Matcher;
16
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;
24
25 /**
26 * Factory to create LTTngControlService instances depending on the version of
27 * the LTTng Trace Control installed on the remote host.
28 *
29 * @author Bernd Hufmann
30 */
31 public class LTTngControlServiceFactory {
32
33 // ------------------------------------------------------------------------
34 // Attributes
35 // ------------------------------------------------------------------------
36 /**
37 * The singleton instance.
38 */
39 private static LTTngControlServiceFactory fInstance = null;
40
41 // ------------------------------------------------------------------------
42 // Constructor
43 // ------------------------------------------------------------------------
44 /**
45 * Constructor
46 */
47 private LTTngControlServiceFactory() {
48 }
49
50 // ------------------------------------------------------------------------
51 // Accessors
52 // ------------------------------------------------------------------------
53 /**
54 * @return the LTTngControlServiceFactory singleton instance.
55 */
56 public static synchronized LTTngControlServiceFactory getInstance() {
57 if (fInstance == null) {
58 fInstance = new LTTngControlServiceFactory();
59 }
60 return fInstance;
61 }
62
63 // ------------------------------------------------------------------------
64 // Factory method
65 // ------------------------------------------------------------------------
66 /**
67 * Gets the LTTng Control Service implementation based on the version of the
68 * remote LTTng Tools.
69 *
70 * @param shell
71 * - the shell implementation to pass to the service
72 * @return - LTTng Control Service implementation
73 * @throws ExecutionException
74 * If the command fails
75 */
76 public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException {
77 // get the version
78 boolean machineInterfaceMode = true;
79 String command = LTTngControlServiceConstants.CONTROL_COMMAND + LTTngControlServiceConstants.COMMAND_VERSION;
80 String commandMi = LTTngControlServiceConstants.CONTROL_COMMAND_MI_XML + LTTngControlServiceConstants.COMMAND_VERSION;
81
82 // Logging
83 if (ControlPreferences.getInstance().isLoggingEnabled()) {
84 ControlCommandLogger.log(commandMi);
85 }
86
87 ICommandResult result = null;
88
89 // Looking for a machine interface on LTTng side
90 try {
91 result = shell.executeCommand(commandMi, new NullProgressMonitor());
92 } catch (ExecutionException e) {
93 throw new ExecutionException(Messages.TraceControl_GettingVersionError, e);
94 }
95
96 // Output logging
97 if (ControlPreferences.getInstance().isLoggingEnabled()) {
98 ControlCommandLogger.log(LTTngControlService.formatOutput(result));
99 }
100
101 if (result.getResult() != 0) {
102 machineInterfaceMode = false;
103 // Fall back if no machine interface is present
104
105 // Logging
106 if (ControlPreferences.getInstance().isLoggingEnabled()) {
107 ControlCommandLogger.log(command);
108 }
109
110 try {
111 result = shell.executeCommand(command, new NullProgressMonitor());
112 } catch (ExecutionException e) {
113 throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$
114 }
115
116 // Output logging
117 if (ControlPreferences.getInstance().isLoggingEnabled()) {
118 ControlCommandLogger.log(LTTngControlService.formatOutput(result));
119 }
120 }
121
122
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());
127 return service;
128 }
129 int index = 0;
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);
139 return service;
140 }
141 throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$
142 }
143 index++;
144 }
145 }
146 throw new ExecutionException(Messages.TraceControl_GettingVersionError);
147 }
148 }
This page took 0.036438 seconds and 5 git commands to generate.