| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2016 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.tracecompass.internal.lttng2.control.ui.views.service; |
| 14 | |
| 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 16 | |
| 17 | import java.util.regex.Matcher; |
| 18 | |
| 19 | import org.eclipse.core.commands.ExecutionException; |
| 20 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 21 | import org.eclipse.jdt.annotation.NonNullByDefault; |
| 22 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.logging.ControlCommandLogger; |
| 23 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; |
| 24 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlPreferences; |
| 25 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandInput; |
| 26 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandResult; |
| 27 | import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell; |
| 28 | |
| 29 | /** |
| 30 | * Factory to create LTTngControlService instances depending on the version of |
| 31 | * the LTTng Trace Control installed on the remote host. |
| 32 | * |
| 33 | * @author Bernd Hufmann |
| 34 | */ |
| 35 | @NonNullByDefault |
| 36 | public final class LTTngControlServiceFactory { |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | // Constructor |
| 40 | // ------------------------------------------------------------------------ |
| 41 | /** |
| 42 | * Constructor |
| 43 | */ |
| 44 | private LTTngControlServiceFactory() { |
| 45 | } |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Factory method |
| 49 | // ------------------------------------------------------------------------ |
| 50 | /** |
| 51 | * Gets the LTTng Control Service implementation based on the version of the |
| 52 | * remote LTTng Tools. |
| 53 | * |
| 54 | * @param shell |
| 55 | * - the shell implementation to pass to the service |
| 56 | * @return - LTTng Control Service implementation |
| 57 | * @throws ExecutionException |
| 58 | * If the command fails |
| 59 | */ |
| 60 | public static ILttngControlService getLttngControlService(ICommandShell shell) throws ExecutionException { |
| 61 | // get the version |
| 62 | boolean machineInterfaceMode = true; |
| 63 | |
| 64 | // Looking for a machine interface on LTTng side |
| 65 | ICommandInput command = shell.createCommand(); |
| 66 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); |
| 67 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_OPTION); |
| 68 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND_MI_XML); |
| 69 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); |
| 70 | ICommandResult result = executeCommand(shell, command); |
| 71 | |
| 72 | if (result.getResult() != 0) { |
| 73 | machineInterfaceMode = false; |
| 74 | // Fall back if no machine interface is present |
| 75 | command = shell.createCommand(); |
| 76 | command.add(LTTngControlServiceConstants.CONTROL_COMMAND); |
| 77 | command.add(LTTngControlServiceConstants.COMMAND_VERSION); |
| 78 | result = executeCommand(shell, command); |
| 79 | } |
| 80 | |
| 81 | if ((result.getResult() == 0) && (!result.getOutput().isEmpty())) { |
| 82 | if (machineInterfaceMode) { |
| 83 | LTTngControlServiceMI service = new LTTngControlServiceMI(shell, LTTngControlServiceMI.parseVersion(result)); |
| 84 | return service; |
| 85 | } |
| 86 | |
| 87 | for (String line : result.getOutput()) { |
| 88 | line = line.replace("-", "."); //$NON-NLS-1$//$NON-NLS-2$ |
| 89 | Matcher versionMatcher = LTTngControlServiceConstants.VERSION_PATTERN.matcher(line); |
| 90 | if (versionMatcher.matches()) { |
| 91 | String version = versionMatcher.group(1).trim(); |
| 92 | Matcher matcher = LTTngControlServiceConstants.VERSION_2_PATTERN.matcher(version); |
| 93 | if (matcher.matches()) { |
| 94 | LTTngControlService service = new LTTngControlService(shell); |
| 95 | service.setVersion(checkNotNull(version)); |
| 96 | return service; |
| 97 | } |
| 98 | throw new ExecutionException(Messages.TraceControl_UnsupportedVersionError + ": " + version); //$NON-NLS-1$ |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | throw new ExecutionException(Messages.TraceControl_GettingVersionError); |
| 103 | } |
| 104 | |
| 105 | private static ICommandResult executeCommand(ICommandShell shell, ICommandInput command) throws ExecutionException { |
| 106 | // Logging |
| 107 | if (ControlPreferences.getInstance().isLoggingEnabled()) { |
| 108 | ControlCommandLogger.log(command.toString()); |
| 109 | } |
| 110 | |
| 111 | ICommandResult result = null; |
| 112 | |
| 113 | try { |
| 114 | result = shell.executeCommand(command, new NullProgressMonitor()); |
| 115 | } catch (ExecutionException e) { |
| 116 | throw new ExecutionException(Messages.TraceControl_GettingVersionError + ": " + e); //$NON-NLS-1$ |
| 117 | } |
| 118 | |
| 119 | // Output logging |
| 120 | if (ControlPreferences.getInstance().isLoggingEnabled()) { |
| 121 | ControlCommandLogger.log(result.toString()); |
| 122 | } |
| 123 | return result; |
| 124 | } |
| 125 | } |