| 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.logging.ControlCommandLogger; |
| 19 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.preferences.ControlPreferences; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandResult; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.ICommandShell; |
| 23 | |
| 24 | /** |
| 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> |
| 29 | * |
| 30 | * @author Bernd Hufmann |
| 31 | */ |
| 32 | public class LTTngControlServiceFactory { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Attributes |
| 36 | // ------------------------------------------------------------------------ |
| 37 | /** |
| 38 | * The singleton instance. |
| 39 | */ |
| 40 | private static LTTngControlServiceFactory fInstance = null; |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Constructor |
| 44 | // ------------------------------------------------------------------------ |
| 45 | /** |
| 46 | * Constructor |
| 47 | */ |
| 48 | private LTTngControlServiceFactory() { |
| 49 | } |
| 50 | |
| 51 | // ------------------------------------------------------------------------ |
| 52 | // Accessors |
| 53 | // ------------------------------------------------------------------------ |
| 54 | /** |
| 55 | * @return the LTTngControlServiceFactory singleton instance. |
| 56 | */ |
| 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 | // ------------------------------------------------------------------------ |
| 67 | /** |
| 68 | * Gets the LTTng Control Service implementation based on the version of the |
| 69 | * remote LTTng Tools. |
| 70 | * |
| 71 | * @param shell - the shell implementation to pass to the service |
| 72 | * @return - LTTng Control Service implementation |
| 73 | * @throws ExecutionException If the command fails |
| 74 | */ |
| 75 | public ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
| 76 | // get the version |
| 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 | } |
| 88 | |
| 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]; |
| 93 | Matcher matcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
| 94 | if (matcher.matches()) { |
| 95 | String version = matcher.group(1).trim(); |
| 96 | if (version.startsWith(LTTngControlServiceConstants.LTTNG_MAJOR_VERSION_2_0)) { |
| 97 | LTTngControlService service = new LTTngControlService(shell); |
| 98 | service.setVersion(version); |
| 99 | return service; |
| 100 | } |
| 101 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ |
| 102 | } |
| 103 | index++; |
| 104 | } |
| 105 | } |
| 106 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); |
| 107 | } |
| 108 | } |