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; | |
9315aeee BH |
18 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
19 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult; | |
20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell; | |
276c17e7 BH |
21 | |
22 | /** | |
276c17e7 BH |
23 | * <p> |
24 | * Factory to create LTTngControlService instances depending on the version of the LTTng Trace Control | |
25 | * installed on the remote host. | |
26 | * </p> | |
cfdb727a | 27 | * |
dbd4432d | 28 | * @author Bernd Hufmann |
276c17e7 BH |
29 | */ |
30 | public class LTTngControlServiceFactory { | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
35 | /** |
36 | * The singleton instance. | |
37 | */ | |
276c17e7 | 38 | private static LTTngControlServiceFactory fInstance = null; |
cfdb727a | 39 | |
276c17e7 BH |
40 | // ------------------------------------------------------------------------ |
41 | // Constructor | |
42 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
43 | /** |
44 | * Constructor | |
45 | */ | |
276c17e7 BH |
46 | private LTTngControlServiceFactory() { |
47 | } | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Accessors | |
51 | // ------------------------------------------------------------------------ | |
c5f68877 BH |
52 | /** |
53 | * @return the LTTngControlServiceFactory singleton instance. | |
54 | */ | |
276c17e7 BH |
55 | public static synchronized LTTngControlServiceFactory getInstance() { |
56 | if (fInstance == null) { | |
57 | fInstance = new LTTngControlServiceFactory(); | |
58 | } | |
59 | return fInstance; | |
60 | } | |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Factory method | |
64 | // ------------------------------------------------------------------------ | |
c5f68877 | 65 | /** |
cfdb727a | 66 | * Gets the LTTng Control Service implementation based on the version of the |
c5f68877 | 67 | * remote LTTng Tools. |
cfdb727a | 68 | * |
c5f68877 BH |
69 | * @param shell - the shell implementation to pass to the service |
70 | * @return - LTTng Control Service implementation | |
cfdb727a | 71 | * @throws ExecutionException If the command fails |
c5f68877 | 72 | */ |
276c17e7 BH |
73 | public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
74 | // get the version | |
75 | ICommandResult result = shell.executeCommand(LTTngControlServiceConstants.CONTROL_COMMAND + LTTngControlServiceConstants.COMMAND_VERSION, new NullProgressMonitor()); | |
cfdb727a | 76 | |
276c17e7 BH |
77 | if ((result != null) && (result.getResult() == 0) && (result.getOutput().length >= 1) && (!LTTngControlServiceConstants.ERROR_PATTERN.matcher(result.getOutput()[0]).matches())) { |
78 | int index = 0; | |
79 | while (index < result.getOutput().length) { | |
80 | String line = result.getOutput()[index]; | |
81 | Matcher matcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); | |
82 | if (matcher.matches()) { | |
83 | String version = matcher.group(1).trim(); | |
84 | if (version.startsWith(LTTngControlServiceConstants.LTTNG_MAJOR_VERSION_2_0)) { | |
85 | LTTngControlService service = new LTTngControlService(shell); | |
86 | service.setVersion(version); | |
87 | return service; | |
88 | } | |
89 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
90 | } | |
91 | index++; | |
92 | } | |
93 | } | |
94 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
95 | } | |
96 | } |