Commit | Line | Data |
---|---|---|
276c17e7 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 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 | **********************************************************************/ | |
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; | |
18 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages; | |
19 | ||
20 | /** | |
21 | * <b><u>LTTngControlServiceFactory</u></b> | |
22 | * <p> | |
23 | * Factory to create LTTngControlService instances depending on the version of the LTTng Trace Control | |
24 | * installed on the remote host. | |
25 | * </p> | |
26 | */ | |
27 | public class LTTngControlServiceFactory { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | private static LTTngControlServiceFactory fInstance = null; | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Constructor | |
36 | // ------------------------------------------------------------------------ | |
37 | private LTTngControlServiceFactory() { | |
38 | } | |
39 | ||
40 | // ------------------------------------------------------------------------ | |
41 | // Accessors | |
42 | // ------------------------------------------------------------------------ | |
43 | public static synchronized LTTngControlServiceFactory getInstance() { | |
44 | if (fInstance == null) { | |
45 | fInstance = new LTTngControlServiceFactory(); | |
46 | } | |
47 | return fInstance; | |
48 | } | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Factory method | |
52 | // ------------------------------------------------------------------------ | |
53 | public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { | |
54 | // get the version | |
55 | ICommandResult result = shell.executeCommand(LTTngControlServiceConstants.CONTROL_COMMAND + LTTngControlServiceConstants.COMMAND_VERSION, new NullProgressMonitor()); | |
56 | ||
57 | if ((result != null) && (result.getResult() == 0) && (result.getOutput().length >= 1) && (!LTTngControlServiceConstants.ERROR_PATTERN.matcher(result.getOutput()[0]).matches())) { | |
58 | int index = 0; | |
59 | while (index < result.getOutput().length) { | |
60 | String line = result.getOutput()[index]; | |
61 | Matcher matcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); | |
62 | if (matcher.matches()) { | |
63 | String version = matcher.group(1).trim(); | |
64 | if (version.startsWith(LTTngControlServiceConstants.LTTNG_MAJOR_VERSION_2_0)) { | |
65 | LTTngControlService service = new LTTngControlService(shell); | |
66 | service.setVersion(version); | |
67 | return service; | |
68 | } | |
69 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ | |
70 | } | |
71 | index++; | |
72 | } | |
73 | } | |
74 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); | |
75 | } | |
76 | } |