Commit | Line | Data |
---|---|---|
276c17e7 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 Ericsson | |
cfdb727a | 3 | * |
276c17e7 BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
276c17e7 BH |
10 | * Bernd Hufmann - Initial API and implementation |
11 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.service; | |
13 | ||
14 | import java.util.regex.Matcher; | |
15 | ||
16 | import org.eclipse.core.commands.ExecutionException; | |
17 | import org.eclipse.core.runtime.NullProgressMonitor; | |
7a6e4bfd | 18 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.logging.ControlCommandLogger; |
9315aeee | 19 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
7a6e4bfd | 20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.preferences.ControlPreferences; |
9315aeee BH |
21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult; |
22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell; | |
276c17e7 BH |
23 | |
24 | /** | |
276c17e7 BH |
25 | * <p> |
26 | * Factory to create LTTngControlService instances depending on the version of the LTTng Trace Control | |
27 | * installed on the remote host. | |
28 | * </p> | |
cfdb727a | 29 | * |
dbd4432d | 30 | * @author Bernd Hufmann |
276c17e7 BH |
31 | */ |
32 | public class LTTngControlServiceFactory { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
37 | /** |
38 | * The singleton instance. | |
39 | */ | |
276c17e7 | 40 | private static LTTngControlServiceFactory fInstance = null; |
cfdb727a | 41 | |
276c17e7 BH |
42 | // ------------------------------------------------------------------------ |
43 | // Constructor | |
44 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
45 | /** |
46 | * Constructor | |
47 | */ | |
276c17e7 BH |
48 | private LTTngControlServiceFactory() { |
49 | } | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Accessors | |
53 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
54 | /** |
55 | * @return the LTTngControlServiceFactory singleton instance. | |
56 | */ | |
276c17e7 BH |
57 | public static synchronized LTTngControlServiceFactory getInstance() { |
58 | if (fInstance == null) { | |
59 | fInstance = new LTTngControlServiceFactory(); | |
60 | } | |
61 | return fInstance; | |
62 | } | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Factory method | |
66 | // ------------------------------------------------------------------------ | |
c5f68877 | 67 | /** |
cfdb727a | 68 | * Gets the LTTng Control Service implementation based on the version of the |
c5f68877 | 69 | * remote LTTng Tools. |
cfdb727a | 70 | * |
c5f68877 BH |
71 | * @param shell - the shell implementation to pass to the service |
72 | * @return - LTTng Control Service implementation | |
cfdb727a | 73 | * @throws ExecutionException If the command fails |
c5f68877 | 74 | */ |
276c17e7 BH |
75 | public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
76 | // get the version | |
7a6e4bfd BH |
77 | String command = LTTngControlServiceConstants.CONTROL_COMMAND + LTTngControlServiceConstants.COMMAND_VERSION; |
78 | ||
79 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
80 | ControlCommandLogger.log(command); | |
81 | } | |
82 | ||
83 | ICommandResult result = shell.executeCommand(command, new NullProgressMonitor()); | |
84 | ||
85 | if (ControlPreferences.getInstance().isLoggingEnabled()) { | |
86 | ControlCommandLogger.log(LTTngControlService.formatOutput(result)); | |
87 | } | |
cfdb727a | 88 | |
276c17e7 BH |
89 | if ((result != null) && (result.getResult() == 0) && (result.getOutput().length >= 1) && (!LTTngControlServiceConstants.ERROR_PATTERN.matcher(result.getOutput()[0]).matches())) { |
90 | int index = 0; | |
91 | while (index < result.getOutput().length) { | |
92 | String line = result.getOutput()[index]; | |
cfe737e4 BH |
93 | Matcher versionMatcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
94 | if (versionMatcher.matches()) { | |
95 | String version = versionMatcher.group(1).trim(); | |
96 | Matcher matcher = LTTngControlServiceConstants.VERSION_2_PATTERN.matcher(version); | |
97 | if (matcher.matches()) { | |
276c17e7 BH |
98 | LTTngControlService service = new LTTngControlService(shell); |
99 | service.setVersion(version); | |
100 | return service; | |
101 | } | |
102 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
103 | } | |
104 | index++; | |
105 | } | |
106 | } | |
107 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
108 | } | |
109 | } |